passing arguments, local/global variables writing functions( ) (part 2)
Post on 18-Jan-2016
217 Views
Preview:
TRANSCRIPT
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
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
>>
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
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
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
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
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!
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.
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
def hello( ):
print (“hey there!”)
def goodbye( ):
print (“see you later!”)
print (“hi!”)
hello( )
print (“I’ll see you again”)
goodbye( )
Multiple Functions
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!
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
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!
• Functions are like “mini programs”
• You can create variables inside functions, as you would in your main source code
Local Variables
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
• 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
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
• 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
def Donald( ):
money = 3000 #local variable
print (“Donald has $”, money)
def Johnson( ):
money = 4000 #local variable
print (“Johnson has $”, money)
Local Variables
• 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
• 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
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
• 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
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
• When arguments are passed into a function, we are actually passing the “value” into the function, not the actual variable
Rules for Arguments
def change (num):
print(“original value:”, num)
num = 16
print(“new value: “, num)
num = 5
change(num)
print(“value:”, num)
Local Variables
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
• 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
• 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
• 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
• 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
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
• 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
name = “Donald”
def sayhello( ):
print (“Hey there,”, name)
print (“You are”, name)
sayhello( )
Global Variables
• 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
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
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
>>
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
>>
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
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
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
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
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
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
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
• 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
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
• 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
top related