Questions
Conceptual
What is a function call, and how does it differ from a function definition?
What is the difference between parameters and arguments in the context of functions?
What is a function signature, and why is it significant?
Can the return statement in a function be replaced with a print statement? Why or why not?
Does a function need to have a return statement? Why or why not?
What will follow the arrow in a function signature if the return statement is “return None”? What does this mean?
Use the following to write the format of a function signature. You might not need to use all and can use any multiple times:
def
,()
,=
,function_name
,param_1
,param_2
,param_n
,arg_1
,arg_2
,arg_n
,type_of_value
,->
,:
Use the following to write the format of a function call. You might not need to use all and can use any multiple times:
def
,()
,=
,function_name
,param_1
,param_2
,param_n
,arg_1
,arg_2
,arg_n
,type_of_value
,->
,:
When you call a function, once the function is executed, where do you return back to?
When you call a function, is the return value outputted? If yes, explain how? If not, explain how you could print the return value?
If you never encounter a
return
statement in your function, what is your return value?What is the purpose of the
return
keyword, and what happens if it is omitted in a function?What happens if you call a function before it is defined? Why?
How can you pass a function as an argument to another function? How does this work? Provide an example.
The following questions questions will refer to the code snippet below:
def echo(word: str, times: int) -> str:
return word * times
print(echo(word="hello", times=4))
15.1 Identify where a call to a function occurs. How do you know it is a call? 15.2 Where do we have a return type declared? 15.3 Where do we have a return statement? 15.4 What value does the call to the function hold? How do you know? Show this both through code and through a memory diagram of the snippet. 15.5 Explain why print(echo)
does not function the same as echo(word="hello", times=4)
. Show this both through code and through a memory diagram of the snippet. 15.6 Change the code to provide the same output but with a different functionality. 15.7 On line 2, we multiply word
by times
, both of which are local variables. In the operation word * times
, what exactly is being multiplied?
- For the following code, explain what is happening on line 5 with the statement
word=word
withinreturning(word=word)
. Use the following terms in your explanation: value, local variable, parameter, assignment operator, keyword argument, and call.
def returning(word: str) -> str:
return word
def echo(word: str, times: int) -> str:
return returning(word=word) * times
print(echo(word="hello", times=4))
Calling functions
- For the following code snippet, write a line of code that will result in the following output:
def flavor(taste: str, percent: float) -> None:
print("This flavor is " + str(percent) + "% " + taste)
Output:
$ python -m flavor
This flavor is 100% umami
- For the following code snippet, write a line of code that will call the
main
function. Next, write a line of code to be inserted within the body of themain
function. This line should call theeat
function with the argument passed when the main function was called.
def eat(food: str) -> None:
print("Eating " + food)
def main(food: str) -> None:
= "apple" food_item
- For the following code snippet, write a line of code that will call the
have_done
function with the arguments"homework"
andFalse
. If you wanted to print the string created by thehave_done
function, how would you modify your function call?
def have_done(task, completed) -> str:
return "Completed " + task + ": " + str(completed)
Spot the error
- Does the following code contain any issues that could affect its example usage? If yes, what is it and why is it a problem?
def fuel_needed(distance: float, mpg: float) -> float:
return distance / mpg
def total_fuel_cost(distance: float, mpg: float, price_per_gallon: float) -> float:
=distance, mpg=mpg) * price_per_gallon
fuel_needed(distance
# Example usage:
print(total_fuel_cost(distance=300, mpg=25, price_per_gallon=4))
- Does the following code contain any issues that could affect its example usage? If yes, what is it and why is it a problem?
def fuel_needed(distance: float, mpg: float) -> float:
return distance / mpg
def total_fuel_cost(distance: float, mpg: float, price_per_gallon: float) -> None:
print(fuel_needed(distance=distance, mpg=mpg) * price_per_gallon)
# Example usage:
=300, mpg=25, price_per_gallon=4) total_fuel_cost(distance
- Does the following code contain any issues that could affect its example usage? If yes, what is it and why is it a problem?
def greet(name: str) -> str:
print("Hello " + name + ", your name starts with an " + name[0] + " and ends with an " + name[len(name) - 1])
return "I'm so happy to see you " + name + "!"
def main() -> None:
print(greet(name="Molly"))
# Example usage:
main()
- Does the following code contain any issues that could affect its example usage? If yes, what is it and why is it a problem?
def greet(name: str) -> str:
return "I'm so happy to see you " + name + "!"
print("Hello " + name + ", your name starts with an " + str(name[0]) + " and ends with an " + str(name[len(name) - 1]))
def main() -> None:
print(greet(name="Molly"))
# Example usage:
main()
- Does the following code contain any issues that could affect its example usage? If yes, what is it and why is it a problem?
str) -> str:
greet(name: return "I'm so happy to see you " + name + "!"
print("Hello " + name + ", your name starts with an " + str(name[0]) + " and ends with an " + str(name[len(name) - 1]))
def main() -> None:
print(greet(name="Molly"))
# Example usage:
main()
- Does the following code contain any issues that could affect its example usage? If yes, what is it and why is it a problem?
def greet(name: str) -> str:
print("I'm so happy to see you " + name + "!")
print("Hello " + name + ", your name starts with an " + str(name[0]) + " and ends with an " + str(name[len(name) - 1]))
def main() -> None:
print(greet(name="Molly"))
# Example usage:
main()
Solutions
Conceptual
A function definition is where you create the function and specify what it does. It includes the function signature and everything indented below it. The signature consists of the function name, parameters, the types of the parameters, and the return type. The function body contains the code block that executes when the function is called. A function call occurs when you invoke or execute the function by writing the function name followed by parentheses. If the function accepts inputs, the parentheses will contain the values assigned to the parameters, also known as arguments. When a function definition is encountered, the function body is not executed; it is bypassed until the function is called. To call a function, you must first define it.
Parameters are the variables listed in the function signature that define the expected inputs. Your parameters will hold the values passed into the function when the function is called. These values that are assigned to the parameters are called arguments. Arguments are the actual values passed to the function when it is called.
A function signature refers to the first line of a function definition, where the
def
keyword is used. Following thedef
keyword is the function name, and after the function name is a set of parentheses that enclose the parameter list. After the parameter list comes the return type. The function signature is important because it defines how the function can be called and what kinds of inputs and outputs are expected. Without a signature, you wouldn’t know what parameters to provide or even how to call the function, since the function name would be missing.No, the
return
statement cannot be replaced with aprint
statement. The return statement ends the function execution and passes a value back to the caller. Theprint
statement merely outputs a value to the console without returning anything to the function caller.No, a function does not need to have a return statement. If no return is present, the function will return None by default after completing its execution. However, using return is necessary when you need to send a value back to the caller.
The arrow (
->
) in a function signature will be followed byNone
. This indicates that the function does not return any meaningful value (ex.int
,float
,bool
, etc.) and will returnNone
.
def function_name(param_1: type_of_value, param_2: type_of_value, param_n: type_of_value) -> type_of_value:
=arg_1, param_2=arg_2, param_n=arg_n) function_name(param_1
After the function is executed, you will return back to the point in the code where the function was called.
No, the return value is not automatically outputted. To see the return value, you need to use a print statement to output it. Your argument within the print function is going to be the call to your function that you would like to receive the return value from.
If there is no
return
statement in a function, it returnsNone
by default.The return keyword ends the function execution and specifies the value to be returned to the caller. If omitted, the function will return
None
after execution.If you call a function before it is defined, you will get an error. Python reads from top to bottom and so if the function is not defined and memorized in memory, when python encounters the function at a function call, it will not know what the function_name is referring to. The function must be defined before it can be called as Python needs to know that the function is defined before executing trying to call it.
To pass a function’s return value as an argument to another function, you need to call the first function, which will execute it and produce a return value. That return value can then be passed as an argument to the second function.
This works because when you call the first function, it returns a value, and that value is then used as input (an argument) for the second function.
Example:
def double(x: int) -> int:
return x * 2
def add_five(y: int) -> int:
return y + 5
print(add_five(y=double(x=3)))
- The following questions questions will refer to the code snippet below:
def echo(word: str, times: int) -> str:
return word * times
print(echo(word="hello", times=4))
15.1 Line 4. echo
is called. We know it is a call as it follows the syntax described in question 8 of the conceptual problems.
15.2 Line 1. The signature declares the return type.
15.3 Line 2.
15.4 “hellohellohellohello”. Show this both through code and through a memory diagram of the snippet.
15.5 echo
is a function. In memory, it holds the value id: 1
within the Globals
frame. When we print(echo)
, we are printing the value that echo
holds, which is the ID number, id: 1
. echo(word="hello", times=4)
is a call to the function and holds the return value. In code, if you type print(echo)
, you will receive something similar to <function echo at 0x1012a22a0>
, where 0x1012a22a0
is a hexadecimal representation of a reference number to a location in memory. Just like in memory, the name of the function, echo
, is identified as being defined as a function.
15.6
def echo(word: str, times: int) -> None:
print(word * times)
="hello", times=4) echo(word
15.7 The values that the local variables hold. Whenever we reference any variable (such as a parameter), we are referencing the value that it holds.
- On line 5, within the call
returning(word=word)
, the statementword=word
uses a keyword argument to pass a value to thereturning
function. In this context,word
on the left side of the=
is a parameter in thereturning
function, whileword
on the right side is a local variable from theecho
function. The local variableword
holds the value"hello"
whenecho
is executed. The assignment operator=
is used here to pass this value to the parameterword
in thereturning
function. Thus, the keyword argumentword=word
effectively assigns the value of the local variableword
fromecho
to the parameterword
inreturning
, ensuring thatreturning
receives the correct value to process and return.
Calling functions
For the
flavor
function:To call the
flavor
function using keyword arguments:="umami", percent=100.0) flavor(taste
For calling the
main
function and modifying its body with keyword arguments:To call the
main
function with a keyword argument:="apple") main(food
In the
main
function, to call theeat
function with keyword arguments:def main(food: str) -> None: = "apple" food_item =food) eat(food
For calling
have_done
and printing the result with keyword arguments:To call
have_done
with keyword arguments:="homework", completed=False) have_done(task
To print the result of this function call:
print(have_done(task="homework", completed=False))
Spot the error
Code Snippet 1:
Problem: The
total_fuel_cost
function does not return a value. It performs a calculation but does not include areturn
statement. As a result, it returnsNone
by default, which leads toprint(total_fuel_cost(...))
printingNone
which is not the intended use of line 8.Solution: Add a
return
statement in thetotal_fuel_cost
function:Code Snippet 2:
Problem: There is no issue with this code that stops execution. The
total_fuel_cost
function correctly prints the result.Solution: None needed; the code works as intended.
Code Snippet 3:
Problem: There is no issue with this code that stops execution. The
greet
function prints a message and then returns a string. Themain
function callsgreet
and prints its return value.Solution: None needed; the code works as intended.
Code Snippet 4:
Problem: The
print
statement after thereturn
statement in thegreet
function is unreachable code. Once thereturn
statement is executed, the function exits, and any code following it will not be executed. This does not stop the example usage however if the user wanted to print statement on line 3, this would not be outputted.Solution: Move the
print
statement before thereturn
statement:Code Snippet 5:
Problem: There is a syntax error due to incorrect indentation. The
greet
function definition is not properly aligned.Solution: Correct the indentation of the
greet
function definition:Code Snippet 6:
Problem: There is no issue with this code that stops the execution of calling
main
. Thegreet
function prints two messages, and thenmain
callsgreet
and prints its return value (which isNone
). It’s important to note that the intended use of the example usage is to call themain
function. There are no issues that stop this execution however there is an issue within the body of the main function which callsgreet
and tries to print its return value (which might not be intended). Since greet doesn’t return anything (implicitly returns None), the output will include None instead of the intended greeting message.Solution: None needed; the code works as intended.
main
is called without issues.