Section 14: Arguments and FUnction Objects Flashcards

1
Q

Review

What are the different parts of a function called

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Positional, Keywork, Display

What is the difference between a Positional Argument and a Keyword Argument?

A

```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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Positional, Keywork, Default

What are default values?

A

(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
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What happens when a function defintiion is called?

A

When a function definition is executed:

  1. An object created that stores the code of the function
  2. Reference to this object is stored in variable named after the function
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How are functions “callable?”

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

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
A

Functions as variables: can be assigned to another variable

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

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
A

Functions as arguments: can be passed as arguments to another function

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How can functions be a return type?

A

Functions as return types: can be returned from a function

How well did you know this?
1
Not at all
2
3
4
5
Perfectly