0% found this document useful (0 votes)
9 views25 pages

Chapter 5

The document explains the concept of functions in Python, detailing how to define and call both value-returning and non-value-returning functions. It covers parameter passing, keyword arguments, default arguments, and the distinction between local and global variables. Additionally, it emphasizes the importance of defining functions before calling them and provides examples of function usage.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views25 pages

Chapter 5

The document explains the concept of functions in Python, detailing how to define and call both value-returning and non-value-returning functions. It covers parameter passing, keyword arguments, default arguments, and the distinction between local and global variables. Additionally, it emphasizes the importance of defining functions before calling them and provides examples of function usage.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25

Function

What is Fundamental routine?


• A program routine is a named group of
instructions that accomplishes some task.
• A routine may be invoked (called) as many
times as needed in a given program.
• A function is Python’s version of a program
routine.
Defining Functions
• The first line of a function definition is the function
header .
• A function header starts with the keyword def, followed
by an identifier (avg), which is the function’s name.
• The function name is followed by a comma-separated
(possibly empty) list of identifiers (n1, n2, n3) called
formal parameters , or simply “parameters.” Following
the parameter list is a colon ( : ).
• Following the function header is the body of the function,
a suite (program block) containing the function’s
instructions.
• As with all suites, the statements must be indented at the
same level, relative to the function header.
• The number of items in a parameter list indicates
the number of values that must be passed to the
function, called actual arguments (or simply
“arguments”), such as the variables num1, num2,
and num3 below.
• >>> num1 5 10
• >>> num2 5 25
• >>> num3 5 16
• >>> avg(num1,num2,num3)
• Functions are generally defined at the top of a
program. However, every function must be defined
before it is called .
Value-Returning Functions
• A value-returning function is a program routine called for its return value,
and is therefore similar to a mathematical function.
• Take the simple mathematical function f(x) = 2x. In this notation, “x”
• stands for any numeric value that function f may be applied to, for
example, f(2) = 2x = 4.
• Program functions are similarly used
• Function avg takes three arguments (n1, n2, and n3) and returns the
average of the three.
• The function call avg(10, 25, 16), therefore, is an expression that evaluates
to the returned function value.
• This is indicated in the function’s return statement of the form return expr ,
where expr may be any expression.
• Next, we look at a second form of program routine called for a purpose
other than a returned function value.
Non-Value-Returning Functions
• A non-value-returning function is a function
called for its side effects, and not for a
returned function value.
• >>> def hello(name):
• print('Hello', name + '!')

• >>> name = 'John‘


>>> hello(name)
Calling Value-Returning Functions
• we apply built-in function max to a list of
integers, num_list. Examples of additional
allowable forms of function calls are given
below.
• (a) result =max(num_list1) * max(num_list2)
• (b) result = abs(max(num_list))
• (c) if max(num_list) > 10: ...
• (d) print('Largest value in num_list is ',
max(num_list))
• function definition
def maxmin(num_list):
return (max(num_list), min(num_list))
• function use
weekly_temps = [45, 30, 52, 58, 62, 48, 49]
(a) highlow_temps = maxmin(weekly_temps)
(b) high, low = maxmin(weekly_temps)
Calling Non-Value-Returning Functions
• def sayHello():
print('Hello!')
• >>> sayHello()
Parameter Passing
• Parameter passing is the process of passing
arguments to a function.
• Actual arguments are the values passed to a
function’s formal parameters to be operated
on.
Summation of 2 numbers using functions

• >>> def sum(a,b)


c=a+b
print(c)

>>>sum(2,3)
>>>5
• The correspondence of actual arguments and
formal parameters is determined by the order
of the arguments passed, and not their names.
Mutable vs. Immutable Arguments
• def avg(n1, n2, n3):
• avg(10, 25, 40)
• In this case, literal values are passed as the
arguments to function avg. When variables are
passed as actual arguments, however, as
shown below,
• def avg( n1 , n2 , n3 ):
• avg( num1 , num2 , num3 )
• def countDown(n):
while n >= 0:
if (n != 0):
print(n, '..', end='')
else:
print(n)
n=n–1
Calling a function
countDown(4)
N=int(input(”enter a no”))
countDown(N)
Keyword Arguments in Python
• A positional argument is an argument that is
assigned to a particular parameter based on
its position in the argument list.
• A keyword argument is an argument that is
specified by parameter name.
• note that keyword arguments, by convention,
do not have a space before or after the equal
sign
• def mortgage_rate(amount, rate, term):
• monthly_payment = mortgage_rate(350000,
0.06, 20)
• def mortgage_rate(amount, rate, term):
• monthly_payment = mortgage_rate(rate=0.06,
term=20, amount=350000)
Default Arguments in Python
• def mortgage_rate(amount, rate, term=20)
• monthly_payment=mortgage_rate(35000,
0.62)
• A default argument is an argument that can
be optionally provided in a given function
call.
• When not provided, the corresponding
parameter provides a default value.
Variable Scope
• Local Scope and Local Variables
A local variable is a variable that is only
accessible from within the function it resides. Such
variables are said to have local scope.
• Global Variables and Global Scope
A global variable is a variable defined outside
of any function definition. Such variables are said
to have global scope. The use of global variables is
considered bad programming practice.
Inaccessibility of Local Variables
Access to Value of Global Variable

You might also like