CSE 231 LAB 4

21 Slides3.62 MB

CSE 231 LAB 4

TOPICS TO COVER 1. FUNCTIONS 2. GLOBAL VS LOCAL VARIABLES

FUNCTIONS Think a math function! F(x) x2 What is F(2) ? F(2) 4 x 2

FUNCTIONS - THINK A MATH FUNCTION! F(x) x2 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.

FUNCTIONS – BACK TO PYTHON What are functions? Why do we use functions? Examples of functions?

WHY 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()

FUNCTIONS – BACK TO PYTHON Functions in Python (and everywhere else in the world) can: a) b) Accept input. (Domain) Provide output. (Range)

FUNCTIONS – WHAT DO THEY LOOK LIKE? Functions have a definition, parameters, and some (most) have return value(s)

EXAMPLES Example 1: def print hi(): print(“Hi!”) print hi() Example 2: def print hi(name): print(“Hi”, name, “!”) print hi(‘Bob’) Example 3: def square(num): return num**2 What is the output? Example 4: def print hi(name): print(“Hi”, name, “!”) Hi! Hi Bob ! Nothing Error

EXAMPLES def average(number1, number2): average (number1 number2) / 2 return average avg average(2,3) print(avg) num1 10 num2 20 avg average(num1,num2 2.5 15

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?

FUNCTIONS – MULTIPLE RETURNS def get double and triple(num): return num*2, num*3 IMPORTANT double two, triple two get double and triple(2) print(“2 * 2 ”, double two, “ 2 * 3 ”, triple two) 2 * 2 4 2 * 3 6

GLOBAL VS LOCAL VARIABLES

LOCAL def local test(): local variable “Hi, I’m Local!” print(local variable) What’s the issue here?

LOCAL def local test(): local variable “Hi, I’m Local!” print(local variable) local variable only accessible inside local test()!

GLOBAL x 10 y 4 def print globals(y): y 3 #10 print(x) #3 print(y) print globals(2) print(x) print(y) #10 #4

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?

GLOBAL x 10 y 20 X and y are global because they were defined outside of a function def mess with globals(): x 10 y 10 print(x) #10 print(y) #20 Nothing happen because you did not call the function

GLOBAL x 10 y 20 X and y are global because they were defined outside of a function def mess with globals(): x 10 y 10 mess with globals() print(x) print(y) UnboundLocalError: local variable ‘x' referenced before assignment

GLOBAL x 10 y 20 def mess with globals(x,y): x 10 y 10 return x,y x,y mess with globals(x,y) print(x) #20 print(y) #30 If you want to change a variable, pass the variable to the function and return the result All good

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 or modify a variable,

Back to top button