dennis komm programming and problem-solving · control flow – if order of the (repeated)...

117
Dennis Komm Programming and Problem-Solving Functions, local, and global variables Spring 2020 – March 12, 2020

Upload: others

Post on 26-Sep-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Dennis Komm

Programming and Problem-SolvingFunctions, local, and global variables

Spring 2020 – March 12, 2020

Page 2: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Control StructuresControl Flow

Page 3: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Control Flow – if

Order of the (repeated) execution of statements

generally from top to bottom. . .

. . . except in selection and iteration statements

condition

statement

true

false if condition:statement

Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 1 / 23

Page 4: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Control Flow – if

Order of the (repeated) execution of statements

generally from top to bottom. . .

. . . except in selection and iteration statements

condition

statement

true

false

if condition:statement

Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 1 / 23

Page 5: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Control Flow – if

Order of the (repeated) execution of statements

generally from top to bottom. . .

. . . except in selection and iteration statements

condition

statement

true

false

if condition:statement

Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 1 / 23

Page 6: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Control Flow – if

Order of the (repeated) execution of statements

generally from top to bottom. . .

. . . except in selection and iteration statements

condition

statement

true

false if condition:statement

Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 1 / 23

Page 7: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Control Flow – if-else

condition

statement1

statement2

true

false

if condition:statement1

else:statement2

Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 2 / 23

Page 8: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Control Flow – if-else

condition

statement1

statement2

true

falseif condition:

statement1else:

statement2

Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 2 / 23

Page 9: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Control Flow – if-else

condition

statement1

statement2

true

falseif condition:

statement1else:

statement2

Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 2 / 23

Page 10: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Control Flow – while

condition

statement

true

false

while condition:statement

Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 3 / 23

Page 11: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Control Flow – while

condition

statement

true

false

while condition:statement

Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 3 / 23

Page 12: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Control Flow – while

condition

statement

true

falsewhile condition:

statement

Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 3 / 23

Page 13: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Control Flow – while

condition

statement

true

falsewhile condition:

statement

Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 3 / 23

Page 14: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Kontrollfluss break in while-Schleife

condition

statement

break

Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 4 / 23

Page 15: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Kontrollfluss break in while-Schleife

condition

statement

break

Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 4 / 23

Page 16: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Functions

Page 17: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Functions

So far. . .

One algorithm per fileStatements are processed sequentiallyUsage of loops and control structures

Group related code as function

def welcome():

date = "October 24, 2019"print("Hello", username, "!")print("Welcome to the lecture on", date)

welcome()

Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 5 / 23

Page 18: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Functions

So far. . .

One algorithm per fileStatements are processed sequentiallyUsage of loops and control structures

Group related code as function

def welcome():

date = "October 24, 2019"print("Hello", username, "!")print("Welcome to the lecture on", date)

welcome()

Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 5 / 23

Page 19: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Functions

So far. . .

One algorithm per fileStatements are processed sequentiallyUsage of loops and control structures

Group related code as function

def welcome():

date = "October 24, 2019"print("Hello", username, "!")print("Welcome to the lecture on", date)

welcome()

Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 5 / 23

Page 20: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Functions

So far. . .

One algorithm per fileStatements are processed sequentiallyUsage of loops and control structures

Group related code as function

def welcome():

date = "October 24, 2019"print("Hello", username, "!")print("Welcome to the lecture on", date)

welcome()Definition of a function

Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 5 / 23

Page 21: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Functions

So far. . .

One algorithm per fileStatements are processed sequentiallyUsage of loops and control structures

Group related code as function

def welcome():

date = "October 24, 2019"print("Hello", username, "!")print("Welcome to the lecture on", date)

welcome()

Optional list of parameters

Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 5 / 23

Page 22: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Analogy to Natural Languages

Python “understands” some specific words

These are called keywords: def, if, while, etc.

Basic stock of functions: print(), range(), input(), etc.

def f(): ⇐⇒ Python “learns” new word f

From Merriam-Webster dictionary

re·frig·er·a·torA room or appliance for keeping food or other items cool

Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 6 / 23

Page 23: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Analogy to Natural Languages

Python “understands” some specific words

These are called keywords: def, if, while, etc.

Basic stock of functions: print(), range(), input(), etc.

