Lecture 7 Functions PartI
Lecture 7 Functions PartI
languages
Functions- Part I
Today…
• Last Session:
• Basic Elements of Python Programs
• Today’s Session:
• Functions- Part I:
• Why using functions?
• Formal definition, parameters, local and global scopes of variables, return values, and
pass-by-value
Multiple-line Snippets and Functions
• So far, we have been using only one-line snippets in the interactive
mode, but we may want to go beyond that and execute an entire
sequence of statements
Run
Run
10 20 10 100 ERROR
Parameters vs. Arguments
• The terms parameter and argument refer to the same thing: passing
information to a function. But, there is a subtle difference between
the two.
• A parameter is the variable inside the parenthesis in a function. An
argument is the value that is passed to a function when it is called.
Modularity and Maintenance
• Consider the following code def happy():
print("Happy birthday to you!")
print("Happy birthday to you!")
print("Happy birthday to you!") def singFred():
print("Happy birthday, dear Fred") happy()
print("Happy birthday to you!") happy()
print("Happy birthday, dear Fred")
Can we write this program with
happy()
ONLY two prints?
singFred()
More modular & maintainable– changing anything in the lyric “Happy birthday to you!”
requires making a change at only one place in happy(); thanks to the happy function!
Extensibility and Readability
• Consider the following code
print("Happy birthday to you!")
print("Happy birthday to you!") print("Happy birthday to you!")
print("Happy birthday to you!") print("Happy birthday, dear Fred")
print("Happy birthday, dear Fred") print("Happy birthday to you!")
print("Happy birthday to you!") print("Happy birthday to you!")
print("Happy birthday to you!")
What if we want to sing a verse for print("Happy birthday, dear Lucy")
Lucy right after Fred? print("Happy birthday to you!")
x, y, and z belong solely to the scope of func1(...) and can only be accessed inside
func1(…); z is said to be local to func1(…), hence, referred to as a local variable
Global Variables
• Consider the following code
#global scope
x = 100
func2()
print(x)
x is said to be a global variable since it is defined within the global scope of the
program and can be, subsequently, accessed inside and outside func2()
Local vs. Global Variables
• Consider the following code
x = 100
def func3():
x = 20 20
Run
print(x) 100
func3()
print(x)
The global variable x is distinct from the local variable x inside func3()
Parameters vs. Global Variables
• Consider the following code
x = 100
def func4(x):
print(x) 20
Run
100
func4(20)
print(x)
def func5():
global x
x = 20
print(x) 20
Run
20
func5()
print(x)
The global keyword binds variable x in the global scope; hence, can be accessed inside
and outside func5()
Getting Results From Functions
• We can get information from a function by having it return a value
>>> def square(x): >>> def cube(x): >>> def power(a, b):
... return x * x ... return x * x * x ... return a ** b
... ... ...
>>> square(3) >>> cube(3) >>> power(2, 3)
9 27 8
>>> >>> >>>
Pass By Value
• Consider the following code
>>> def addInterest(balance, rate):
... newBalance = balance * (1+rate)
... return newBalance
...
>>> def test():
... amount = 1000
... rate = 0.05
... nb = addInterest(amount, rate)
... print(nb)
...
>>> test()
1050.0
>>>
Pass By Value
• Is there a way for a function to communicate back its result without
returning it?
>>> def addInterest(balance, rate):
... newBalance = balance * rate
... balance = newBalance
...
>>> def test():
... amount = 1000
... rate = 0.05
... addInterest(amount, rate)
What will be the result? ... print(amount)
...
>>> test()
1000
>>>
Pass By Value
• Is there a way for a function to communicate back its result without
returning it?
>>> def addInterest(balance, rate):
... newBalance = balance * rate
... balance = newBalance
...
>>> def test():
... amount = 1000
... rate = 0.05
Why 1000 and NOT ... addInterest(amount, rate)
... print(amount)
1050.0? ...
>>> test()
1000
>>>
Pass By Value
• The function only receives the values of the parameters
1000
addInterest(…) gets >>> def addInterest(balance, rate):
... newBalance = balance * rate
ONLY the value of
... balance = newBalance
amount (i.e., 1000) ...
>>> def test(): Python is said to
... amount = 1000 pass parameters
... rate = 0.05
... addInterest(amount, rate) by value!
... print(amount)
...
>>> test()
1000
>>>
Pass By Value vs. Returning a Value
• Consider the following code
def increment_func(x):
def increment_func(x): x=x+1
x=x+1 return x
x=1 x=1
increment_func(x) x = increment_func(x)
print(x) print(x)
Run
Run
1 2
Python Return
• def example_function():
• return "Example"
More on the Print Function
• There are different forms of the print function
1) print(), which produces a blank line of output
>>> print()
>>>
More on the Print Function
• There are different forms of the print function
2) print(<expr>, <expr>, …, <expr>), which indicates that the print function can
take a sequence of expressions, separated by commas
>>> print(3+4)
7
>>> print(3, 4, 3+4)
347
>>> print("The answer is ", 3 + 4)
The answer is 7
>>> print("The answer is", 3 + 4)
The answer is 7
More on the Print Function
• There are different forms of the print function
3) print(<expr>, <expr>, …, <expr>, end = “\n”), which indicates that the print
function can be modified to have an ending text other than the default one
(i.e., \n or a new line) after all the supplied expressions are printed