Download presentation
Presentation is loading. Please wait.
Published byAlice Edwards Modified over 9 years ago
1
Passing Arguments, Local/Global Variables Writing Functions( ) (Part 2)
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
3
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 >>
4
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
5
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
6
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
7
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
8
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!
9
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.
10
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
11
def hello( ): print (“hey there!”) def goodbye( ): print (“see you later!”) print (“hi!”) hello( ) print (“I’ll see you again”) goodbye( ) Multiple Functions
12
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!
13
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
14
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!
15
Functions are like “mini programs” You can create variables inside functions, as you would in your main source code Local Variables
16
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
17
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
18
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
19
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
20
def Donald( ): money = 3000#local variable print (“Donald has $”, money) def Johnson( ): money = 4000#local variable print (“Johnson has $”, money) Local Variables
21
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
22
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
23
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
24
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
25
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
26
When arguments are passed into a function, we are actually passing the “value” into the function, not the actual variable Rules for Arguments
27
def change (num): print(“original value:”, num) num = 16 print(“new value: “, num) num = 5 change(num) print(“value:”, num) Local Variables
28
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
29
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
30
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
31
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
32
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
33
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
34
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
35
name = “Donald” def sayhello( ): print (“Hey there,”, name) print (“You are”, name) sayhello( ) Global Variables
36
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
37
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
38
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 >>
39
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 >>
40
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
41
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
42
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
43
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
44
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
45
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
46
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
47
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
48
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
49
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.