def f(): ⇐⇒ Python “learns” new word f

From Merriam-Webster dictionary

re·frig·er·a·torA room or appliance for keeping food or other items cool

Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 6 / 23

Page 24: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Analogy to Natural Languages

Python “understands” some specific words

These are called keywords: def, if, while, etc.

Basic stock of functions: print(), range(), input(), etc.

def f(): ⇐⇒ Python “learns” new word f

From Merriam-Webster dictionary

re·frig·er·a·torA room or appliance for keeping food or other items cool

Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 6 / 23

Page 25: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Analogy to Natural Languages

def welcome():date = "October 24, 2019"print("Hello", username, "!")print("Welcome to the lecture on", date)

username = input("Enter username:")if username == "ffrei" or username == "dkomm":

welcome()...

else:print("Username not found.")

...

date = "October 24, 2019"print("Hello", username, "!")print("Welcome to the lecture on", date)

Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 7 / 23

Page 26: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Analogy to Natural Languages

def welcome():date = "October 24, 2019"print("Hello", username, "!")print("Welcome to the lecture on", date)

username = input("Enter username:")if username == "ffrei" or username == "dkomm":

welcome()...

else:print("Username not found.")

...

date = "October 24, 2019"print("Hello", username, "!")print("Welcome to the lecture on", date)

Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 7 / 23

Page 27: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Analogy to Natural Languages

def welcome():date = "October 24, 2019"print("Hello", username, "!")print("Welcome to the lecture on", date)

username = input("Enter username:")if username == "ffrei" or username == "dkomm":

welcome()

...else:

print("Username not found.")...

date = "October 24, 2019"print("Hello", username, "!")print("Welcome to the lecture on", date)

Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 7 / 23

Page 28: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Analogy to Natural Languages

def welcome():date = "October 24, 2019"print("Hello", username, "!")print("Welcome to the lecture on", date)

username = input("Enter username:")if username == "ffrei" or username == "dkomm":

welcome()

...else:

print("Username not found.")...

date = "October 24, 2019"print("Hello", username, "!")print("Welcome to the lecture on", date)

Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 7 / 23

Page 29: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Analogy to Mathematical Functions

f(x) = 2 · x + 1

Functions in Python

Parameter x is passed to functionValue is passed back using return

def f(x):y = 2 * x + 1return y

def f(x):return 2 * x + 1

return without argument is used to simply end the function call

Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 8 / 23

Page 30: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Analogy to Mathematical Functions

f(x) = 2 · x + 1

Functions in Python

Parameter x is passed to functionValue is passed back using return

def f(x):y = 2 * x + 1return y

def f(x):return 2 * x + 1

return without argument is used to simply end the function call

Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 8 / 23

Page 31: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Analogy to Mathematical Functions

f(x) = 2 · x + 1

Functions in Python

Parameter x is passed to functionValue is passed back using return

def f(x):y = 2 * x + 1return y

def f(x):return 2 * x + 1

return without argument is used to simply end the function call

Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 8 / 23

Page 32: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Analogy to Mathematical Functions

f(x) = 2 · x + 1

Functions in Python

Parameter x is passed to functionValue is passed back using return

def f(x):y = 2 * x + 1return y

def f(x):return 2 * x + 1

return without argument is used to simply end the function call

Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 8 / 23

Page 33: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Analogy to Mathematical Functions

f(x) = 2 · x + 1

Functions in Python

Parameter x is passed to functionValue is passed back using return

def f(x):y = 2 * x + 1return y

def f(x):return 2 * x + 1

return without argument is used to simply end the function callProgramming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 8 / 23

Page 34: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Analogy to Mathematical Functions

def f(x):return 2 * x + 1

By using return, the function call represents the corresponding value

print(f(5)) results in output 11

z = f(6) assigns z the value 13

z = 3 * f(2) + f(4) assigns z the value 24

b = (f(10) > 20) assigns b the Boolean value True

Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 9 / 23

Page 35: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Analogy to Mathematical Functions

def f(x):return 2 * x + 1

By using return, the function call represents the corresponding value

print(f(5)) results in output 11

z = f(6) assigns z the value 13

z = 3 * f(2) + f(4) assigns z the value 24

b = (f(10) > 20) assigns b the Boolean value True

Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 9 / 23

