Course Code – Course Name Prepared by:
Python Programming
Unit III
• Functions
• Program Routines
• Defining Functions
• Calling Value-Returning Functions- Calling Non-
Value-Returning Functions
• Parameter Passing - Keyword Arguments in
Python
• Default Arguments in Python-Variable Scope
Course Code – Course Name Prepared by:
Python Programming
Program 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.
Course Code – Course Name Prepared by:
Python Programming
Program Routine
• When a routine terminates, execution automatically returns to the
point from which it was called. Such routines may be predefined in the
programming language, or designed and implemented by the
programmer.
Course Code – Course Name Prepared by:
Python Programming
Function
• A function is a block of code which only runs
when it is called.
• We can pass data, known as parameters, into
a function.
• A function can return data as a result.
Course Code – Course Name Prepared by:
Python Programming
How function works in Python
Course Code – Course Name Prepared by:
Python Programming
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 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.
Course Code – Course Name Prepared by:
Python Programming
Defining Functions
• Functions are generally defined at the top of a
program.
• Every function must be defined before it is called.
• Actual arguments are the values passed to
functions to be operated on.
• Formal parameters are the “placeholder” names
for the arguments passed.
Course Code – Course Name Prepared by:
Python Programming
Example 1
def my_function():
print("Hello from a function")
my_function()
Output
Hello from a function
Course Code – Course Name Prepared by:
Python Programming
Example 2 - Arguments
def my_function(fname):
print(“Welcome” + fname )
my_function(“SAM")
my_function(“John")
my_function(“William")
Output
Welcome SAM
Welcome John
Welcome William
Course Code – Course Name Prepared by:
Python Programming
Example 2.1 - Number of Arguments
By default, a function must be called with the correct number of
arguments. That is, if your function expects 2 arguments, you
have to call the function with 2 arguments, not more, and not
less.
def my_function(fname, lname):
print(fname + " " + lname)
my_function(“Narendra", “Modi")
Output: Narendar Modi
Course Code – Course Name Prepared by:
Python Programming
Example 2.2 - Number of Arguments
def my_function(fname, lname):
print(fname + " " + lname)
my_function("Emil")
Output:
Course Code – Course Name Prepared by:
Python Programming
Example 2.3 - Arguments
def my_function(child3, child2, child1):
print("The youngest child is " + child3)
my_function(child1 = "Emil", child2 = "Tobias",
child3 = "Linus")
Output
The youngest child is Linus
Course Code – Course Name Prepared by:
Python Programming
Example 3 - Default Parameter Value
def my_function(country = "Norway"):
print("I am from " + country)
my_function("Sweden")
my_function("India")
my_function()
my_function("Brazil")
Output
I am from Sweden
I am from India
I am from Norway
I am from Brazil
Function arguments can have default values in
Python. We can provide a default value to an
argument by using the assignment operator (=).
A default argument is an argument that
assumes a default value if a value is not
provided in the function call for that
argument.
Course Code – Course Name Prepared by:
Python Programming
Example 3.1 - Default Parameter Value
Course Code – Course Name Prepared by:
Python Programming
Example 4 - Passing a List as an Argument
def my_function(food):
for x in food:
print(x)
fruits = ["apple", "banana", "cherry"]
my_function(fruits)
Output
apple
banana
cherry
We can send any data types of argument to a
function (string, number, list, dictionary etc.),
and it will be treated as the same data type
inside the function.
Course Code – Course Name Prepared by:
Python Programming
Example 5 – Return Values
def my_function(x):
return 5 * x
print(my_function(3))
print(my_function(5))
print(my_function(9))
Output
15
25
45
The statement return [expression] exits a function,
optionally passing back an expression to the caller.
A return statement with no arguments is the same as
return None.
Course Code – Course Name Prepared by:
Python Programming
Example 5.1 – Return Values
Course Code – Course Name Prepared by:
Python Programming
Example – Pass statement
Function definitions cannot be empty, but if you for
some reason have a function definition with no
content, put in the pass statement to avoid getting
an error.
def myfunction():
pass
Course Code – Course Name Prepared by:
Python Programming
Categories of Functions
• Value-Returning Functions
• Non-Value-Returning Functions
Course Code – Course Name Prepared by:
Python Programming
Value-Returning Functions
• A value-returning function is a program
routine called for its return value, and is
therefore similar to a mathematical function.
Course Code – Course Name Prepared by:
Python Programming
• 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.
Course Code – Course Name Prepared by:
Python Programming
Non-Value-Returning Functions
• A non-value-returning function is called not for a returned
value, but for its side effects .
• A side effect is an action other than returning a function
value, such as displaying output on the screen.
• There is a fundamental difference in the way that value-
returning and non-value-returning functions are called.
• A call to a value-returning function is an expression, as for the
call to function avg: result = avg(10, 25, 16) * factor.
• When non-value-returning functions are called, the function
call is a statement.
• Since such functions do not have a return value, it is incorrect
to use a call to a non-value-returning function as an
expression
Course Code – Course Name Prepared by:
Python Programming
Non-Value-Returning Functions
• In this example, function displayWelcome is called only for the side-effect
of the screen output produced.
• Finally, every function in Python is technically a value-returning function since
any function that does not explicitly return a function value (via a return
statement) automatically returns the special value None. Such functions as non-
value-returning functions.
A non-value-returning function is a function called for its side effects, and not
for a returned function value.

Functions in python programming and its calling statement

  • 1.
    Course Code –Course Name Prepared by: Python Programming Unit III • Functions • Program Routines • Defining Functions • Calling Value-Returning Functions- Calling Non- Value-Returning Functions • Parameter Passing - Keyword Arguments in Python • Default Arguments in Python-Variable Scope
  • 2.
    Course Code –Course Name Prepared by: Python Programming Program 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.
  • 3.
    Course Code –Course Name Prepared by: Python Programming Program Routine • When a routine terminates, execution automatically returns to the point from which it was called. Such routines may be predefined in the programming language, or designed and implemented by the programmer.
  • 4.
    Course Code –Course Name Prepared by: Python Programming Function • A function is a block of code which only runs when it is called. • We can pass data, known as parameters, into a function. • A function can return data as a result.
  • 5.
    Course Code –Course Name Prepared by: Python Programming How function works in Python
  • 6.
    Course Code –Course Name Prepared by: Python Programming 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 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.
  • 7.
    Course Code –Course Name Prepared by: Python Programming Defining Functions • Functions are generally defined at the top of a program. • Every function must be defined before it is called. • Actual arguments are the values passed to functions to be operated on. • Formal parameters are the “placeholder” names for the arguments passed.
  • 8.
    Course Code –Course Name Prepared by: Python Programming Example 1 def my_function(): print("Hello from a function") my_function() Output Hello from a function
  • 9.
    Course Code –Course Name Prepared by: Python Programming Example 2 - Arguments def my_function(fname): print(“Welcome” + fname ) my_function(“SAM") my_function(“John") my_function(“William") Output Welcome SAM Welcome John Welcome William
  • 10.
    Course Code –Course Name Prepared by: Python Programming Example 2.1 - Number of Arguments By default, a function must be called with the correct number of arguments. That is, if your function expects 2 arguments, you have to call the function with 2 arguments, not more, and not less. def my_function(fname, lname): print(fname + " " + lname) my_function(“Narendra", “Modi") Output: Narendar Modi
  • 11.
    Course Code –Course Name Prepared by: Python Programming Example 2.2 - Number of Arguments def my_function(fname, lname): print(fname + " " + lname) my_function("Emil") Output:
  • 12.
    Course Code –Course Name Prepared by: Python Programming Example 2.3 - Arguments def my_function(child3, child2, child1): print("The youngest child is " + child3) my_function(child1 = "Emil", child2 = "Tobias", child3 = "Linus") Output The youngest child is Linus
  • 13.
    Course Code –Course Name Prepared by: Python Programming Example 3 - Default Parameter Value def my_function(country = "Norway"): print("I am from " + country) my_function("Sweden") my_function("India") my_function() my_function("Brazil") Output I am from Sweden I am from India I am from Norway I am from Brazil Function arguments can have default values in Python. We can provide a default value to an argument by using the assignment operator (=). A default argument is an argument that assumes a default value if a value is not provided in the function call for that argument.
  • 14.
    Course Code –Course Name Prepared by: Python Programming Example 3.1 - Default Parameter Value
  • 15.
    Course Code –Course Name Prepared by: Python Programming Example 4 - Passing a List as an Argument def my_function(food): for x in food: print(x) fruits = ["apple", "banana", "cherry"] my_function(fruits) Output apple banana cherry We can send any data types of argument to a function (string, number, list, dictionary etc.), and it will be treated as the same data type inside the function.
  • 16.
    Course Code –Course Name Prepared by: Python Programming Example 5 – Return Values def my_function(x): return 5 * x print(my_function(3)) print(my_function(5)) print(my_function(9)) Output 15 25 45 The statement return [expression] exits a function, optionally passing back an expression to the caller. A return statement with no arguments is the same as return None.
  • 17.
    Course Code –Course Name Prepared by: Python Programming Example 5.1 – Return Values
  • 18.
    Course Code –Course Name Prepared by: Python Programming Example – Pass statement Function definitions cannot be empty, but if you for some reason have a function definition with no content, put in the pass statement to avoid getting an error. def myfunction(): pass
  • 19.
    Course Code –Course Name Prepared by: Python Programming Categories of Functions • Value-Returning Functions • Non-Value-Returning Functions
  • 20.
    Course Code –Course Name Prepared by: Python Programming Value-Returning Functions • A value-returning function is a program routine called for its return value, and is therefore similar to a mathematical function.
  • 21.
    Course Code –Course Name Prepared by: Python Programming • 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.
  • 22.
    Course Code –Course Name Prepared by: Python Programming Non-Value-Returning Functions • A non-value-returning function is called not for a returned value, but for its side effects . • A side effect is an action other than returning a function value, such as displaying output on the screen. • There is a fundamental difference in the way that value- returning and non-value-returning functions are called. • A call to a value-returning function is an expression, as for the call to function avg: result = avg(10, 25, 16) * factor. • When non-value-returning functions are called, the function call is a statement. • Since such functions do not have a return value, it is incorrect to use a call to a non-value-returning function as an expression
  • 23.
    Course Code –Course Name Prepared by: Python Programming Non-Value-Returning Functions • In this example, function displayWelcome is called only for the side-effect of the screen output produced. • Finally, every function in Python is technically a value-returning function since any function that does not explicitly return a function value (via a return statement) automatically returns the special value None. Such functions as non- value-returning functions. A non-value-returning function is a function called for its side effects, and not for a returned function value.