Functions Flashcards
Explain calling a function and what a function call is
- Using function = calling
- Line of code that uses function = function call
What is a function?
- Code that you’ve given a name to (such as print, str, iter, or next).
- Accomplish a task.
- When you use that function name in a program, execution of the code jumps to that function block, executes the function, and then returns to the point where the function name was called
How to define and call a simple function? Give example
def my_first_function(): print("Hello you") my_first_function()
Define Scope
- Scope refers to the definition of variables or functions in Python and the accessibility in different parts of program .
- Applies to variables and functions
**Local Scope: **
Variables created within a function and accessible only inside that function.
Global Scope:
Variables defined outside any function, accessible anywhere in the code.
What scope rule exists with variables created in functions?
If a variable was created in a function, it only exists in that function
What happens when you define my_variable inside and outside a function and both Variables have the same name
- Because the variable within the function only exists within the function, to the rest of the program it is as if that variable does not exist.
- That’s why when we change the value of that variable, it does not change the value of the variable outside of the function.
- The interpreter actually creates two variables with the same name, one within the function and one outside of the function. Changing the value of one will not affect the other.
What is a nested function?
- Function defined within another function.
- Scope of parent function
What is a global Variable and how to access it within functions. Demonstrate.
You use the global keyword to preface the variable you’re going to use. This will tell the interpreter that you want to use the global version of that variable instead of creating a new local version
x = 1 def my_function() global x x = 10 print(x) my_function() print(x)
What is a nonlocal variable?
- Access a function’s local variable from a nested function within that function.
-
nonlocal
keyword. - Tells interpreter that you want to use the version created in the parent function
What are Arguments?
Values that are passed into functions via parameters
Where are function parameters defined?
Between open and closed parentheses is the function definition
Each parameter is seperated by a comma
Define an example of a simple function with parameters
def add_num(a, b, c) the_sum = a + b + c print(the_sum) add_num(1, 5, 6)
Can you think of an important rule for arguments passed into function?
Number of arguments passed into function must be equal to number of arguments specified in function definition
What are the two types of argument setups. Give example
Positional arguments
Correct position is crucial
~~~
add_num(1, 5, 6)
~~~
Named arguments
Order of arguments not important, naming is important add_num(b=4, c=100, a=1)
What are default arguments?
- You can specify a default value for an argument in a function definition, and if that argument is not passed in during the function call, the argument will assume the default value.
- If the argument is passed in during the function call, the passed in value overrides the default value.
- You specify a default value for an argument by setting it equal to a value in the function definition