Section 14: Arguments and FUnction Objects Flashcards
Review
What are the different parts of a function called
Positional, Keywork, Display
What is the difference between a Positional Argument and a Keyword Argument?
```python
def display_square(x, message):
print(message, x*x)
~~~
(1) Positional Arguments must be the same order as parameters:
Call function:display_square(5, 'Square of 5')
(2) Keyword Arguments are “wordy” but can be in different order:
Call Function: display_square(message = 'Square of 5', x = 5)
Positional, Keywork, Default
What are default values?
(3) Default Value used when the argument is missing from the call
- If the call the function without providing a value for the message, its default value will be used:
```python
def display_square(x, message = “The solution is”):
#Message arugment is default equal to ‘The solution is’
#If the function is called without an argument, default is used
print(message, x*x)
> > > display_square(5)
The soluation is 25
~~~
What happens when a function defintiion is called?
When a function definition is executed:
- An object created that stores the code of the function
- Reference to this object is stored in variable named after the function
How are functions “callable?”
Function objects are callable, they can use the invocation operator () to execute the code
Note: Difference between …
-
my_function
: a variable references a function object -
my_fuction(1, 2)
: a call to the function object, evaluates whatever it returns
How can functions be used as variables?
Note: Difference between …
-
my_function
: a variable references a function object -
my_fuction(1, 2)
: a call to the function object, evaluates whatever it returns
Functions as variables: can be assigned to another variable
How can functions be used as arguments?
Note: Difference between …
-
my_function
: a variable references a function object -
my_fuction(1, 2)
: a call to the function object, evaluates whatever it returns
Functions as arguments: can be passed as arguments to another function
How can functions be a return type?
Functions as return types: can be returned from a function