passing arguments, local/global variables writing functions( ) (part 2)

49
Passing Arguments, Local/Global Variables Writing Functions( ) (Part 2)

Upload: alice-edwards

Post on 18-Jan-2016

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Passing Arguments, Local/Global Variables Writing Functions( ) (Part 2)

Passing Arguments, Local/Global Variables

Writing Functions( ) (Part 2)

Page 2: Passing Arguments, Local/Global Variables Writing Functions( ) (Part 2)

def hello( ):

print (“Hi, there!”)

print (“I’m a function.”)

print (“Good morning”)

print (“Welcome to class”)

hello( )

print (“And, now we’re done”)

Flow of Execution

Page 3: Passing Arguments, Local/Global Variables Writing Functions( ) (Part 2)

def hello( ):

print (“Hi, there!”)

print (“I’m a function.”)

print (“Good morning”)

print (“Welcome to class”)

hello( )

print (“And, now we’re done”)

Flow of Execution

>>

Page 4: Passing Arguments, Local/Global Variables Writing Functions( ) (Part 2)

def hello( ):

print (“Hi, there!”)

print (“I’m a function.”)

print (“Good morning”)

print (“Welcome to class”)

hello( )

print (“And, now we’re done”)

Flow of Execution

>> Good morning

Page 5: Passing Arguments, Local/Global Variables Writing Functions( ) (Part 2)

def hello( ):

print (“Hi, there!”)

print (“I’m a function.”)

print (“Good morning”)

print (“Welcome to class”)

hello( )

print (“And, now we’re done”)

Flow of Execution

>> Good morning

Welcome to class

Page 6: Passing Arguments, Local/Global Variables Writing Functions( ) (Part 2)

def hello( ):

print (“Hi, there!”)

print (“I’m a function.”)

print (“Good morning”)

print (“Welcome to class”)

hello( )

print (“And, now we’re done”)

Flow of Execution

>> Good morning

Welcome to class

Page 7: Passing Arguments, Local/Global Variables Writing Functions( ) (Part 2)

def hello( ):

print (“Hi, there!”)

print (“I’m a function.”)

print (“Good morning”)

print (“Welcome to class”)

hello( )

print (“And, now we’re done”)

Flow of Execution

>> Good morning

Welcome to class

Page 8: Passing Arguments, Local/Global Variables Writing Functions( ) (Part 2)

def hello( ):

print (“Hi, there!”)

print (“I’m a function.”)

print (“Good morning”)

print (“Welcome to class”)

hello( )

print (“And, now we’re done”)

Flow of Execution

>> Good morning

Welcome to class

Hi, there!

Page 9: Passing Arguments, Local/Global Variables Writing Functions( ) (Part 2)

def hello( ):

print (“Hi, there!”)

print (“I’m a function.”)

print (“Good morning”)

print (“Welcome to class”)

hello( )

print (“And, now we’re done”)

Flow of Execution

>> Good morning

Welcome to class

Hi, there!

I’m a function.

Page 10: Passing Arguments, Local/Global Variables Writing Functions( ) (Part 2)

def hello( ):

print (“Hi, there!”)

print (“I’m a function.”)

print (“Good morning”)

print (“Welcome to class”)

hello( )

print (“And, now we’re done”)

Flow of Execution

>> Good morning

Welcome to class

Hi, there!

I’m a function.

And, now we’re done

Page 11: Passing Arguments, Local/Global Variables Writing Functions( ) (Part 2)

def hello( ):

print (“hey there!”)

def goodbye( ):

print (“see you later!”)

print (“hi!”)

hello( )

print (“I’ll see you again”)

goodbye( )

Multiple Functions

Page 12: Passing Arguments, Local/Global Variables Writing Functions( ) (Part 2)

def hello( ):

print (“hey there!”)

def goodbye( ):

print (“see you later!”)

print (“hi!”)

hello( )

print (“I’ll see you again”)

goodbye( )

Multiple Functions

>> hi!

hey there!

I’ll see you again

see you later!

Page 13: Passing Arguments, Local/Global Variables Writing Functions( ) (Part 2)

def main( ):

print (“I have a message!”)

message( )

print (“Goodbye!”)

def message( ):

print (“The answer is C!”)

print (“Here we go” )

main( )

Functions within a Function

Page 14: Passing Arguments, Local/Global Variables Writing Functions( ) (Part 2)

def main( ):

print (“I have a message!”)

message( )

print (“Goodbye!”)

def message( ):

print (“The answer is C!”)

print (“Here we go” )

main( )

Functions within a Function

>> Here we go

I have a message!

The answer is C!

Goodbye!

Page 15: Passing Arguments, Local/Global Variables Writing Functions( ) (Part 2)

• Functions are like “mini programs”

• You can create variables inside functions, as you would in your main source code

Local Variables

Page 16: Passing Arguments, Local/Global Variables Writing Functions( ) (Part 2)

def rectangle( ):

x = float(input(“length?: ”)) #local variable

