Download presentation
Presentation is loading. Please wait.
1
CSE 231 Lab 4
2
Functions Global VS local variables
Topics to cover Functions Global VS local variables
3
Think a math function! F(x) = x^2 What is F(2) ? F(2) = 4
Functions Think a math function! F(x) = x^2 What is F(2) ? F(2) = 4 x = 2
4
Functions - Think a math function!
F(x) = x^2 Functions have a Domain and a Range What is a Domain? All possible input of a function. What is a Range? All possible output of a function.
5
Functions – Back to Python
What are functions? Why do we use functions? Examples of functions?
6
What are functions? Allow you to write code once and reuse it later
Make code more readable, easier to debug Some examples of functions we’ve used: print(), input() Computer Science is full of buzzword, When you think functions, you think Abstraction.
7
Functions – Back to Python
Functions in Python (and everywhere else in the world) have the option of: Accepting input. (Domain) Giving output. (Range)
8
Functions – What do they look like?
Functions have a definition, parameters, and some have return value(s)
9
What is the output? Examples Example 1: def print_hi(): print(“Hi!”)
def print_hi(name): print(“Hi”, name, “!”) print_hi(‘Bob’) Hi Bob! Example 3: def square(num): return num**2 Nothing Example 4: def print_hi(name): print(“Hi”, name, “!”) print_hi() Error
10
2.5 15 Examples def average(number1, number2):
return average avg = average(2,3) print(avg) 2.5 num1 = 10 num2 = 20 avg = average(num1,num2) print(avg) 15
11
Functions – Multiple returns
def get_double_and_triple(num): return num*2 num*3 double_two, triple_two = get_double_and_triple(2) print(“2 * 2 =”, double_two, ”| 2 * 3 =”, triple_two”) >>> SyntaxError: invalid syntax Why?
12
Functions – Multiple returns
def get_double_and_triple(num): return num*2, num*3 double_two, triple_two = get_double_and_triple(2) print(“2 * 2 =”, double_two, ”| 2 * 3 =”, triple_two”) >>> 2 * 2 = 4 | 2 * 3 = 6 IMPORTANT
13
Global VS Local Variables
14
Local def local_test(): local_variable = “Hi, I’m Local!” print(local_variable) What’s the issue here?
15
Local def local_test(): local_variable = “Hi, I’m Local!” print(local_variable) local_variable only accessible inside local_test()!
16
Global x = 10 y = 4 def print_globals(y): y = 3 print(x) #10
print(y) #3 print_globals(2) print(y) #4
17
Global x = 10 y = 20 def mess_with_globals(): x += 10 y += 10
print(x) #What is the output? print(y) #What is the output?
18
Global x = 10 y = 20 def mess_with_globals(): x += 10 y += 10
print(x) #10 print(y) #20 X and y are global because they were defined outside of a function Nothing happen because you did not call the function
19
Global x = 10 y = 20 def mess_with_globals(): x += 10 y += 10
print(x) print(y) X and y are global because they were defined outside of a function UnboundLocalError: local variable ‘x' referenced before assignment
20
Global x = 10 y = 20 def mess_with_globals(): global x global y
print(x) #20 print(y) #30 x and y are global because they were defined outside of a function All good. But this is not a good practice. If you want to change a variable, pass the variable to the function and return the result
21
All good Global x = 10 y = 20 def mess_with_globals(x,y): x += 10
return x,y x,y = mess_with_globals(x,y) print(x) #20 print(y) #30 All good
22
Local + some global local_variable = “I’m a liar” def local_test():
local_variable = “Hi, I’m Local!” print(local_variable) #I’m a liar local_variable is actually a global variable.
23
Some things to remember!
Function variables are independent of your main variables Do NOT use globally defined variables in functions Do NOT declare functions inside loops Coding standards, no. 2 If a function needs to access/modify a variable, pass the variable to the function and return the result
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.