Intro to Functions
1
CS 110: Intro to Computer
Programming with Python
Outline
2
Statements & Expressions
3
Examples of Expressions
3 * 7
1 + 2 + 3 * (8 ** 9) - sqrt(4.0)
max(3, 94)
my_var
"bar"
my_var + "bar"
None
True
�Whether constants, operations, accessing values in variables, or function calls, these expressions all evaluate to a value (i.e. a piece of data that can be stored and manipulated)
4
Examples of Statements
a = 3
def my_function():
while CONDITION:
if CONDITION:
for VARIABLE in SEQUENCE:
Statements perform actions but don’t evaluate to a value (i.e. you couldn’t store the results in a variable). We will talk more about statements next week (we haven’t gone over all of these yet).
5
Outline
6
Intro to Functions: Outline
7
1. Functions are similar to operators, but more flexible
Recall: when we examined arithmetic operators...
8
Example 1: Addition
9
4 + 6
Example 1: Addition
10
4 + 6
operand
operand
operator
Example 1: Addition
11
4 + 6
4
operand
6
operand
+
operator
Example 1: Addition
12
4 + 6
10
4
operand
6
operand
+
operator
Example 2: Multiplication
13
4 * 6
Example 2: Multiplication
14
4 * 6
operand
operand
operator
Example 2: Multiplication
15
4 * 6
4
operand
6
operand
*
operator
Example 2: Multiplication
16
4 * 6
24
4
operand
6
operand
*
operator
Example 3: Multiplication using function notation...
17
mul(4, 6)
Example 3: Multiplication using function notation...
18
mul(4, 6)
argument
argument
function name
Example 3: Multiplication using function notation...
19
mul(4, 6)
mul
6
operand
4
argument
argument
function name
Example 3: Multiplication using function notation...
20
mul(4, 6)
24
mul
6
argument
4
function name
argument
Return value
1. Functions are similar to operators, but more flexible
operator → function name
operands → arguments�(usually 2) (zero or more)
result → return value
2 * 2 → mul(2, 2)
21
2. Functions are “call expressions”
22
2. Functions are “Call Expressions”
A call expression invokes a function, providing zero or more inputs, and eventually “returns.” Some call expressions (hereafter “function calls”) return values, others return None (no value).
3 is an expression (a constant)
1 + 2 is also an expression that evaluates to 3
add(1, 2) is a call expression that returns 3 as a result
add(-3, 6) is also a call expression that returns 3 as a result
All of these expressions produce the same value after they are evaluated: 3
23
What are the rules of the add function?
24
What are some legal and illegal arguments?
add(2, 2.5)
add(??, ??)
add(??, ??)
add(??, ??)
add(??, ??)
25
26
mul(add(2, mul(4, 6)), add(3, 5))
Source: http://composingprograms.com/pages/12-elements-of-programming.html | Visualize: https://goo.gl/98wq7w
27
mul(add(2, mul(4, 6)), add(3, 5))
Source: http://composingprograms.com/pages/12-elements-of-programming.html | Visualize: https://goo.gl/98wq7w
function
argument
argument
28
add(3, 5)
add(2, mul(4, 6))
mul
mul(add(2, mul(4, 6)), add(3, 5))
Source: http://composingprograms.com/pages/12-elements-of-programming.html | Visualize: https://goo.gl/98wq7w
function
argument
argument
29
add(3, 5)
add(2, mul(4, 6))
mul
mul(add(2, mul(4, 6)), add(3, 5))
Source: http://composingprograms.com/pages/12-elements-of-programming.html | Visualize: https://goo.gl/98wq7w
function
argument
argument
function
argument
argument
function
argument
argument
30
mul(4, 6)
add(3, 5)
add
5
3
add(2, mul(4, 6))
add
2
mul
mul(add(2, mul(4, 6)), add(3, 5))
Source: http://composingprograms.com/pages/12-elements-of-programming.html | Visualize: https://goo.gl/98wq7w
function
argument
argument
function
argument
argument
function
argument
argument
31
mul(4, 6)
add(3, 5)
add
5
3
add(2, mul(4, 6))
add
2
mul
mul(add(2, mul(4, 6)), add(3, 5))
Source: http://composingprograms.com/pages/12-elements-of-programming.html | Visualize: https://goo.gl/98wq7w
function
argument
argument
function
argument
argument
function
argument
argument
function
argument
argument
32
mul(4, 6)
mul
6
4
add(3, 5)
add
5
3
add(2, mul(4, 6))
add
2
mul
mul(add(2, mul(4, 6)), add(3, 5))
Source: http://composingprograms.com/pages/12-elements-of-programming.html | Visualize: https://goo.gl/98wq7w
function
argument
argument
function
argument
argument
function
argument
argument
function
argument
argument
33
mul(4, 6)
mul
6
4
add(3, 5)
add
5
3
add(2, mul(4, 6))
24
add
2
mul
mul(add(2, mul(4, 6)), add(3, 5))
Source: http://composingprograms.com/pages/12-elements-of-programming.html | Visualize: https://goo.gl/98wq7w
function
argument
argument
function
argument
argument
function
argument
argument
function
argument
argument
34
mul(4, 6)
mul
6
4
add(3, 5)
8
add
5
3
add(2, mul(4, 6))
24
add
2
mul
mul(add(2, mul(4, 6)), add(3, 5))
26
Source: http://composingprograms.com/pages/12-elements-of-programming.html | Visualize: https://goo.gl/98wq7w
function
argument
argument
function
argument
argument
function
argument
argument
function
argument
argument
35
mul(4, 6)
mul
6
4
add(3, 5)
8
add
5
3
add(2, mul(4, 6))
24
add
2
mul
mul(add(2, mul(4, 6)), add(3, 5))
208
26
Source: http://composingprograms.com/pages/12-elements-of-programming.html | Visualize: https://goo.gl/98wq7w
function
argument
argument
function
argument
argument
function
argument
argument
function
argument
argument
3. Functions provide a way to encapsulate and reuse code
36
max()�???
1, 99, 123, 3
123
max()�???
1, 34, 66, 3
66
4. To run a function, you have to call or “invoke” it
In order to run a function, you have to invoke it and give it the data it needs, using the correct type:
>>> max # tells you that max is a function
>>> max(2) # error (it expected a sequence or > 1 arg)
>>> max(2, 55, 29, 88, 7)�88
# interpreter confused: what are you asking me to do?
>>> max('5555', 55, [1, 2, 3])
37
5. Function Inputs: Parameters and Arguments
38
Terminology: Parameters & Arguments
39
Positional Parameters
40
Positional Parameters: Example
Consider the built-in function pow(), which has 2 required positional parameters: (1) the base number, and (2) the exponent. Order matters:
>>> pow(3, 2)�9
>>> pow(2, 3) �8
You have to understand which number is which so that you can use the function correctly
41
Keyword Parameters
42
Keyword Parameters: Example
>>> name = 'Jackson'�>>> print('Hello', 'there', name, '!\n')�>>> print('Hello', 'there', name, end='!\n')�>>> print('Hello', 'there', name, end='!\n')�>>> print('Hello', 'there', name, sep='~~~~~~', end='!\n')�
43
Outline
44
Built-in, imported, and custom functions
45
Frequently used built-in functions
I/O
Understanding your data & functions
For each of the functions listed above...
46
Demo
47