Functions & Procedures
cacticouncil.org
Agenda
Thank You, Mathematicians
f(2) = 4
f(3) = 9
f(10) = 100
From Math to Code
Also… A Good Way To Clean Up Code
DEF Main():
LabelA:
if …
goto LabelC
else if …
goto LabelB
LabelD:
if …
goto LabelE
LabelC:
LabelE:
if …
goto LabelA
else if …
goto LabelD
LabelB:
spaghetti code
I hate my job ☹
Alex E. Proimos, CC BY 2.0 [1]
Wikipedia, CC BY 3.0 [1]
And code reuse:
“If you ever write the same code more than once, it’s time to write a function…”
Function Definition Syntax (C languages)
static int Power(int x, int e)
{
int result = 1;
for (int i = 0; i < e; i++)
result *= x;
return result;
}
output,
return type
name of function
parameters,
arguments,
inputs
body
header,
prototype,
signature
decorator(s)
Function Definition Syntax (Python)
def power(x, e):
result = 1
for i in range(0, e):
result *= x
return result
Activity
Write the function prototypes for the following functions.�Think about the number of inputs and outputs (and what types they could be).�
Terminology
Values vs References
Joe’s reminder:
Talk about values vs references in general, brah.
Value vs Reference Semantics
“Suppose you are interested in a list of countries in the world. That information is available in both printed form and on the web. The information would be the same, but the two media types behave in different ways. Printed pages have value semantics and web sites have reference semantics. In other words, printed pages act like value types and web sites act like reference types.”
p1
p2
p3
Think of printed page vars as values
p1 = ‘some info’�
Think of web page vars as references
p1
URL
p2
URL
p3
URL
An example by Jon Skeet [3]
p2 = p1
p3 = p1
p1 = ‘some info’�
p2 = p1
p3 = p1
In Python, It’s Really All References
addresses | contents |
38001232 | |
38001236 | |
38001240 | |
38001244 | |
38001248 | |
38001252 | |
38001256 | |
38001260 | |
38001264 |
id:1047
id:1048
Memory
id:984
id:1000
id:1050
age
awesome
name
location
hobbies
name = ‘Joe Del Rocco’
Joe Del Rocco
40
True
29.021375
-81.303132
reading
games
biking
drums
age = 40
location = (29.021375, -81.303132)
awesome = True
hobbies = [‘reading’, ‘games’, ‘biking’, ‘drums’]
Pass by Value
def power(x, e):
result = 1
for i in range(0, e):
result *= x
return result
base = 5
exp = 2
answer = 0
answer = power(base, exp)
5
2
addresses | contents |
38001232 | |
38001236 | |
38001240 | |
38001244 | |
38001248 | |
38001252 | |
38001256 | |
38001260 | |
38001264 |
5
2
0
Memory
5
2
25
25
exp
answer
base
x
e
Pass by Reference
def sort_list(items):
# sort items in place
nums = [89, 7, 13, 42, 69, 3, 99]
sort_list(nums)
id:2997
addresses | contents |
38001232 | |
38001236 | |
38001240 | |
38001244 | |
38001248 | |
38001252 | |
38001256 | |
38001260 | |
38001264 |
id:2997
Memory
id:2997
89
7
13
42
69
3
99
items[0] = -1
nums
3
7
13
42
69
89
99
items
-1
Mutability
Megha Mohan [4]
Activity
Write a function to reverse a string.
Because strings are immutable in Python,�you cannot (easily) implement this as a procedure.
But you can convert the string to a list, pass and operate on the list in place,
then merge the list back into a string after your function call.
Activity
Write a function to compute the distance formula.
Activity
References