Podcast
Questions and Answers
Which of the following is NOT a valid function naming rule?
Which of the following is NOT a valid function naming rule?
What is the purpose of a function definition?
What is the purpose of a function definition?
What is included in the function header?
What is included in the function header?
What happens when a function is called?
What happens when a function is called?
Signup and view all the answers
How should a function name be structured for best clarity?
How should a function name be structured for best clarity?
Signup and view all the answers
What does the special value None indicate when returned from a function?
What does the special value None indicate when returned from a function?
Signup and view all the answers
Which function from the math module returns the smallest integer greater than or equal to x?
Which function from the math module returns the smallest integer greater than or equal to x?
Signup and view all the answers
Before using the math module in a Python program, which statement is required?
Before using the math module in a Python program, which statement is required?
Signup and view all the answers
What does the function sqrt(x) return?
What does the function sqrt(x) return?
Signup and view all the answers
If num2 is zero in the divide(num1, num2) function, what is the expected output?
If num2 is zero in the divide(num1, num2) function, what is the expected output?
Signup and view all the answers
What is the scope of a local variable?
What is the scope of a local variable?
Signup and view all the answers
What happens when a function tries to access a local variable that has not been created yet?
What happens when a function tries to access a local variable that has not been created yet?
Signup and view all the answers
How are arguments passed to a function when it is called?
How are arguments passed to a function when it is called?
Signup and view all the answers
What defines a parameter in a function?
What defines a parameter in a function?
Signup and view all the answers
What is the purpose of the penup
method in the square function?
What is the purpose of the penup
method in the square function?
Signup and view all the answers
In the circle function, how is the turtle positioned before drawing the circle?
In the circle function, how is the turtle positioned before drawing the circle?
Signup and view all the answers
What allows a Python function to accept multiple arguments?
What allows a Python function to accept multiple arguments?
Signup and view all the answers
Which statement about local variables is true?
Which statement about local variables is true?
Signup and view all the answers
Which line of code begins the filling process for both the square and circle functions?
Which line of code begins the filling process for both the square and circle functions?
Signup and view all the answers
What parameters are required to call the square
function correctly?
What parameters are required to call the square
function correctly?
Signup and view all the answers
In the context of function execution, what is the relationship between parameters and arguments?
In the context of function execution, what is the relationship between parameters and arguments?
Signup and view all the answers
In a function that accepts multiple arguments, how does Python match the arguments?
In a function that accepts multiple arguments, how does Python match the arguments?
Signup and view all the answers
How many times does the loop execute in the square function to draw the square?
How many times does the loop execute in the square function to draw the square?
Signup and view all the answers
What happens to the argument when a function modifies the parameter value?
What happens to the argument when a function modifies the parameter value?
Signup and view all the answers
How are keyword arguments beneficial in function calls?
How are keyword arguments beneficial in function calls?
Signup and view all the answers
What is a requirement when mixing positional and keyword arguments in a function call?
What is a requirement when mixing positional and keyword arguments in a function call?
Signup and view all the answers
What is the term used to describe when changes made in a function do not affect the original variable?
What is the term used to describe when changes made in a function do not affect the original variable?
Signup and view all the answers
In the context of function parameters, which option best describes the use of keyword arguments?
In the context of function parameters, which option best describes the use of keyword arguments?
Signup and view all the answers
Which statement about keyword arguments is correct?
Which statement about keyword arguments is correct?
Signup and view all the answers
When calling a function with both positional and keyword arguments, what type of error occurs if the order is reversed?
When calling a function with both positional and keyword arguments, what type of error occurs if the order is reversed?
Signup and view all the answers
What does the keyword argument format look like when you are calling a function?
What does the keyword argument format look like when you are calling a function?
Signup and view all the answers
What must be declared inside a function if it needs to assign a value to a global variable?
What must be declared inside a function if it needs to assign a value to a global variable?
Signup and view all the answers
Why are global variables generally avoided in programming?
Why are global variables generally avoided in programming?
Signup and view all the answers
Which of the following statements is true regarding global constants?
Which of the following statements is true regarding global constants?
Signup and view all the answers
Which action is necessary to utilize a function from a module after importing it?
Which action is necessary to utilize a function from a module after importing it?
Signup and view all the answers
What type of function does not return a value?
What type of function does not return a value?
Signup and view all the answers
What is the purpose of a seed value in the context of random number generation?
What is the purpose of a seed value in the context of random number generation?
Signup and view all the answers
Which of the following correctly describes the randint function?
Which of the following correctly describes the randint function?
Signup and view all the answers
What does the random function return?
What does the random function return?
Signup and view all the answers
What must be included in a return statement in a value-returning function?
What must be included in a return statement in a value-returning function?
Signup and view all the answers
How can global constants be simulated in Python?
How can global constants be simulated in Python?
Signup and view all the answers
What is the main disadvantage of functions using global variables?
What is the main disadvantage of functions using global variables?
Signup and view all the answers
Which statement about the random module is incorrect?
Which statement about the random module is incorrect?
Signup and view all the answers
What happens when a variable is assigned a value inside a function without declaring it global?
What happens when a variable is assigned a value inside a function without declaring it global?
Signup and view all the answers
Study Notes
Introduction to Functions
- Function: a group of statements within a program that performs a specific task.
- Large programs are often broken down into smaller, more manageable tasks, which can be performed by functions.
- This divide-and-conquer approach simplifies program design, testing, and maintenance.
- Modularized programs: each task in the program is in its own function.
Topics
- Introduction to Functions
- Defining and Calling a Void Function
- Designing a Program to Use Functions
- Local Variables
- Passing Arguments to Functions
- Global Variables and Global Constants
- Turtle Graphics: Modularizing Code with Functions
- Introduction to Value-Returning Functions
- Generating Random Numbers
- Writing Your Own Value-Returning Functions
- The math Module
- Storing Functions in Modules
Void Functions and Value-Returning Functions
- Void function: performs a task and terminates.
- Value-returning function: performs a task and returns a value.
- Value can then be used later in the program.
Defining and Calling a Void Function
-
Function names must follow Python naming rules.
-
Names cannot use keywords or contain spaces.
-
First character must be a letter or underscore; other characters can be letters, numbers, or underscores.
-
Case-sensitive.
-
Function name should be descriptive of the task it performs.
-
Function definition: specifies a function’s actions.
-
Function header: first line of a function that includes the def keyword, function name, parentheses, and colon.
-
Block: statements that belong together as a group (indentation after the colon).
-
A function definition specifies what a function does but does not execute it.
-
Calling a function to execute it. The interpreter jumps to the function and executes statements contained within the block. It then jumps back to the part of the program that called the function and resumes execution from that point.
Example of Defining and Calling a Function
- A function can be called from within another function as well as from the main part of a program.
- The order of functions matters.
Defining and Calling a Void Function (cont.)
- Main function: starts when the program starts and calls other functions as needed; important logic of the program.
Indentation in Python
- Each block of code must be indented.
- Use consistent indentation for readable code.
- Python interpreters use indentation to determine the structure of the code.
Designing a Program to Use Functions
- Flowchart: visual representation of the flow of logic within a function, but it doesn't show the relationship between functions.
- Hierarchy chart: AKA Structure Chart. Shows relationship between functions. Box for each function; lines illustrate which functions call which functions. Shows only the flow between, but not what happens inside each function.
Using the pass Keyword
- Use the pass keyword, when writing a function that has no specific action.
Local Variables
- Local variable: a variable assigned a value inside a function.
- Only accessible inside the function where it was assigned.
Scope
- Scope: part of a program where a variable can be accessed (for local variables, it's the function where it's created).
- Local variables cannot be seen, or used, in any functions that come before their creation.
- Functions can have local variables with the same name.
Passing Arguments to Functions
- Argument: data given to a function when it is called.
- Parameter: a variable that receives an argument.
- Arguments are passed to parameters by position.
- Function can perform calculations by using an argument provided in the code.
Passing Multiple Arguments
- Functions can accept multiple arguments.
- The arguments are passed to corresponding parameters via their position in the parameter list.
Making Changes to Parameters
- Changes to parameter values inside a function do not affect the original argument values outside the function.
- This unidirectional communication method is also known as pass-by-value.
Keyword Arguments
- Keyword arguments specify which parameter should receive a given value.
- The order of keyword arguments does not matter in the function call.
Mixing Keyword Arguments with Positional Arguments
- Positional arguments must come before keyword arguments. Otherwise, Python will produce an error.
Global Variables
- Global variable: declared outside of all functions
- Can be accessed by many statements in the program, including from within any function.
- Can be modified by functions.
- If a function needs to change a global variable’s value, the statement global var_name must appear at the top of the function.
Global Constants
- Constants cannot be changed by functions.
- Use to give values like pi or other constants in a program.
- Values are given once, and functions do not change them.
Introduction to Value-Returning Functions: Generating Random Numbers
- Value-returning functions: perform a task and return a value.
- The value is returned to the part of the program that called the function when the function finishes executing.
Standard Library Functions and the import Statement
- Standard library: a pre-written set of functions, included with Python, that programmers commonly use.
Modules
- Modules: Files that organize the standard library, which helps organize the library functions into files.
- When calling a function from a module, you must first import the module.
- Module names normally end with .py. The name should not be the same as a Python keyword.
Generating Random Numbers
- Random number generation: the random module (randint(), randrange(), random(), uniform).
Random Number Seeds
- Random number seeds are used to initialize random numbers.
- Different seeds give different series of random numbers.
- Random seeds are used to adjust the randomness and repeat processes with random functions.
Writing Your Own Value-Returning Functions
- Functions that return values are written simply; one or multiple return statements are used.
- Returns a value specified in the return statement, to the part of the program that called it.
- Expressions in a return statement may be complex .
Example: How to Use the return value of a Function
- Returning values from a function to use in other calculations or to simplify complex calculations.
How to Use Value-Returning Functions
- You can use value-returning functions for specific situations involving input, mathematical calculations.
- Use returned values in variables or as arguments in other functions.
Using IPO Charts
- IPO Charts: a tool for describing input, processing, and output in a function.
- Use columns to lay out the input, processing and output.
###Returning Strings
- Functions can return strings.
Returning Boolean Values
- Boolean functions either return True or False.
- Can use in decision/repetition structures.
Returning Multiple Values
-
Functions can return multiple values separated by commas
-
Use variables on the left side of the = operator to receive return values.
Returning None From a Function
- None: Special value that means there's no return value.
- Can be returned by functions to indicate an error.
The math Module
- Part of the standard library containing helpful mathematical functions, which need to be imported to use.
- This module includes functions like sqrt(), cos(), sin(), etc. and constants like pi, e.
- The math module functions are typically called using the dot notation format (module_name.function_name()).
Storing Functions in Modules
Menu Driven Programs
Conditionally Executing the main Function
- The special variable
__name__
will be set to"main"
when the file is run directly, and the name of the module when the file is imported into another program. A simple conditional statement allows you to execute code withinmain()
that you wish only ran when the file is run directly.
Turtle Graphics
- Turtle Graphics operations can be stored in functions and called when needed.
- Functions can be written to perform specific operations on turtles like creating squares, circles and lines.
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Related Documents
Description
Test your knowledge on Python functions with this quiz. It covers function naming rules, definitions, headers, and behaviors during function calls. Assess your understanding of the math module and common pitfalls when using functions in Python.