y = float(input(“width?: ”)) #local variable

area = x * y #local variable

print (“Area: ”, area)

rectangle( )

Local Variables

Page 17: Passing Arguments, Local/Global Variables Writing Functions( ) (Part 2)

• However, variables that are defined inside of a function, they are considered “local” to that function

• Therefore, these local variables cannot be accessed from outside the function’s “scope”, because they simple do not exist to the main program

Local Variables

Page 18: Passing Arguments, Local/Global Variables Writing Functions( ) (Part 2)

def rectangle( ):

x = float(input(“length?: ”)) #local variable

y = float(input(“width?: ”)) #local variable

area = x * y #local variable

print (“Area: ”, area)

print( x * y ) # error, x and y are not defined

Local Variables

Page 19: Passing Arguments, Local/Global Variables Writing Functions( ) (Part 2)

• Because local variables don’t “exist” outside the scope of the function, different functions can use the same name for variables defined in their function

• These local variables, although carrying the same name, do not overwrite one another

Local Variables

Page 20: Passing Arguments, Local/Global Variables Writing Functions( ) (Part 2)

def Donald( ):

money = 3000 #local variable

print (“Donald has $”, money)

def Johnson( ):

money = 4000 #local variable

print (“Johnson has $”, money)

Local Variables

Page 21: Passing Arguments, Local/Global Variables Writing Functions( ) (Part 2)

• Functions can also take arguments, some more than others, as we’ve seen already

print(“hello”, “goodbye”, 75) # 3 arguments

format(“hello”, “<20s”) # 2 arguments

input(“length?: ”) # 1 argument

• Arguments are separated by commas

Passing Arguments to a Function

Page 22: Passing Arguments, Local/Global Variables Writing Functions( ) (Part 2)

• We can define functions that pass an argument

• We need to specify what exactly we are going to pass into the function, and what to do with it. We can do this by the use of variables.

Passing Arguments to a Function

Page 23: Passing Arguments, Local/Global Variables Writing Functions( ) (Part 2)

def square(side): # whatever is passed into

print(side ** 2) # the square( ) function

# will assume the value

# of the variable “side”

square(5) # variable “side” is assuming value 5

>> 25

Local Variables

Page 24: Passing Arguments, Local/Global Variables Writing Functions( ) (Part 2)

• You can define a function that passes multiple arguments

• Each argument is separated by a comma

• Each argument is assigned to the variable in the position in which it is defined in the function

Passing Multiple Arguments

Page 25: Passing Arguments, Local/Global Variables Writing Functions( ) (Part 2)

def average (test1, test2, test3):

sum = test1 + test2 + test3

avg = sum / 3

print(avg) #test1 assumes 95

#test2 assumes 98

average(95, 98, 88) #test3 assumes 88

>> 93.6666666667

Local Variables

Page 26: Passing Arguments, Local/Global Variables Writing Functions( ) (Part 2)

• When arguments are passed into a function, we are actually passing the “value” into the function, not the actual variable

Rules for Arguments

Page 27: Passing Arguments, Local/Global Variables Writing Functions( ) (Part 2)

def change (num):

print(“original value:”, num)

num = 16

print(“new value: “, num)

num = 5

change(num)

print(“value:”, num)

Local Variables

Page 28: Passing Arguments, Local/Global Variables Writing Functions( ) (Part 2)

def change (num):

print(“original value:”, num)

num = 16

print(“new value: “, num)

num = 5 >> original value: 5

change(num) new value: 16

print(“value:”, num) value: 5

Local Variables

Page 29: Passing Arguments, Local/Global Variables Writing Functions( ) (Part 2)

• We call this a “passing by value”

• It creates a one-way communication with the function, meaning we can send data into a function but the function cannot change the argument or communicate back to the main program

• (We will learn how to create a two-way communication street later)

Rules for Arguments

Page 30: Passing Arguments, Local/Global Variables Writing Functions( ) (Part 2)

• As we discussed, most functions pass arguments by position

def function (a, b, c):

print(a, b, c)

function(1, 2, 3)

# variable a holds 1

# variable b holds 2

# variable c holds 3

Keyword Arguments

Page 31: Passing Arguments, Local/Global Variables Writing Functions( ) (Part 2)

• However, we can also pass arguments by assigning them keywords, or their variable names as defined in the function

def function (a, b, c):

print(a, b, c)

function(b = 1, a = 2, c = 3)

Keyword Arguments

Page 32: Passing Arguments, Local/Global Variables Writing Functions( ) (Part 2)

• You can also pass arguments both by position and with keywords

• However, you need to pass positional arguments first, then by the keywords

Keyword Arguments

Page 33: Passing Arguments, Local/Global Variables Writing Functions( ) (Part 2)

def function (a, b, c):

print(a, b, c)

function( 5, c = 2, b = 3)

# here, a holds 5

# b holds 3

# and c holds 2

Keyword Arguments

Page 34: Passing Arguments, Local/Global Variables Writing Functions( ) (Part 2)