Page 36: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Analogy to Mathematical Functions

def f(x):return 2 * x + 1

By using return, the function call represents the corresponding value

print(f(5)) results in output 11

z = f(6) assigns z the value 13

z = 3 * f(2) + f(4) assigns z the value 24

b = (f(10) > 20) assigns b the Boolean value True

Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 9 / 23

Page 37: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Analogy to Mathematical Functions

def f(x):return 2 * x + 1

By using return, the function call represents the corresponding value

print(f(5)) results in output 11

z = f(6) assigns z the value 13

z = 3 * f(2) + f(4) assigns z the value 24

b = (f(10) > 20) assigns b the Boolean value True

Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 9 / 23

Page 38: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Analogy to Mathematical Functions

def f(x):return 2 * x + 1

By using return, the function call represents the corresponding value

print(f(5)) results in output 11

z = f(6) assigns z the value 13

z = 3 * f(2) + f(4) assigns z the value 24

b = (f(10) > 20) assigns b the Boolean value True

Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 9 / 23

Page 39: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Functions with Parameters

def checkuser(givenname):

validnames = [ "ffrei", "ezalzala", "jkaufmann", "lfritschi", "skamp" ]if givenname in validnames:

return Trueelse:

return False

username = input("Enter username:")

if checkuser(username) == True:

print("Welcome", username)password = input("Enter your password:")...

else:print("Username not found.")

Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 10 / 23

Page 40: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Functions with Parameters

def checkuser(givenname):

validnames = [ "ffrei", "ezalzala", "jkaufmann", "lfritschi", "skamp" ]if givenname in validnames:

return Trueelse:

return False

username = input("Enter username:")

if checkuser(username) == True:

print("Welcome", username)password = input("Enter your password:")...

else:print("Username not found.")

Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 10 / 23

Page 41: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Functions with Parameters

def checkuser(givenname):

validnames = [ "ffrei", "ezalzala", "jkaufmann", "lfritschi", "skamp" ]if givenname in validnames:

return Trueelse:

return False

username = input("Enter username:")

if checkuser(username) == True:

print("Welcome", username)password = input("Enter your password:")...

else:print("Username not found.")

Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 10 / 23

Page 42: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Functions with Parameters

def checkuser(givenname):

validnames = [ "ffrei", "ezalzala", "jkaufmann", "lfritschi", "skamp" ]if givenname in validnames:

return Trueelse:

return False

username = input("Enter username:")

if checkuser(username) == True:

print("Welcome", username)password = input("Enter your password:")...

else:print("Username not found.")

Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 10 / 23

Page 43: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Functions with Parameters

username = input(”Enter username:”)

Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 11 / 23

Page 44: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Functions with Parameters

username = input(”Enter username:”)

Enter username:

Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 11 / 23

Page 45: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Functions with Parameters

username = input(”Enter username:”)

Enter username: dkomm

Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 11 / 23

Page 46: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Functions with Parameters

username = input(”Enter username:”)

Enter username: dkomm

username = dkomm

Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 11 / 23

Page 47: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Functions with Parameters

username = input(”Enter username:”)

Enter username: dkomm

username = dkomm

if checkuser(username) == True:

Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 11 / 23

Page 48: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Functions with Parameters

username = input(”Enter username:”)

Enter username: dkomm

username = dkomm

if checkuser( dkomm ) == True:

Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 11 / 23

Page 49: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Functions with Parameters

username = input(”Enter username:”)

Enter username: dkomm

username = dkomm

if checkuser( dkomm ) == True:

def checkuser(givenname):validnames = [ ”ffrei”, ”ezalzala”, ”lfritschi”, ”skamp”, ”spiasko” ]if givenname in validnames:

return Trueelse:

return False

Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 11 / 23

Page 50: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Functions with Parameters

username = input(”Enter username:”)

Enter username: dkomm

username = dkomm

if checkuser( dkomm ) == True:

def checkuser(dkomm):validnames = [ ”ffrei”, ”ezalzala”, ”lfritschi”, ”skamp”, ”spiasko” ]if givenname in validnames:

return Trueelse:

return False

Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 11 / 23

Page 51: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Functions with Parameters

username = input(”Enter username:”)

Enter username: dkomm

username = dkomm

