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

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

PPT
Py-Slides-3 difficultpythoncoursefforbeginners.ppt
mohamedsamydeveloper
 
PPTX
Functions in c
reshmy12
 
PPTX
Function
Saniati
 
PPTX
Functions
Golda Margret Sheeba J
 
PDF
Python functions
Learnbay Datascience
 
PDF
Python functions
Prof. Dr. K. Adisesha
 
PPT
Powerpoint presentation for Python Functions
BalaSubramanian376976
 
PPTX
Python Functions.pptx
AnuragBharti27
 
PPTX
Python Functions.pptx
AnuragBharti27
 
PPTX
Funtions of c programming. the functions of c helps to clarify all the tops
sameermhr345
 
PPT
functions _
SwatiHans10
 
PPTX
functions.pptx
KavithaChekuri3
 
PPT
python slides introduction interrupt.ppt
Vinod Deenathayalan
 
PPTX
FUNCTIONS IN R PROGRAMMING.pptx
SafnaSaff1
 
PPTX
Functions and Modules.pptx
Ashwini Raut
 
PPTX
Functions
Lakshmi Sarvani Videla
 
PPTX
python-fefedfasdgsgfahfdshdhunctions-190506123237.pptx
avishekpradhan24
 
PPT
Python programming variables and comment
MalligaarjunanN
 
PPTX
Lecture 08.pptx
Mohammad Hassan
 
PDF
Functions_21_22.pdf
paijitk
 
Py-Slides-3 difficultpythoncoursefforbeginners.ppt
mohamedsamydeveloper
 
Functions in c
reshmy12
 
Function
Saniati
 
Python functions
Learnbay Datascience
 
Python functions
Prof. Dr. K. Adisesha
 
Powerpoint presentation for Python Functions
BalaSubramanian376976
 
Python Functions.pptx
AnuragBharti27
 
Python Functions.pptx
AnuragBharti27
 
Funtions of c programming. the functions of c helps to clarify all the tops
sameermhr345
 
functions _
SwatiHans10
 
functions.pptx
KavithaChekuri3
 
python slides introduction interrupt.ppt
Vinod Deenathayalan
 
FUNCTIONS IN R PROGRAMMING.pptx
SafnaSaff1
 
Functions and Modules.pptx
Ashwini Raut
 
python-fefedfasdgsgfahfdshdhunctions-190506123237.pptx
avishekpradhan24
 
Python programming variables and comment
MalligaarjunanN
 
Lecture 08.pptx
Mohammad Hassan
 
Functions_21_22.pdf
paijitk
 

More from rehna9 (7)

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

Recently uploaded (20)

PPTX
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
PDF
People & Earth's Ecosystem -Lesson 2: People & Population
marvinnbustamante1
 
PDF
The-Ever-Evolving-World-of-Science (1).pdf/7TH CLASS CURIOSITY /1ST CHAPTER/B...
Sandeep Swamy
 
PPTX
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
PPTX
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
PDF
LAW OF CONTRACT ( 5 YEAR LLB & UNITARY LLB)- MODULE-3 - LEARN THROUGH PICTURE
APARNA T SHAIL KUMAR
 
PPTX
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
PPTX
SPINA BIFIDA: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
PPTX
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
PPTX
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 
PDF
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
PDF
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
PDF
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
PDF
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
PDF
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
PDF
Lesson 2 - WATER,pH, BUFFERS, AND ACID-BASE.pdf
marvinnbustamante1
 
PPSX
HEALTH ASSESSMENT (Community Health Nursing) - GNM 1st Year
Priyanshu Anand
 
PPTX
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
PPTX
2025 Winter SWAYAM NPTEL & A Student.pptx
Utsav Yagnik
 
PDF
The Different Types of Non-Experimental Research
Thelma Villaflores
 
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
People & Earth's Ecosystem -Lesson 2: People & Population
marvinnbustamante1
 
The-Ever-Evolving-World-of-Science (1).pdf/7TH CLASS CURIOSITY /1ST CHAPTER/B...
Sandeep Swamy
 
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
LAW OF CONTRACT ( 5 YEAR LLB & UNITARY LLB)- MODULE-3 - LEARN THROUGH PICTURE
APARNA T SHAIL KUMAR
 
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
SPINA BIFIDA: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
Lesson 2 - WATER,pH, BUFFERS, AND ACID-BASE.pdf
marvinnbustamante1
 
HEALTH ASSESSMENT (Community Health Nursing) - GNM 1st Year
Priyanshu Anand
 
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
2025 Winter SWAYAM NPTEL & A Student.pptx
Utsav Yagnik
 
The Different Types of Non-Experimental Research
Thelma Villaflores
 
Ad

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.