Unit4-Functions and Classes
Unit4-Functions and Classes
CLASSES
n Advantage of modularizing program into functions,
function definition and function invocation.
n Function arguments: default, keyword and positional
arguments.
n Scope and lifetime of a variable. Recurrence relations
and Recursion
n Advantage of using classes, defining class data
members & functions and accessing using objects.
n Constructors and destructors in a class,
parameterized constructors
Introducing Functions
Mathematical concept of a function
Definition
n Function is a self-contained block of
code that encapsulates a specific task
or related group of tasks.
Built in functions:-
len() returns length of the argument passed to it
n Each of the built-in functions performs a specific task.
You can define a function that doesn’t take any arguments, but
the parentheses are still required. Both a function definition and
a function call must always include parentheses, even if they’re
empty.
Getting Started
Stub
n Sometimes you may want to define an empty function that
does nothing. This is referred to as a stub, which is usually a
temporary placeholder for a Python function that will be fully
implemented at a later time.
Argument Passing
arguments that are passed
(6, 'bananas', and 1.74) are bound to
the parameters in order, as though
by variable assignment:
n Positional Arguments
n Also called “required arguments”
the order of the arguments in the call must match the order of
the parameters in the definition. There’s nothing to stop you
from specifying positional arguments out of order, of course:
>>>f('bananas', 1.74, 6)
bananas 1.74 cost $6.00
The function may even still run, as it did in the example above,
but it’s very unlikely to produce the correct results.
n With positional arguments, the arguments in the call and the
parameters in the definition must agree not only in order but
in number as well.
n Gives us this…
>>> main()
Happy birthday to you!
Happy birthday to you!
Happy birthday, dear Fred...
Happy birthday to you!
n There’s some duplicated code in the
program! (print("Happy birthday to you!"))
n We can define a function to print out
this line:
def happy():
print("Happy birthday to you!")
variables called
rate = 0.05
addInterest(amount, rate)
line of rate)
balance = newBalance
addInterest
def test():
creates a new amount = 1000
n balance is then
assigned the value
of newBalance.
Functions that Modify
Parameters
n balance now refers def addInterest(balance, rate):
newBalance = balance * (1 + rate)
to the same value as balance = newBalance
returns to test.
def test():
n The local variables, amount = 1000
including the rate = 0.05
def test():
amount = 1000
rate = 0.05
amount = addInterest(amount, rate)
print(amount)
test()
Functions that Modify
Parameters
n Instead of looking at a single account, say we
are writing a program for a bank that deals
with many accounts. We could store the
account balances in a list, then add the
accrued interest to each of the balances in
the list.
n We could update the first balance in the list
with code like:
balances[0] = balances[0] * (1 + rate)
Functions that Modify
Parameters
n This code says, “multiply the value in
the 0th position of the list by (1 + rate)
and store the result back into the 0th
position of the list.”
n A more general way to do this would be
with a loop that goes through positions
0, 1, …, length – 1.
Functions that Modify
Parameters
# addinterest3.py
# Illustrates modification of a mutable parameter (a list).
def test():
amounts = [1000, 2200, 800, 360]
rate = 0.05
addInterest(amounts, 0.05)
print(amounts)
test()
Functions that Modify
Parameters
n Remember, our original code had these
values:
[1000, 2200, 800, 360]
and rate.
amounts = [1000, 2200, 800, 360]
rate = 0.05
addInterest(amounts, 0.05)
n The value of the print(amounts)
variable amounts is
a list object that
contains four int
values.
Functions that Modify
Parameters
Functions that Modify
Parameters
n Next, addInterest def addInterest(balances, rate):
for i in range(len(balances)):
1, …, length –1 and
addInterest(amounts, 0.05)
print(amounts)