Top Banner
Functions Jay Summet
29

Functions Jay Summet. Aug 29 2007 2 Functions A function is a piece of code you can use over and over again Treat it like a black box You pass it (optional)

Dec 17, 2015

Download

Documents

Amos Perry
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Functions Jay Summet. Aug 29 2007 2 Functions A function is a piece of code you can use over and over again Treat it like a black box You pass it (optional)

Functions

Jay Summet

Page 2: Functions Jay Summet. Aug 29 2007 2 Functions A function is a piece of code you can use over and over again Treat it like a black box You pass it (optional)

Aug 29 2007 2

Functions

A function is a piece of code you can use over and over again

Treat it like a black box

You pass it (optional) values, it does some work, and it (optionally) returns valuesYou “call it”,”invoke it”, or “use it” by

using its name and parenthesesThe things you pass it go inside the parenthesesoutput = function( input )

function

input

output

Page 3: Functions Jay Summet. Aug 29 2007 2 Functions A function is a piece of code you can use over and over again Treat it like a black box You pass it (optional)

Aug 29 2007 3

Using Simple Functions

print( “Some Text” )

You call a function by using it's identifier (name) followed by parenthesis. If the function takes parameters, you

insert them inside the parenthesis.

Page 4: Functions Jay Summet. Aug 29 2007 2 Functions A function is a piece of code you can use over and over again Treat it like a black box You pass it (optional)

Aug 29 2007 4

Writing Simple Functions

def sayHi():print(“Hi there!”)print(“Nice to meet you!”)

sayHi()

Indent

Defining functionsCreates functionDoes not execute/run them

Indenting indicates a “block” of codeCall functions from top-level or other functions

No Indention“Top Level”

Page 5: Functions Jay Summet. Aug 29 2007 2 Functions A function is a piece of code you can use over and over again Treat it like a black box You pass it (optional)

Aug 29 2007 5

Format of a function definition

def function-name():statementstatement…statement

Page 6: Functions Jay Summet. Aug 29 2007 2 Functions A function is a piece of code you can use over and over again Treat it like a black box You pass it (optional)

Aug 29 2007 6

Writing Functions with Parameters

def sayHi(name):print( “Hi there”, name)print( “Nice to meet you!”)

sayHi(“Jay”)sayHi(“Bob”)

Page 7: Functions Jay Summet. Aug 29 2007 2 Functions A function is a piece of code you can use over and over again Treat it like a black box You pass it (optional)

Aug 29 2007 7

Parameters are Variables

• When you pass values into functions as parameters, they get assigned to the variable names declared in the definition line of the function.

• For example, when you call sayHi(“Jay”)The name variable is assigned (points to) the value “Jay”

• When the code in the function refers to the name variable, it evaluates to the string “Jay”

• So, when you call sayHi(“Jay”) and the sayHi function calls print(“Hi there”, name), it's the same as if it called print(“Hi there”, “Jay”)

Page 8: Functions Jay Summet. Aug 29 2007 2 Functions A function is a piece of code you can use over and over again Treat it like a black box You pass it (optional)

Aug 29 2007 8

Format of a Function Definition with Parameters

def function-name(param0, param1, ...):

statementstatement

…statement

function-name(param0, param1, ...)

Page 9: Functions Jay Summet. Aug 29 2007 2 Functions A function is a piece of code you can use over and over again Treat it like a black box You pass it (optional)

Aug 29 2007 9

Using Functions that Return Values

name = getName()print( “Hello”, name)

Page 10: Functions Jay Summet. Aug 29 2007 2 Functions A function is a piece of code you can use over and over again Treat it like a black box You pass it (optional)

Aug 29 2007 10

Composing Functions

•You can use the output (return value) of one function as the input (parameter) to another function.

sayHi( getName() )

•In this example, the getName() function executes first (things inside parenthesis execute before things outside parenthesis)•The getName() function returns a name, which is then given to the sayHi() function as a parameter.

Page 11: Functions Jay Summet. Aug 29 2007 2 Functions A function is a piece of code you can use over and over again Treat it like a black box You pass it (optional)

Aug 29 2007 11

Writing Functions that Return Values

def area(radius):return 3.14 * radius**2

def circumference(diameter):return 3.14 * diameter

print( “Area of a 3 ft circle”, area(3) )

print( “Circumference”, circumference(2*3) )

Page 12: Functions Jay Summet. Aug 29 2007 2 Functions A function is a piece of code you can use over and over again Treat it like a black box You pass it (optional)

Aug 29 2007 12

Return Statements

• The return statement is used to return a value from a function

• The return statement also affects the flow of execution• Whenever the flow of execution hits a return statement it

jumps back to the place where the function was called• All functions have an implicit return statement at the end

of the block of indented code, even if you do not specifically place one at the end of your function

Page 13: Functions Jay Summet. Aug 29 2007 2 Functions A function is a piece of code you can use over and over again Treat it like a black box You pass it (optional)

Aug 29 2007 13

Functions with Local Variables

def area(radius):a = 3.14 * radius**2return a

def circumference(diameter):c = 3.14 * diameterreturn c

print( “Area of a 3 ft circle”, area(3) )

print( “Circumference”, circumference(2*3) )

