SlideShare a Scribd company logo
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.

More Related Content

PPT
functions _
PDF
functions- best.pdf
PPTX
UNIT 3 python.pptx
PPTX
Functions in Python Programming Language
PPTX
Lecture 08.pptx
PPT
functions modules and exceptions handlings.ppt
PDF
Notes5
PPTX
2_3 Functions 5d.pptx2_3 Functions 5d.pptx
functions _
functions- best.pdf
UNIT 3 python.pptx
Functions in Python Programming Language
Lecture 08.pptx
functions modules and exceptions handlings.ppt
Notes5
2_3 Functions 5d.pptx2_3 Functions 5d.pptx

Similar to Functions in python programming and its calling statement (20)

PPTX
Functions in Python with all type of arguments
PPTX
Function in Python function in python.pptx
PPTX
JNTUK python programming python unit 3.pptx
PPTX
Python programming - Functions and list and tuples
PPTX
Learn more about the concepts Functions of Python
PPT
Powerpoint presentation for Python Functions
PPT
python slides introduction interrupt.ppt
PDF
3-Python Functions.pdf in simple.........
PPTX
function in python programming languges .pptx
PPT
Python programming variables and comment
PPTX
Python for Data Science function third module ppt.pptx
PDF
Python functions
PPT
Py-Slides-3 difficultpythoncoursefforbeginners.ppt
PPTX
Working with functions.pptx. Hb.
PPTX
functions new.pptx
PPTX
04. WORKING WITH FUNCTIONS-2 (1).pptx
PPTX
Working with functions the minumum ppt.pptx
PPTX
Functions in python slide share
PPTX
FUNCTIONS.pptx
PPTX
UNIT-02-pythonfunctions python function using detehdjsjehhdjejdhdjdjdjddjdhdhhd
Functions in Python with all type of arguments
Function in Python function in python.pptx
JNTUK python programming python unit 3.pptx
Python programming - Functions and list and tuples
Learn more about the concepts Functions of Python
Powerpoint presentation for Python Functions
python slides introduction interrupt.ppt
3-Python Functions.pdf in simple.........
function in python programming languges .pptx
Python programming variables and comment
Python for Data Science function third module ppt.pptx
Python functions
Py-Slides-3 difficultpythoncoursefforbeginners.ppt
Working with functions.pptx. Hb.
functions new.pptx
04. WORKING WITH FUNCTIONS-2 (1).pptx
Working with functions the minumum ppt.pptx
Functions in python slide share
FUNCTIONS.pptx
UNIT-02-pythonfunctions python function using detehdjsjehhdjejdhdjdjdjddjdhdhhd
Ad

More from rehna9 (7)

PPT
Software Design – Overview – Characteristics – Cohesion & Coupling – Layered ...
PPT
software engineering evolution and all of its models
PPT
CSC UNIT II IN THE SUBJECT CLIENT SERVER COMPUTING
PPT
CSC UNIT1 CONTENT IN THE SUBJECT CLIENT SERVER COMPUTING
PPTX
STRUCTURE OF PAGE TABLE IN OPERATING SYSTEM
PPT
4_25655_SE291_2020_1__2_1_Lecture 3 - Software Process Models.ppt
PPT
PHP CONDITIONAL STATEMENTS AND LOOPING.ppt
Software Design – Overview – Characteristics – Cohesion & Coupling – Layered ...
software engineering evolution and all of its models
CSC UNIT II IN THE SUBJECT CLIENT SERVER COMPUTING
CSC UNIT1 CONTENT IN THE SUBJECT CLIENT SERVER COMPUTING
STRUCTURE OF PAGE TABLE IN OPERATING SYSTEM
4_25655_SE291_2020_1__2_1_Lecture 3 - Software Process Models.ppt
PHP CONDITIONAL STATEMENTS AND LOOPING.ppt
Ad

Recently uploaded (20)

PDF
Sports Quiz easy sports quiz sports quiz
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
Computing-Curriculum for Schools in Ghana
PPTX
PPH.pptx obstetrics and gynecology in nursing
PPTX
master seminar digital applications in india
PPTX
Pharma ospi slides which help in ospi learning
PPTX
Cell Types and Its function , kingdom of life
PDF
VCE English Exam - Section C Student Revision Booklet
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
Insiders guide to clinical Medicine.pdf
PPTX
GDM (1) (1).pptx small presentation for students
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
Sports Quiz easy sports quiz sports quiz
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Computing-Curriculum for Schools in Ghana
PPH.pptx obstetrics and gynecology in nursing
master seminar digital applications in india
Pharma ospi slides which help in ospi learning
Cell Types and Its function , kingdom of life
VCE English Exam - Section C Student Revision Booklet
Microbial diseases, their pathogenesis and prophylaxis
Renaissance Architecture: A Journey from Faith to Humanism
102 student loan defaulters named and shamed – Is someone you know on the list?
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Insiders guide to clinical Medicine.pdf
GDM (1) (1).pptx small presentation for students
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
2.FourierTransform-ShortQuestionswithAnswers.pdf
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Module 4: Burden of Disease Tutorial Slides S2 2025

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.