Functions Flashcards

1
Q

Define all the sections of this function?
~~~
def 1______(2________):
“ 3______”

return 4______

result = name(5_______)
~~~

A
  1. Function definition or Funcation name
  2. Parameter(s)
  3. Docstring
  4. Return value
  5. Argument(s)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are the different type of function arguments?

A
  1. Postional arguments
  2. Keyword arguments
  3. Default values(arguments)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

With regards to Functions, what are Keyword arguments?

A
  1. Keyword argument is a name-vaule pair that you pass to a function.
  2. The “=” is needed to assign the value for each variable
  3. You can pass as many variables as need, 1, 2, 4, 10, or 100 varibles
    Eg. def name_maker(first_name=”Miachel”, last_name= “Jackson”):
    ~~~
    def name_maker(first_name=”Miachel”, last_name= “Jackson”):
    return f”{first_name} {last_name}”

print(name_maker())
~~~

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

Can you have all default Parameter calls in Functions?

A

Yes, the can, they all have set arguments.
~~~
def name_maker(first_name=”Stev”, middle_name=”Donnar”, last_name=”Wilson”, ):
return f”{first_name} {middle_name} {last_name}”

print(name_maker())
print(name_maker(“Michael”, “Joseph”, “Jaskson”))
print(name_maker(“Jaskson”, “Joseph”, “Michael”))
~~~
When called like this, the position matters, similar to Positional arguments:
Output:
print(name_maker()): Stev Donnar Wilson

print(name_maker(“Michael”, “Joseph”, “Jaskson”)): Michael Joseph Jaskson

print(name_maker(“Jaskson”, “Joseph”, “Michael”)): Jaskson Joseph Michael

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

What is the most import thing with regards to positional arguments?

A

The order the argument is passed, matters in functional arguments.
First name must come first, then last name.
The order of the function call must match the order of the parameters in the function definition.
See below:
~~~
def name_maker(first_name, last_name):
return f”{first_name} {last_name}”

print(name_maker(“Michael”, “Jackson”))
print(name_maker(“Jackson”, “Miachel”))
~~~
Output:
print(name_maker(“Michael”, “Jackson”))
Miachel Jackson

print(name_maker(“Jackson”, “Michael”))
Jackson Michael

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

What most import for calling Default values(aruguments)?

A

Default values(aruguments) most be the last parameter set in the Function call.
def name_maker(first_name, last_name, middle_name=”Joseph”):

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

What are the names for all of these function calls?
1. describe_pet(‘willie’)
2. describe_pet(pet_name=’willie’)
3. describe_pet(‘harry’, ‘hamster’)
4. describe_pet(pet_name=’harry’, animal_type=’hamster’)
5. describe_pet(animal_type=’hamster’, pet_name=’harry’)
6. describe_pet(animal_type, pet_name, size=small)

A
  1. Positional
  2. Keyword
  3. Positional
  4. keyword, with positional
  5. Default
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

With regards to Functions, what are Default values(aruguments)?

A

Default values is the passed vaule os a parameter provided in a Function call. Python will use the provided value if no argument is passed in the function call.
~~~
def name_maker(first_name, last_name, middle_name=”Joseph”):
return f”{first_name} {middle_name} {last_name}”

print(name_maker(“Michael”, “Jaskson”))
~~~
Output:
Michael Joseph Jaskson

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

Function annotations

A

Function annotations gives an indication of the expected return data type.
~~~
def word_multiplier(word: str, times: int) -> str:
return word * times

print(word_multiplier(10,16))
~~~

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

What are the steps the steps to create a Function annotation?

A
  1. : data type
  2. (If there is a second variable) : data type
    3.After ) -> data type
    Eg.
    ~~~
    def word_multiplier(word : str, times : in*) -> str:
    return word * times
    ~~~
    Explination:

def funcation_name(variable: < data type>str, variable: < data type>int) -> < data type>str

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