= float(3) x
Week 5: Returns, Arguments, Logic
Reading: Think Python Chapter 6
Notes
Review - Function return
We have seen built-in functions that return a value after evaluation, such as float
:
print(x)
3.0
We have also seen how print
does not return anything (None
):
= print("this gets printed") y
this gets printed
print(y)
None
We have also written our own functions, so far without returning anything:
Calling the function results in the print statement running (and printed output). However, nothing is returned.
Defining return
Values
We can write functions so that they do return something. This is one of the most powerful concepts in computer programming.
To make your function return a value, use the keyword return
at the end of the function:
When execution inside a function reaches a return
statement:
- Execution inside that function ends
- The
return
value is brought back to the function call- The value “replaces” the function call
- This is similar to evaluation of an expression
Note that nothing is printed by the function itself!
- The function call on line 5 results in a value being returned and assigned to
d
- There is only printed output from the explicit call to
print
on line 6
A function can have multiple return statements, but remember that the function ends the first time any return statement is reached.
An example. This function returns True
if the input is an integer with multiple digits, otherwise it returns False
.
multiple_return.py
Note that this program has no printed output, because there are no calls to print
.
Multiple Arguments
A function can have any number of arguments. We’ve defined functions with no arguments and one argument. Multiple arguments are similar:
multiple_args.py
Arguments are passed into the function in the same position as the function call. Since 3
is the first argument, it becomes a
when the function is called; likewise 4
becomes b
.
Remember: if you define a function as needing a certain number of arguments, it must be called with that number of arguments. Otherwise, you’ll get an error.
Combining Conditionals
Conditional expressions can be combined and modified with three additional logic operators: and
, or
, and not
.
- The
and
operator will evaluate toTrue
if both sides of theand
areTrue
.
A |
B |
A and B |
---|---|---|
True |
True |
True |
True |
False |
False |
False |
True |
False |
False |
False |
False |
- The
or
operator will evaluate toTrue
if either (or both) sides of theor
areTrue
.
A |
B |
A or B |
---|---|---|
True |
True |
True |
True |
False |
True |
False |
True |
True |
False |
False |
False |
- The
not
operator reverses any boolean coming after it.
We can look at examples of these expressions using the interpreter:
4 < 5) and (2 < 0) (
False
= 2 x
== 2) or (x == 3) (x
True
not (3 < 4)
False
These evaluate to bools - which means they can be used with conditionals!
Practice
Practice Problem 5.1
Practice Problem 5.2
Practice Problem 5.3
Practice Problem 5.4
Practice Problem 5.5
Homework
Homework problems should always be your individual work. Please review the collaboration policy and ask the course staff if you have questions.
Double check your file names, printed output, and return values. These need to be exact matches for you to get credit.
Going forward, you will need to write functions that return instead of print. Confusing these two is a common error - take care to avoid it!