# Functions

Tips for navigating the slides:
• Press O or Escape for overview mode.
• Visit this link for a nice printable version
• Press the copy icon on the upper right of code blocks to copy the code

## What we'll discuss today...

• Announcements
• Zoom rules
• Values
• Expressions
• Functions

### The Rules of the Zoom

• Chat will always be enabled. Please hide it and/or disable notifications if distracting.
• Chat is for questions and comments about the current topic.
• If the chat goes too off-topic, we'll ask you to focus back in. π
• If you're uncomfortable asking a question in the public chat, you can DM a staff member or post anonymously in the Piazza thread.

### Community guidelines

Your goal should be to learn and help others learn.

Even if everyone here has programming experience, there is still a wide range of experience levels. All are welcome!

There are no "stupid" questions. Ask all your questions and welcome everyone else's questions.
ππ»ππΌππΏββοΈππ½ππΎββοΈππΎππΏππ½ββοΈππ»ββοΈππΌββοΈππΌββοΈππ½ββοΈππ»ββοΈππΎββοΈππΏββοΈππ»ββοΈ

## Expressions & Values

### What do programs do?

• Programs work by manipulating values
• Expressions in programs evaluate to values
• Expression: `'a' + 'hoy'`
• Value: `'ahoy'`
• The Python interpreter evaluates expressions and displays their values

### Values

Programs manipulate values.

Each value has a certain data type.

Data type Example values
Integers `2` `44` `-3`
Floats `3.14` `4.5` `-2.0`
Booleans `True` `False`
Strings `'Β‘hola!'` `'its python time!'`

Try in a Python interpreter, like on code.cs61a.org.

### Expressions (with operators)

An expression describes a computation and evaluates to a value.

Some expressions use operators:

``````
18 + 69
``````
``````
6/23
``````
``````
2 * 100
``````
``````
2 ** 100
``````

Try in a Python interpreter, like on code.cs61a.org.

### Call expressions

Many expressions use function calls:

``````
pow(2, 100)
``````
``````
max(50, 300)
``````
``````
min(-1, -300)
``````

### Expressions (both ways)

Expressions with operators can also be expressed with function call notation:

``````
2 ** 100
pow(2, 100)
``````
``````
from operator import add

18 + 69
``````

The `pow()` function is a built-in; it's provided in every Python environment. Other functions (`add()`, `div()`, etc) must be imported from the `operator` module in the Python standard library.

### Anatomy of a Call Expression

 add ( 18 , 69 ) Operator Operand Operand

How Python evaluates a call expression:

1. Evaluate the operator
2. Evaluate the operands
3. Apply the operator (a function) to the evaluated operands (arguments)

Operators and operands are also expressions, so they must be evaluated to discover their values.

### Evaluating nested expressions

This is called an expression tree.

### Exercise: Expressions

After the lecture, you can try out this exercise. (Not graded, just another way to engage with the material!)

## Names

### Names

A name can be bound to a value.

One way to bind a name is with an assignment statement:

 x = 7 Name Value

The value can be any expression:

 x = 1 + 2 * 3 - 4 // 5 Name Expression

### Using names

A name can be referenced multiple times:

``````
x = 10
y = 3

result1 = x * y
result2 = x + y
``````

A name that's bound to a data value is also known as a variable.

### Name rebinding

A name can only be bound to a single value.

``````
my_name = 'Pamela'

my_name = my_name + 'ela'
``````

π¬ Will that code error? If not, what will `my_name` store?
It will not error (similar code in other languages might, however). The name `my_name` is now bound to the value 'Pamelaela'.

### Exercise

Try this after the lecture...

What will be the value of the final expression in this sequence?

``````
f = min
f = max
g = min
h = max
max = g
max(f(2, g(h(1, 5), 3)), 4)
``````

## Environment diagrams

An environment diagram is a visualization of how Python interprets a program. Use the free website PythonTutor to generate diagrams. View example

Code (left) Frames (right)
Arrows indicate the order of execution. Green = just executed, red = up next. Each name is bound to a value.
Within a frame, each name cannot be repeated.

### Assignments in Environment diagrams

How Python interprets an assignment statement:

• Evaluate the expression to the right of `=`.
• Bind the expression's value to the name that's on the left side of the `=` sign.

## Functions

### What is a function?

A function is a sequence of code that performs a particular task and can be easily reused. β»οΈ

We've already used functions:

``````
mul(60, sub(5, 4))
``````

A function takes inputs (the arguments) and returns an output (the return value).