if checkuser( dkomm ) == True:

def checkuser(dkomm):validnames = [ ”ffrei”, ”ezalzala”, ”lfritschi”, ”skamp”, ”spiasko” ]if dkomm in validnames:

return Trueelse:

return False

Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 11 / 23

Page 52: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Functions with Parameters

username = input(”Enter username:”)

Enter username: dkomm

username = dkomm

if checkuser( dkomm ) == True:

def checkuser(dkomm):validnames = [ ”ffrei”, ”ezalzala”, ”lfritschi”, ”skamp”, ”spiasko” ]if dkomm in validnames:

return Trueelse:

return False

Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 11 / 23

Page 53: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Functions with Parameters

username = input(”Enter username:”)

Enter username: dkomm

username = dkomm

if checkuser( dkomm ) == True:

def checkuser(dkomm):validnames = [ ”ffrei”, ”ezalzala”, ”lfritschi”, ”skamp”, ”spiasko” ]if dkomm in validnames:

return Trueelse:

return False

if checkuser(dkomm) == True:

Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 11 / 23

Page 54: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Functions with Parameters

username = input(”Enter username:”)

Enter username: dkomm

username = dkomm

if checkuser( dkomm ) == True:

def checkuser(dkomm):validnames = [ ”ffrei”, ”ezalzala”, ”lfritschi”, ”skamp”, ”spiasko” ]if dkomm in validnames:

return Trueelse:

return False

if False == True:

Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 11 / 23

Page 55: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Functions with Parameters

username = input(”Enter username:”)

Enter username: dkomm

username = dkomm

if checkuser( dkomm ) == True:

def checkuser(dkomm):validnames = [ ”ffrei”, ”ezalzala”, ”lfritschi”, ”skamp”, ”spiasko” ]if dkomm in validnames:

return Trueelse:

return False

if False == True:

Username not found.Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 11 / 23

Page 56: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Definition of Functions

Function has to be defined before it can be used

def f(x):return 2 * x + 1

print(f(2))

works, but not. . .

print(f(2))

def f(x):return 2 * x + 1

NameError: name ’f’ is not defined

Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 12 / 23

Page 57: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Definition of Functions

Function has to be defined before it can be used

def f(x):return 2 * x + 1

print(f(2))

works, but not. . .

print(f(2))

def f(x):return 2 * x + 1

NameError: name ’f’ is not defined

Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 12 / 23

Page 58: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Definition of Functions

Function has to be defined before it can be used

def f(x):return 2 * x + 1

print(f(2))

works, but not. . .

print(f(2))

def f(x):return 2 * x + 1

NameError: name ’f’ is not defined

Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 12 / 23

Page 59: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Definition of Functions

Function has to be defined before it can be used

def f(x):return 2 * x + 1

print(f(2))

works, but not. . .

print(f(2))

def f(x):return 2 * x + 1

NameError: name ’f’ is not definedProgramming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 12 / 23

Page 60: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

FunctionsExample – Cookie Calculator

Page 61: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Example – Cookie Calculator

children = int(input("Number of children:"))cookies = int(input("Number of cookies:"))