Page 14: Functions Jay Summet. Aug 29 2007 2 Functions A function is a piece of code you can use over and over again Treat it like a black box You pass it (optional)

Aug 29 2007 14

Variables in a Function are Local

• Variables in a function are private– Including the parameters

• Each function has its own variables– Even when the names are the same

• Allows you to write functions independently without worrying about using the same name

Page 15: Functions Jay Summet. Aug 29 2007 2 Functions A function is a piece of code you can use over and over again Treat it like a black box You pass it (optional)

Aug 29 2007 15

Different Variables - Same Name

def area(radius):a = 3.14 * radius**2return a

def circumference(radius):a = 3.14 * 2 * radiusreturn a

a = 20print( “Area of a 3 ft circle”, area(3) )print( “Circumference”, circumference(3) )print( a )

Page 16: Functions Jay Summet. Aug 29 2007 2 Functions A function is a piece of code you can use over and over again Treat it like a black box You pass it (optional)

Aug 29 2007 16

Writing Functions with Return Values

def function-name(list-of-params):statementstatement

…statementreturn value

output = function-name(list-of-params)

Page 17: Functions Jay Summet. Aug 29 2007 2 Functions A function is a piece of code you can use over and over again Treat it like a black box You pass it (optional)

Aug 29 2007 17

Passing variables to a functions

• If you pass a variable to a function, the function gets the value that the variable is pointing at

userInput = input(“Enter your Name”)sayHi(userInput)

Page 18: Functions Jay Summet. Aug 29 2007 2 Functions A function is a piece of code you can use over and over again Treat it like a black box You pass it (optional)

Aug 29 2007 18

Functions in general

# description of this function# what it expects as input# what is provides as outputdef function (p0, p2, …, pn):

statement … statement return value

z = function(a0, a2, …, an)

Page 19: Functions Jay Summet. Aug 29 2007 2 Functions A function is a piece of code you can use over and over again Treat it like a black box You pass it (optional)

Aug 29 2007 19

Where’s the Error?

def area: a = 3.14 * radius**2 return a

print( “Area of a 3 ft circle”, area( 3 )

Page 20: Functions Jay Summet. Aug 29 2007 2 Functions A function is a piece of code you can use over and over again Treat it like a black box You pass it (optional)

Aug 29 2007 20

Not enough Parentheses!

def area: a = 3.14 * radius**2 return a

print( “Area of a 3 ft circle”, area(3) )

Page 21: Functions Jay Summet. Aug 29 2007 2 Functions A function is a piece of code you can use over and over again Treat it like a black box You pass it (optional)

Aug 29 2007 21

Where’s the Error?

def area( radius ):a = 3.14 * radius**2return a

print( “Area of a 3 ft circle”, area(3) )

Page 22: Functions Jay Summet. Aug 29 2007 2 Functions A function is a piece of code you can use over and over again Treat it like a black box You pass it (optional)

Aug 29 2007 22

No Indentation!

def area(radius): a = 3.14 * radius**2 return a

print( “Area of a 3 ft circle”, area(3) )

Page 23: Functions Jay Summet. Aug 29 2007 2 Functions A function is a piece of code you can use over and over again Treat it like a black box You pass it (optional)

Aug 29 2007 23

Where’s the Error?

def area(radius): a = 3.14 * radius**2 return a

print( “Area of a 3 ft circle”, area() )

Page 24: Functions Jay Summet. Aug 29 2007 2 Functions A function is a piece of code you can use over and over again Treat it like a black box You pass it (optional)

Aug 29 2007 24

No argument to the function!

def area(radius): a = 3.14 * radius**2 return a

print( “Area of a 3 ft circle”, area(3) )

Page 25: Functions Jay Summet. Aug 29 2007 2 Functions A function is a piece of code you can use over and over again Treat it like a black box You pass it (optional)

Aug 29 2007 25

Where's the Error?

def area(radius): a = 3.14 * radius**2

print( “Area of a 3 ft circle”, area(3) )

Page 26: Functions Jay Summet. Aug 29 2007 2 Functions A function is a piece of code you can use over and over again Treat it like a black box You pass it (optional)

Aug 29 2007 26

No Return Value!

def area(radius): a = 3.14 * radius**2

print( “Area of a 3 ft circle”, area(3) )

Page 27: Functions Jay Summet. Aug 29 2007 2 Functions A function is a piece of code you can use over and over again Treat it like a black box You pass it (optional)

Aug 29 2007 27

Where’s the Error?

def area(radius): a = 3.14 * radius**2 return a

area(3)print( “Area of a 3 ft circle”, a )

Page 28: Functions Jay Summet. Aug 29 2007 2 Functions A function is a piece of code you can use over and over again Treat it like a black box You pass it (optional)

Aug 29 2007 28

Don't save the return value into a!

def area(radius): a = 3.14 * radius**2 return a

area(3)print( “Area of a 3 ft circle”, a )

Page 29: Functions Jay Summet. Aug 29 2007 2 Functions A function is a piece of code you can use over and over again Treat it like a black box You pass it (optional)

Aug 29 2007 29

What’s the result?

def area(radius): a = 3.14 * radius**2 return a

v = area(3)a = 16print( “Area of a 3 ft circle”, v )print( “value of a”, a)