18, 69 β add β 87

### Defining functions

The most common way to define functions is Python is the `def` statement.

``````
def <name>(<parameters>):
return <return expression>
``````

Example:

``````
return num1 + num2
``````

Once defined, we can call it:

``````
``````

### Anatomy of a function definition

The first line is called the function signature, all lines after are considered the function body.

``````
def <name>(<parameters>):        # β Function signature
return <return expression>   # β Function body
``````
``````
def add(num1, num2):             # β Function signature
return num1 + num2           # β Function body
``````

The function body can have multiple lines:

``````
def add(num1, num2):             # β Function signature
sum = num1 + num2            # β Function body
return sum                   # β Function body
``````

### Function arguments

We can pass in any expressions as arguments.

``````
return num1 + num2
``````
``````
x = 1
y = 2
``````
``````
x = 3
add(x * x, x + x)
``````

Example with strings

### Return values

The return keyword returns a value to whoever calls the function (and exits the function).

``````
return num1 + num2

sum = add(2, 4)
``````

Reminder: You can use function calls in expressions:

``````
big_sum = add(200, 412) + add(312, 256)
``````

...and nest function calls inside function calls:

``````
``````

### Spot the bug #1

What's wrong with this code?

``````
return sum
sum = num1 + num2

sum = add(2, 4)
``````

The code after the return statement will not be executed, that line belongs before the return.

### Spot the bug #2

What's wrong with this code?

``````
return num1 + num2

sum = add(2, 4)
``````

The function body is referring to variables that don't seem to exist. Most likely, they should be parameters in the function signature.

### Spot the bug #3

What's wrong with this code?

``````
sum = num1 + num2

sum = add(2, 4)
``````

The function body does not return any value. However, the code that calls it tries to use the result of the expression. It should have a return statement that returns the sum.

### Functions in environment diagrams

How Python interprets a def statement:

• It creates a function with the `name` and `parameters`
• It sets the function body to everything indented after the first line
• It binds the function name to that function body (similar to an assignment statement)

### Function calls in environment diagrams

How Python interprets a function call:

• It creates a new frame in the environment
• It binds the function call's arguments to the parameters in that frame
• It executes the body of the function in the new frame

## More on names

### Names and environments

All Python code is evaluated in the context of an environment, which is a sequence of frames.

We've seen two possible environments:

 Global frame Function's local frame, child of Global frame

### Name lookup rules

How Python looks up names in a user-defined function:

1. Look it up in the local frame
2. If name isn't in local frame, look it up in the global frame
3. If name isn't in either frame, throw a NameError

*This is simplified since we haven't learned all the Python features that complicate the rules.

### Name lookup example #1

``````
def exclamify(text):
start_exclaim = "Β‘"
end_exclaim = "!"
return start_exclaim + text + end_exclaim

exclamify("the snails are eating my lupines")
``````
• On line 4, which frame is `start_exclaim` found in?
The local frame for exclamify
• On line 4, Which frame is `text` found in?
The local frame for exclamify
• On line 6, which frame is `exclamify` found in?
The global frame

### Name lookup example #2

``````
start_exclaim = "Β‘"
end_exclaim = "β£οΈ"

def exclamify(text):
return start_exclaim + text + end_exclaim

exclamify("the voles are digging such holes")
``````
• On line 5, which frame is `start_exclaim` found in?
The global frame
• On line 5, Which frame is `text` found in?
The local frame for exclamify
• On line 6, which frame is `exclamify` found in?
The global frame

### Name lookup example #3

``````
def exclamify(text):
end_exclaim = "βοΈοΈοΈ"
return start_exclaim + text + end_exclaim

exclamify("the voles are digging such holes")
``````
• Which name will cause a `NameError`?
The `start_exclaim` name, since it was never assigned.
• When will that error happen?
It will happen when `exclamify` is called and Python tries to execute the return statement.

### Summary

• Programs consist of statements, or instructions for the computer, containing expressions, which describe computation and evaluate to values.
• Values can be assigned to names to avoid repeating computations.
• An assignment statement assigns the value of an expression to a name in the current environment.
• Functions encapsulate a series of statements that maps arguments to a return value.
• A def statement creates a function object with certain parameters and a body and binds it to a name in the current environment.
• A call expression applies the value of its operator, a function, to the value(s) or its operand(s), some arguments.

### Exercises

You can try these exercises after the lecture for some additional practice:

To run the doctests, press the red test tube in the upper right corner.