• We said that variables defined inside a function are called “local variables”

• When variables are created outside of a function, they are called “global variables”

• We’ve created these global variables all along

• Global variables can be accessed anywhere in the code, even from inside a function

Global Variables

Page 35: Passing Arguments, Local/Global Variables Writing Functions( ) (Part 2)

name = “Donald”

def sayhello( ):

print (“Hey there,”, name)

print (“You are”, name)

sayhello( )

Global Variables

Page 36: Passing Arguments, Local/Global Variables Writing Functions( ) (Part 2)

• You can change global variables inside of a function

• But you must first tell Python you want to do so by adding the keyword “global” before defining the variable inside your function

Global Variables

Page 37: Passing Arguments, Local/Global Variables Writing Functions( ) (Part 2)

name = “Donald”

def sayhello( ):

global name

print (“Hey there,”, name)

name = “Bryan”

print (“Hey there,”, name)

print (“You are”, name)

sayhello( )

print (“You are”, name)

Global Variables

Page 38: Passing Arguments, Local/Global Variables Writing Functions( ) (Part 2)

name = “Donald”

def sayhello( ):

global name

print (“Hey there,”, name)

name = “Bryan”

print (“Hey there,”, name)

print (“You are”, name)

sayhello( )

print (“You are”, name)

Global Variables

>>

Page 39: Passing Arguments, Local/Global Variables Writing Functions( ) (Part 2)

name = “Donald”

def sayhello( ):

global name

print (“Hey there,”, name)

name = “Bryan”

print (“Hey there,”, name)

print (“You are”, name)

sayhello( )

print (“You are”, name)

Global Variables

>>

Page 40: Passing Arguments, Local/Global Variables Writing Functions( ) (Part 2)

name = “Donald”

def sayhello( ):

global name

print (“Hey there,”, name)

name = “Bryan”

print (“Hey there,”, name)

print (“You are”, name)

sayhello( )

print (“You are”, name)

Global Variables

>> You are Donald

Page 41: Passing Arguments, Local/Global Variables Writing Functions( ) (Part 2)

name = “Donald”

def sayhello( ):

global name

print (“Hey there,”, name)

name = “Bryan”

print (“Hey there,”, name)

print (“You are”, name)

sayhello( )

print (“You are”, name)

Global Variables

>> You are Donald

Page 42: Passing Arguments, Local/Global Variables Writing Functions( ) (Part 2)

name = “Donald”

def sayhello( ):

global name

print (“Hey there,”, name)

name = “Bryan”

print (“Hey there,”, name)

print (“You are”, name)

sayhello( )

print (“You are”, name)

Global Variables

>> You are Donald

Page 43: Passing Arguments, Local/Global Variables Writing Functions( ) (Part 2)

name = “Donald”

def sayhello( ):

global name

print (“Hey there,”, name)

name = “Bryan”

print (“Hey there,”, name)

print (“You are”, name)

sayhello( )

print (“You are”, name)

Global Variables

>> You are Donald

Hey there, Donald

Page 44: Passing Arguments, Local/Global Variables Writing Functions( ) (Part 2)

name = “Donald”

def sayhello( ):

global name

print (“Hey there,”, name)

name = “Bryan”

print (“Hey there,”, name)

print (“You are”, name)

sayhello( )

print (“You are”, name)

Global Variables

>> You are Donald

Hey there, Donald

Page 45: Passing Arguments, Local/Global Variables Writing Functions( ) (Part 2)

name = “Donald”

def sayhello( ):

global name

print (“Hey there,”, name)

name = “Bryan”

print (“Hey there,”, name)

print (“You are”, name)

sayhello( )

print (“You are”, name)

Global Variables

>> You are Donald

Hey there, Donald

Hey there, Bryan

Page 46: Passing Arguments, Local/Global Variables Writing Functions( ) (Part 2)

name = “Donald”

def sayhello( ):

global name

print (“Hey there,”, name)

name = “Bryan”

print (“Hey there,”, name)

print (“You are”, name)

sayhello( )

print (“You are”, name)

Global Variables

>> You are Donald

Hey there, Donald

Hey there, Bryan

You are Bryan

Page 47: Passing Arguments, Local/Global Variables Writing Functions( ) (Part 2)

• You can also pass arguments that are inputted by the user into a function

def sayhello(name):

print( “Hello”, name )

sayhello( input (“What’s your name: ”) )

Global Variables

Page 48: Passing Arguments, Local/Global Variables Writing Functions( ) (Part 2)

def square(x):

print(“The square of”, x, “is”, x**2)

square( int ( input (“Give me a number: ”) ) )

>> Give me a number: 8

The square of 8 is 64

Global Variables

Page 49: Passing Arguments, Local/Global Variables Writing Functions( ) (Part 2)

• Write a program that defines three functions. One that finds the circumference of a circle, the area of a circle and the volume of a sphere. Then a main program that passes a single variable, which is the radius, into three subsequent functions.

• Set a global variable for pi and use it in your functions.

Practice - Rectangles