1 of 19

Functions & Procedures

cacticouncil.org

2 of 19

Agenda

  • Some background…

  • Syntax

  • Examples

  • Functions vs Procedures vs Methods vs Routines vs Subroutines vs Subprogram etc…

  • Values vs References

  • Mutability

3 of 19

Thank You, Mathematicians

  • Like many concepts in programming, functions come from the math world…

  • DomainRange written like this f:XY

  • A “machine” or “black box” that yields a corresponding output per input

    • “black box” = interface is all that matters

f(2) = 4

f(3) = 9

f(10) = 100

4 of 19

From Math to Code

  • What we kept:

    • Parentheses
    • Concept of inputs and outputs (I/O)
    • Identifiers (naming functions)
    • Function call as a replacement for its output (and function chaining)

  • What we changed / expanded upon:

    • Lots of inputs possible (65k in most languages)
    • Multiple outputs possible in some languages
    • Multiple I/O possibilities (0+ inputs, 0+ outputs)
    • Mulitple ways to pass I/O around in memory
    • Ability to change state beyond scope of function itself!! (procedure)

5 of 19

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…”

6 of 19

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)

7 of 19

Function Definition Syntax (Python)

def power(x, e):

result = 1

for i in range(0, e):

result *= x

return result

8 of 19

Activity

Write the function prototypes for the following functions.�Think about the number of inputs and outputs (and what types they could be).�

  1. A function to compute the area of a triangle

  • A function to normalize a number between a range

9 of 19

Terminology

  • A canonical function does not change the state of the program beyond the function

    • It only operates on the inputs provided
    • It is guaranteed to be self-contained
    • Does not alter the inputs beyond the scope of the function
    • Does not alter ANYTHING beyond the scope of the function

  • A procedure provides no such guarantees…

    • Printing to the screen is actually a procedure. Why?

  • A method is simply a function (or procedure) inside a containing structure (class, etc.)�It is a term that came with Object Oriented Programming.

  • Subprogram, routine, and subroutine are the original programming terms

10 of 19

Values vs References

Joe’s reminder:

Talk about values vs references in general, brah.

11 of 19

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

12 of 19

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’]

13 of 19

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

14 of 19

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

15 of 19

Mutability

  • Immutablecannot be changed after creation

    • More efficient to store and access in memory
    • Primitive (smaller) object types are usually immutable
    • Pass by value

  • Mutablecan be changed after creation

    • More maintenance under the hood
    • Objects that can get large are mutable
    • Pass by reference

Megha Mohan [4]

16 of 19

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.

17 of 19

Activity

Write a function to compute the distance formula.

18 of 19

Activity

19 of 19

References

  1. Creative Commons Share Alike 3.0 License (CC BY-SA 3.0)�https://creativecommons.org/licenses/by-sa/3.0/legalcode�* scaling may have been applied to images to fit onscreen.