print("Every child receives", cookies // children, "cookies")print("Dad receives", cookies % children, "cookies")

We want to make sure that children is positive and that each child gets atleast one cookie

Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 13 / 23

Page 62: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Example – Cookie Calculator

children = int(input("Number of children:"))cookies = int(input("Number of cookies:"))

print("Every child receives", cookies // children, "cookies")print("Dad receives", cookies % children, "cookies")

We want to make sure that children is positive and that each child gets atleast one cookie

Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 13 / 23

Page 63: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Cookie Calculator – Check Input

From this . . .

children = int(input("Number of children:"))

. . . we go to this

while True:children = int(input("Number of children:"))if children >= 1:

breakelse:

print("Value needs to be at least 1")

Analogously, we have to check that cookies >= children

Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 14 / 23

Page 64: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Cookie Calculator – Check Input

From this . . .

children = int(input("Number of children:"))

. . . we go to this

while True:children = int(input("Number of children:"))if children >= 1:

breakelse:

print("Value needs to be at least 1")

Analogously, we have to check that cookies >= children

Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 14 / 23

Page 65: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Cookie Calculator – Check Input

From this . . .

children = int(input("Number of children:"))

. . . we go to this

while True:children = int(input("Number of children:"))if children >= 1:

breakelse:

print("Value needs to be at least 1")

Analogously, we have to check that cookies >= children

Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 14 / 23

Page 66: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Cookie Calculator – Check Input

From this . . .

children = int(input("Number of children:"))

. . . we go to this

while True:children = int(input("Number of children:"))if children >= 1:

breakelse:

print("Value needs to be at least 1")

Analogously, we have to check that cookies >= children

Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 14 / 23

Page 67: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Cookie Calculator – Getting Complicated

while True:

children = int(input("Number of children:"))if children >= 1:

breakelse:

print("Value needs to be at least 1")

while True:

cookies = int(input("Number of cookies:"))if cookies >= children:

breakelse:

print("Value needs to be at least", children)

print("Every child receives", cookies // cookies, "cookies")print("Dad receives", cookies % children, "cookies")

Read and checknumber of children

Read and checknumber of cookies

Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 15 / 23

Page 68: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Cookie Calculator – Getting Complicated

while True:

children = int(input("Number of children:"))if children >= 1:

breakelse:

print("Value needs to be at least 1")

while True:

cookies = int(input("Number of cookies:"))if cookies >= children:

breakelse:

print("Value needs to be at least", children)

print("Every child receives", cookies // cookies, "cookies")print("Dad receives", cookies % children, "cookies")

Read and checknumber of children

Read and checknumber of cookies

Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 15 / 23

Page 69: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Cookie Calculator – Getting Complicated

while True:

children = int(input("Number of children:"))if children >= 1:

breakelse:

print("Value needs to be at least 1")

while True:

cookies = int(input("Number of cookies:"))if cookies >= children:

breakelse:

print("Value needs to be at least", children)

print("Every child receives", cookies // cookies, "cookies")print("Dad receives", cookies % children, "cookies")

Read and checknumber of children

Read and checknumber of cookies

Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 15 / 23

Page 70: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Cookie Calculator – Takeaway

The two code fragments are nearly identical

The following aspects are different:The prompt, i.e., "children:" vs. "cookies:"The minimum, i.e., 1 vs. children

We can outsource the code fragment into a function and thus feature reuse

We have to parameterize the different aspects

Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 16 / 23

Page 71: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Cookie Calculator – Takeaway

The two code fragments are nearly identical

The following aspects are different:The prompt, i.e., "children:" vs. "cookies:"The minimum, i.e., 1 vs. children

We can outsource the code fragment into a function and thus feature reuse

We have to parameterize the different aspects

Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 16 / 23

Page 72: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Cookie Calculator – Takeaway

The two code fragments are nearly identical

The following aspects are different:The prompt, i.e., "children:" vs. "cookies:"The minimum, i.e., 1 vs. children

We can outsource the code fragment into a function and thus feature reuse

We have to parameterize the different aspects

Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 16 / 23

Page 73: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Cookie Calculator – Takeaway

The two code fragments are nearly identical

The following aspects are different:The prompt, i.e., "children:" vs. "cookies:"The minimum, i.e., 1 vs. children

We can outsource the code fragment into a function and thus feature reuse

We have to parameterize the different aspects

Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 16 / 23

Page 74: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Exercise – Cookie Calculator

Write a function that

gets two parameters prompt and minimum

asks the user for an integer input

returns the input using return if it is at leastminimum

otherwise asks for a new input

Use the function in the cookie calculator

Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 17 / 23

Page 75: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Exercise – Cookie Calculator

def checkinput(prompt, minimum):while True:

x = int(input(prompt))if x >= minimum:

return xelse:

print("Value needs to be at least", minimum)

children = checkinput("Number of children:", 1)cookies = checkinput("Number of cookies:", children)

print("Every child receives", cookies // cookies, "cookies")print("Dad receives", cookies % children, "cookies")

Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 18 / 23

Page 76: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

FunctionsScope and Lifetime of Variables

Page 77: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Local Variables

Parameters of a function are only valid within this function

def f(x):return x + 5

print(x)

NameError: name ’x’ is not defined

Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 19 / 23

Page 78: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Local Variables

Parameters of a function are only valid within this function

def f(x):return x + 5

print(x)

NameError: name ’x’ is not defined

Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 19 / 23

Page 79: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Local Variables

The same is true for variables that are defined in a function

def f(x):y = 5return x + y

print(y)

NameError: name ’y’ is not defined

Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 20 / 23

Page 80: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Local Variables

The same is true for variables that are defined in a function

def f(x):y = 5return x + y

print(y)

NameError: name ’y’ is not defined

Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 20 / 23

Page 81: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Local Variables

Such variables (parameters) are called local variablesVariables defined outside of a function are called global variablesField of validity is called scope of the variableTime in which it is defined is called lifetime of the variable

def f(x):if x < 0:

return -100y = x + 1if y < 10:

y += 10else:

y -= 20return y

def f(x):if x < 0:

return -100y = x + 1if y < 10:

y += 10else:

y -= 20return y

Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 21 / 23

Page 82: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Local Variables

Such variables (parameters) are called local variablesVariables defined outside of a function are called global variablesField of validity is called scope of the variableTime in which it is defined is called lifetime of the variable

def f(x):if x < 0:

return -100y = x + 1if y < 10:

y += 10else:

y -= 20return y

def f(x):if x < 0:

return -100y = x + 1if y < 10:

y += 10else:

y -= 20return y

Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 21 / 23

Page 83: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Local Variables

Such variables (parameters) are called local variablesVariables defined outside of a function are called global variablesField of validity is called scope of the variableTime in which it is defined is called lifetime of the variable

def f(x):if x < 0:

return -100y = x + 1if y < 10:

y += 10else:

y -= 20return y

scop

eof

x

def f(x):if x < 0:

return -100y = x + 1if y < 10:

y += 10else:

y -= 20return y

Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 21 / 23

Page 84: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Local Variables

Such variables (parameters) are called local variablesVariables defined outside of a function are called global variablesField of validity is called scope of the variableTime in which it is defined is called lifetime of the variable

def f(x):if x < 0:

return -100y = x + 1if y < 10:

y += 10else:

y -= 20return y

scop

eof

x

def f(x):if x < 0:

return -100y = x + 1if y < 10:

y += 10else:

y -= 20return y

Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 21 / 23

Page 85: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Local Variables

Such variables (parameters) are called local variablesVariables defined outside of a function are called global variablesField of validity is called scope of the variableTime in which it is defined is called lifetime of the variable

def f(x):if x < 0:

return -100y = x + 1if y < 10:

y += 10else:

y -= 20return y

scop

eof

x

def f(x):if x < 0:

return -100y = x + 1if y < 10:

y += 10else:

y -= 20return y

scop

eof

y

Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 21 / 23

Page 86: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Global Variables

Global variables can be accessed within a function

x = 1

def f():y = x + 1print(y)return

print(x)

f()

print(x)

x = 1

def f(y):

z = x + yreturn z

print(x)

print(f(2))

print(x)

Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 22 / 23

Page 87: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Global Variables

Global variables can be accessed within a function

x = 1

def f():y = x + 1print(y)return

print(x)

f()

print(x)

x = 1

def f(y):

z = x + yreturn z

print(x)

print(f(2))

print(x)

Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 22 / 23

Page 88: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Global Variables

Global variables can be accessed within a function

x = 1

def f():y = x + 1print(y)return

print(x)

f()

print(x)

global variable x

x = 1

def f(y):

z = x + yreturn z

print(x)

print(f(2))

print(x)

Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 22 / 23

Page 89: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Global Variables

Global variables can be accessed within a function

x = 1

def f():y = x + 1print(y)return

print(x)

f()

print(x)

local variable y gets valuewhich depends on global variable x

x = 1

def f(y):

z = x + yreturn z

print(x)

print(f(2))

print(x)

Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 22 / 23

Page 90: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Global Variables

Global variables can be accessed within a function

x = 1

def f():y = x + 1print(y)return

print(x)

f()

print(x)

output global variable x

x = 1

def f(y):

z = x + yreturn z

print(x)

print(f(2))

print(x)

Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 22 / 23

Page 91: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Global Variables

Global variables can be accessed within a function

x = 1

def f():y = x + 1print(y)return

print(x)

f()

print(x)

output global variable xoutput local variable y

x = 1

def f(y):

z = x + yreturn z

print(x)

print(f(2))

print(x)

Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 22 / 23

Page 92: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Global Variables

Global variables can be accessed within a function

x = 1

def f():y = x + 1print(y)return

print(x)

f()

print(x)

output global variable xoutput local variable y

output global variable x

x = 1

def f(y):

z = x + yreturn z

print(x)

print(f(2))

print(x)

Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 22 / 23

Page 93: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Global Variables

Global variables can be accessed within a function

x = 1

def f():y = x + 1print(y)return

print(x)

f()

print(x)

x = 1

def f(y):

z = x + yreturn z

print(x)

print(f(2))

print(x)

Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 22 / 23

Page 94: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Global Variables

Global variables can be accessed within a function

x = 1

def f():y = x + 1print(y)return

print(x)

f()

print(x)

x = 1

def f(y):

z = x + yreturn z

print(x)

print(f(2))

print(x)

global variable x

Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 22 / 23

Page 95: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Global Variables

Global variables can be accessed within a function

x = 1

def f():y = x + 1print(y)return

print(x)

f()

print(x)

x = 1

def f(y):

z = x + yreturn z

print(x)

print(f(2))

print(x)

parameter y

Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 22 / 23

Page 96: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Global Variables

Global variables can be accessed within a function

x = 1

def f():y = x + 1print(y)return

print(x)

f()

print(x)

x = 1

def f(y):

z = x + yreturn z

print(x)

print(f(2))

print(x)

local variable z gets valuethat depends on global variable xand parameter y

Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 22 / 23

Page 97: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Global Variables

Global variables can be accessed within a function

x = 1

def f():y = x + 1print(y)return

print(x)

f()

print(x)

x = 1

def f(y):

z = x + yreturn z

print(x)

print(f(2))

print(x)

output global variable x

Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 22 / 23

Page 98: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Global Variables

Global variables can be accessed within a function

x = 1

def f():y = x + 1print(y)return

print(x)

f()

print(x)

x = 1

def f(y):

z = x + yreturn z

print(x)

print(f(2))

print(x)

output global variable x

output local variable z

Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 22 / 23

Page 99: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Global Variables

Global variables can be accessed within a function

x = 1

def f():y = x + 1print(y)return

print(x)

f()

print(x)

x = 1

def f(y):

z = x + yreturn z

print(x)

print(f(2))

print(x)

output global variable x

output local variable zoutput global variable x

Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 22 / 23

Page 100: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Local Variables

Local and global variables can have the same name

ShadowingNot forbidden, but should be avoided here whenever possible

x = 1

def f():x = 2

return x

print(x)

print(f())

print(x)

x = 1

def f(x):

x = x + 1

return x

print(x)

print(f(2))

print(x)

Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 23 / 23

Page 101: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Local Variables

Local and global variables can have the same name

ShadowingNot forbidden, but should be avoided here whenever possible

x = 1

def f():x = 2

return x

print(x)

print(f())

print(x)

x = 1

def f(x):

x = x + 1

return x

print(x)

print(f(2))

print(x)

Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 23 / 23

Page 102: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Local Variables

Local and global variables can have the same name

ShadowingNot forbidden, but should be avoided here whenever possible

x = 1

def f():x = 2

return x

print(x)

print(f())

print(x)

x = 1

def f(x):

x = x + 1

return x

print(x)

print(f(2))

print(x)

Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 23 / 23

Page 103: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Local Variables

Local and global variables can have the same name

ShadowingNot forbidden, but should be avoided here whenever possible

x = 1

def f():x = 2

return x

print(x)

print(f())

print(x)

global variable x

x = 1

def f(x):

x = x + 1

return x

print(x)

print(f(2))

print(x)

Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 23 / 23

Page 104: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Local Variables

Local and global variables can have the same name

ShadowingNot forbidden, but should be avoided here whenever possible

x = 1

def f():x = 2

return x

print(x)

print(f())

print(x)

local variable x

x = 1

def f(x):

x = x + 1

return x

print(x)

print(f(2))

print(x)

Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 23 / 23

Page 105: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Local Variables

Local and global variables can have the same name

ShadowingNot forbidden, but should be avoided here whenever possible

x = 1

def f():x = 2

return x

print(x)

print(f())

print(x)

local variable x is returned

x = 1

def f(x):

x = x + 1

return x

print(x)

print(f(2))

print(x)

Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 23 / 23

Page 106: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Local Variables

Local and global variables can have the same name

ShadowingNot forbidden, but should be avoided here whenever possible

x = 1

def f():x = 2

return x

print(x)

print(f())

print(x)

output global variable x

x = 1

def f(x):

x = x + 1

return x

print(x)

print(f(2))

print(x)

Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 23 / 23

Page 107: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Local Variables

Local and global variables can have the same name

ShadowingNot forbidden, but should be avoided here whenever possible

x = 1

def f():x = 2

return x

print(x)

print(f())

print(x)

output global variable x

output local variable x

x = 1

def f(x):

x = x + 1

return x

print(x)

print(f(2))

print(x)

Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 23 / 23

Page 108: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Local Variables

Local and global variables can have the same name

ShadowingNot forbidden, but should be avoided here whenever possible

x = 1

def f():x = 2

return x

print(x)

print(f())

print(x)

output global variable x

output local variable xoutput global variable x

x = 1

def f(x):

x = x + 1

return x

print(x)

print(f(2))

print(x)

Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 23 / 23

Page 109: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Local Variables

Local and global variables can have the same name

ShadowingNot forbidden, but should be avoided here whenever possible

x = 1

def f():x = 2

return x

print(x)

print(f())

print(x)

x = 1

def f(x):

x = x + 1

return x

print(x)

print(f(2))

print(x)

Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 23 / 23

Page 110: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Local Variables

Local and global variables can have the same name

ShadowingNot forbidden, but should be avoided here whenever possible

x = 1

def f():x = 2

return x

print(x)

print(f())

print(x)

x = 1

def f(x):

x = x + 1

return x

print(x)

print(f(2))

print(x)

global variable x

Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 23 / 23

Page 111: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Local Variables

Local and global variables can have the same name

ShadowingNot forbidden, but should be avoided here whenever possible

x = 1

def f():x = 2

return x

print(x)

print(f())

print(x)

x = 1

def f(x):

x = x + 1

return x

print(x)

print(f(2))

print(x)

parameter x

Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 23 / 23

Page 112: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Local Variables

Local and global variables can have the same name

ShadowingNot forbidden, but should be avoided here whenever possible

x = 1

def f():x = 2

return x

print(x)

print(f())

print(x)

x = 1

def f(x):

x = x + 1

return x

print(x)

print(f(2))

print(x)

parameter x gets new valuethat depends on itscurrent value

Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 23 / 23

Page 113: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Local Variables

Local and global variables can have the same name

ShadowingNot forbidden, but should be avoided here whenever possible

x = 1

def f():x = 2

return x

print(x)

print(f())

print(x)

x = 1

def f(x):

x = x + 1

return x

print(x)

print(f(2))

print(x)

parameter x is returned

Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 23 / 23

Page 114: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Local Variables

Local and global variables can have the same name

ShadowingNot forbidden, but should be avoided here whenever possible

x = 1

def f():x = 2

return x

print(x)

print(f())

print(x)

x = 1

def f(x):

x = x + 1

return x

print(x)

print(f(2))

print(x)

output global variable x

Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 23 / 23

Page 115: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Local Variables

Local and global variables can have the same name

ShadowingNot forbidden, but should be avoided here whenever possible

x = 1

def f():x = 2

return x

print(x)

print(f())

print(x)

x = 1

def f(x):

x = x + 1

return x

print(x)

print(f(2))

print(x)

output global variable xoutput parameter x

Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 23 / 23

Page 116: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Local Variables

Local and global variables can have the same name

ShadowingNot forbidden, but should be avoided here whenever possible

x = 1

def f():x = 2

return x

print(x)

print(f())

print(x)

x = 1

def f(x):

x = x + 1

return x

print(x)

print(f(2))

print(x)

output global variable xoutput parameter x

output global variable x

Programming and Problem-Solving – Functions and local variables Spring 2020 Dennis Komm 23 / 23

Page 117: Dennis Komm Programming and Problem-Solving · Control Flow – if Order of the (repeated) execution of statements generally from top to bottom.....except in selection and iteration

Thanks for your attention