SlideShare a Scribd company logo
FUNCTIONS
Learning outcomes:
 What is Function?
 Defining a Function
 Calling a Function
 Passing by Reference vs Passing by Value
 Ways to write a function
 Types of functions
 Anonymous Functions
 Recursive Function
Functions
 A function is a set of statements sorted out together to play out a specific task.
 Python has a enormous number of in-built functions and the user can also make their own
functions.
 Functions are utilized to sensibly break our code into less complex parts which become simple to
keep up and comprehend.
 A developer develops a function to abstain from rehashing a similar assignment, or reduce
complexity.
 In Python, function is a group of related statements that perform a specific task.
 Functions help break our program into smaller and modular chunks.
 Furthermore, it avoids repetition and makes code reusable.
Defining a Function
 You can define functions to provide the required functionality.
 Here are simple rules to define a function in Python.
 Function blocks begin with the keyword def followed by the function name and parentheses.
 Any input parameters or arguments should be placed within these parentheses.
 You can also define parameters inside these parentheses.
 The first statement of a function can be an optional statement - the documentation string of
the function or docstring.
 The code block within every function starts with a colon (:) and is indented.
 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.
 Syntax of a Function:
def function_name(parameters): statement(s)
Indentation should be maintained
functions.pptx
Defining a Function
 Example of Function:
def pow (a, b) :
result = a**b
print(a,"raised to the power", b, "is", result)
Here, we created a function called pow(). It takes two arguments, finds the first argument raised
to the power of second argument and prints the result in appropriate format.
Calling a Function
 Defining a function only gives it a name, specifies the parameters that are to
be included in the function and structures the blocks of code.
 Once a function is defined, you may call by it’s function name with the
required arguments/parameters.
 Following is the example to call pow() function:
def pow (a, b) :
result = a**b
print(a,"raised to the power", b, "is", result)
pow(5,3)
Passing by Reference Versus Passing by
Value
 Python utilizes a system, which is known as “Call by Object Reference” or
“Call by assignment”.
 In the event that you pass arguments like whole numbers, strings or tuples to
a function, the passing is like call-by-value because you can not change the
value of the immutable objects being passed to the function.
 Whereas passing mutable objects can be considered as call by reference
because when their values are changed inside the function, then it will also
be reflected outside the function.
Passing by Reference Versus Passing by
Value
 Example:
def changeme( mylist ):
mylist = [1,2,3,4]; # This would assig new reference in mylist
print ("Values inside the function: ", mylist)
# Now you can call changeme function
mylist = [10,20,30];
changeme( mylist );
print ("Values outside the function: ", mylist)
 Example:
def changeme( mylist ):
mylist.append([1,2,3,4]);
print ("Values inside the function: ", mylist)
# Now you can call changeme function
mylist = [10,20,30];
changeme( mylist );
print ("Values outside the function: ", mylist)
Ways to write a function
functions.pptx
functions.pptx
functions.pptx
functions.pptx
functions.pptx
functions.pptx
functions.pptx
functions.pptx
Anonymous Functions
 These functions are called anonymous because they are not declared in the
standard manner by using the def keyword.
 You can use the lambda keyword to create small anonymous functions.
 Lambda forms can take any number of arguments but return just one value in
the form of an expression.
 They cannot contain commands or multiple expressions.
 An anonymous function cannot be a direct call to print because lambda
requires an expression
Syntax:
lambda parameter_1,parameter_2,….parameter_n :
expression
functions.pptx
 Program to add two numbers using function
def sum(n1,n2):
s=n1+n2
return s
x=int(input("Enter the number"))
y=int(input("Enter the number"))
print(sum(x,y))
Program to add two numbers using lambda function :
sum = lambda n1,n2 : n1+n2
x=int(input("Enter the number"))
y=int(input("Enter the number"))
print(sum(x,y)
Recursive Functions
 A function that calls itself is called a recursive function and this technique is known as recursion.
 This special programming technique can be used to solve problems by breaking them into
smaller and simpler sub-problems.
 Recursion is a common mathematical and programming concept. It means that a function calls
itself.
 This has the benefit of meaning that you can loop through data to reach a result.
 An example can help clarify this concept. Let us take the example of finding the factorial of a
number.
 Factorial of a positive integer number is defined as the product of all the integers from 1 to
that number.
 For example, the factorial of 5 (denoted as 5!) will be 1*2*3*4*5 = 120
 This problem of finding factorial of 5 can be broken down into a sub-problem of multiplying the
factorial of 4 with 5.
5! = 5*4!
Or more generally,
n! = n*(n-1)!
Now we can continue this until we reach 0! which is 1.
Thank you
Ad

More Related Content

Similar to functions.pptx (20)

Py-Slides-3 difficultpythoncoursefforbeginners.ppt
Py-Slides-3 difficultpythoncoursefforbeginners.pptPy-Slides-3 difficultpythoncoursefforbeginners.ppt
Py-Slides-3 difficultpythoncoursefforbeginners.ppt
mohamedsamydeveloper
 
Unit 2function in python.pptx
Unit 2function in python.pptxUnit 2function in python.pptx
Unit 2function in python.pptx
vishnupriyapm4
 
ESIT135 Problem Solving Using Python Notes of Unit-2 and Unit-3
ESIT135 Problem Solving Using Python Notes of Unit-2 and Unit-3ESIT135 Problem Solving Using Python Notes of Unit-2 and Unit-3
ESIT135 Problem Solving Using Python Notes of Unit-2 and Unit-3
prasadmutkule1
 
Ch4 functions
Ch4 functionsCh4 functions
Ch4 functions
Hattori Sidek
 
Learn more about the concepts Functions of Python
Learn more about the concepts Functions of PythonLearn more about the concepts Functions of Python
Learn more about the concepts Functions of Python
PrathamKandari
 
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptxUnit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
vekariyakashyap
 
Python programming variables and comment
Python programming variables and commentPython programming variables and comment
Python programming variables and comment
MalligaarjunanN
 
All chapters C++ - Copy.pdfyttttttttttttttttttttttttttttt
All chapters C++ - Copy.pdfytttttttttttttttttttttttttttttAll chapters C++ - Copy.pdfyttttttttttttttttttttttttttttt
All chapters C++ - Copy.pdfyttttttttttttttttttttttttttttt
jacobdiriba
 
Chapter 1.ppt
Chapter 1.pptChapter 1.ppt
Chapter 1.ppt
ethiouniverse
 
Python programming - Functions and list and tuples
Python programming - Functions and list and tuplesPython programming - Functions and list and tuples
Python programming - Functions and list and tuples
MalligaarjunanN
 
Functions
FunctionsFunctions
Functions
Septi Ratnasari
 
C programming language working with functions 1
C programming language working with functions 1C programming language working with functions 1
C programming language working with functions 1
Jeevan Raj
 
Python Function.pdf
Python Function.pdfPython Function.pdf
Python Function.pdf
NehaSpillai1
 
FUNCTIONS IN R PROGRAMMING.pptx
FUNCTIONS IN R PROGRAMMING.pptxFUNCTIONS IN R PROGRAMMING.pptx
FUNCTIONS IN R PROGRAMMING.pptx
SafnaSaff1
 
Python_Unit_2.pdf
Python_Unit_2.pdfPython_Unit_2.pdf
Python_Unit_2.pdf
alaparthi
 
Module 3-Functions
Module 3-FunctionsModule 3-Functions
Module 3-Functions
nikshaikh786
 
Python functions
Python functionsPython functions
Python functions
Prof. Dr. K. Adisesha
 
python slides introduction interrupt.ppt
python slides introduction interrupt.pptpython slides introduction interrupt.ppt
python slides introduction interrupt.ppt
Vinod Deenathayalan
 
programlama fonksiyonlar c++ hjhjghjv jg
programlama fonksiyonlar c++ hjhjghjv jgprogramlama fonksiyonlar c++ hjhjghjv jg
programlama fonksiyonlar c++ hjhjghjv jg
uleAmet
 
Amit user defined functions xi (2)
Amit  user defined functions xi (2)Amit  user defined functions xi (2)
Amit user defined functions xi (2)
Arpit Meena
 
Py-Slides-3 difficultpythoncoursefforbeginners.ppt
Py-Slides-3 difficultpythoncoursefforbeginners.pptPy-Slides-3 difficultpythoncoursefforbeginners.ppt
Py-Slides-3 difficultpythoncoursefforbeginners.ppt
mohamedsamydeveloper
 
Unit 2function in python.pptx
Unit 2function in python.pptxUnit 2function in python.pptx
Unit 2function in python.pptx
vishnupriyapm4
 
ESIT135 Problem Solving Using Python Notes of Unit-2 and Unit-3
ESIT135 Problem Solving Using Python Notes of Unit-2 and Unit-3ESIT135 Problem Solving Using Python Notes of Unit-2 and Unit-3
ESIT135 Problem Solving Using Python Notes of Unit-2 and Unit-3
prasadmutkule1
 
Learn more about the concepts Functions of Python
Learn more about the concepts Functions of PythonLearn more about the concepts Functions of Python
Learn more about the concepts Functions of Python
PrathamKandari
 
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptxUnit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
vekariyakashyap
 
Python programming variables and comment
Python programming variables and commentPython programming variables and comment
Python programming variables and comment
MalligaarjunanN
 
All chapters C++ - Copy.pdfyttttttttttttttttttttttttttttt
All chapters C++ - Copy.pdfytttttttttttttttttttttttttttttAll chapters C++ - Copy.pdfyttttttttttttttttttttttttttttt
All chapters C++ - Copy.pdfyttttttttttttttttttttttttttttt
jacobdiriba
 
Python programming - Functions and list and tuples
Python programming - Functions and list and tuplesPython programming - Functions and list and tuples
Python programming - Functions and list and tuples
MalligaarjunanN
 
C programming language working with functions 1
C programming language working with functions 1C programming language working with functions 1
C programming language working with functions 1
Jeevan Raj
 
Python Function.pdf
Python Function.pdfPython Function.pdf
Python Function.pdf
NehaSpillai1
 
FUNCTIONS IN R PROGRAMMING.pptx
FUNCTIONS IN R PROGRAMMING.pptxFUNCTIONS IN R PROGRAMMING.pptx
FUNCTIONS IN R PROGRAMMING.pptx
SafnaSaff1
 
Python_Unit_2.pdf
Python_Unit_2.pdfPython_Unit_2.pdf
Python_Unit_2.pdf
alaparthi
 
Module 3-Functions
Module 3-FunctionsModule 3-Functions
Module 3-Functions
nikshaikh786
 
python slides introduction interrupt.ppt
python slides introduction interrupt.pptpython slides introduction interrupt.ppt
python slides introduction interrupt.ppt
Vinod Deenathayalan
 
programlama fonksiyonlar c++ hjhjghjv jg
programlama fonksiyonlar c++ hjhjghjv jgprogramlama fonksiyonlar c++ hjhjghjv jg
programlama fonksiyonlar c++ hjhjghjv jg
uleAmet
 
Amit user defined functions xi (2)
Amit  user defined functions xi (2)Amit  user defined functions xi (2)
Amit user defined functions xi (2)
Arpit Meena
 

Recently uploaded (20)

211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf
211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf
211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf
inmishra17121973
 
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptxLidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
RishavKumar530754
 
five-year-soluhhhhhhhhhhhhhhhhhtions.pdf
five-year-soluhhhhhhhhhhhhhhhhhtions.pdffive-year-soluhhhhhhhhhhhhhhhhhtions.pdf
five-year-soluhhhhhhhhhhhhhhhhhtions.pdf
AdityaSharma944496
 
Data Structures_Searching and Sorting.pptx
Data Structures_Searching and Sorting.pptxData Structures_Searching and Sorting.pptx
Data Structures_Searching and Sorting.pptx
RushaliDeshmukh2
 
Introduction to Zoomlion Earthmoving.pptx
Introduction to Zoomlion Earthmoving.pptxIntroduction to Zoomlion Earthmoving.pptx
Introduction to Zoomlion Earthmoving.pptx
AS1920
 
DSP and MV the Color image processing.ppt
DSP and MV the  Color image processing.pptDSP and MV the  Color image processing.ppt
DSP and MV the Color image processing.ppt
HafizAhamed8
 
International Journal of Distributed and Parallel systems (IJDPS)
International Journal of Distributed and Parallel systems (IJDPS)International Journal of Distributed and Parallel systems (IJDPS)
International Journal of Distributed and Parallel systems (IJDPS)
samueljackson3773
 
Mathematical foundation machine learning.pdf
Mathematical foundation machine learning.pdfMathematical foundation machine learning.pdf
Mathematical foundation machine learning.pdf
TalhaShahid49
 
ADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITY
ADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITYADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITY
ADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITY
ijscai
 
Reagent dosing (Bredel) presentation.pptx
Reagent dosing (Bredel) presentation.pptxReagent dosing (Bredel) presentation.pptx
Reagent dosing (Bredel) presentation.pptx
AlejandroOdio
 
Process Parameter Optimization for Minimizing Springback in Cold Drawing Proc...
Process Parameter Optimization for Minimizing Springback in Cold Drawing Proc...Process Parameter Optimization for Minimizing Springback in Cold Drawing Proc...
Process Parameter Optimization for Minimizing Springback in Cold Drawing Proc...
Journal of Soft Computing in Civil Engineering
 
Explainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptx
Explainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptxExplainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptx
Explainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptx
MahaveerVPandit
 
The Gaussian Process Modeling Module in UQLab
The Gaussian Process Modeling Module in UQLabThe Gaussian Process Modeling Module in UQLab
The Gaussian Process Modeling Module in UQLab
Journal of Soft Computing in Civil Engineering
 
QA/QC Manager (Quality management Expert)
QA/QC Manager (Quality management Expert)QA/QC Manager (Quality management Expert)
QA/QC Manager (Quality management Expert)
rccbatchplant
 
RICS Membership-(The Royal Institution of Chartered Surveyors).pdf
RICS Membership-(The Royal Institution of Chartered Surveyors).pdfRICS Membership-(The Royal Institution of Chartered Surveyors).pdf
RICS Membership-(The Royal Institution of Chartered Surveyors).pdf
MohamedAbdelkader115
 
Data Structures_Introduction to algorithms.pptx
Data Structures_Introduction to algorithms.pptxData Structures_Introduction to algorithms.pptx
Data Structures_Introduction to algorithms.pptx
RushaliDeshmukh2
 
DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...
DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...
DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...
charlesdick1345
 
ELectronics Boards & Product Testing_Shiju.pdf
ELectronics Boards & Product Testing_Shiju.pdfELectronics Boards & Product Testing_Shiju.pdf
ELectronics Boards & Product Testing_Shiju.pdf
Shiju Jacob
 
Artificial Intelligence (AI) basics.pptx
Artificial Intelligence (AI) basics.pptxArtificial Intelligence (AI) basics.pptx
Artificial Intelligence (AI) basics.pptx
aditichinar
 
new ppt artificial intelligence historyyy
new ppt artificial intelligence historyyynew ppt artificial intelligence historyyy
new ppt artificial intelligence historyyy
PianoPianist
 
211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf
211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf
211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf
inmishra17121973
 
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptxLidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
RishavKumar530754
 
five-year-soluhhhhhhhhhhhhhhhhhtions.pdf
five-year-soluhhhhhhhhhhhhhhhhhtions.pdffive-year-soluhhhhhhhhhhhhhhhhhtions.pdf
five-year-soluhhhhhhhhhhhhhhhhhtions.pdf
AdityaSharma944496
 
Data Structures_Searching and Sorting.pptx
Data Structures_Searching and Sorting.pptxData Structures_Searching and Sorting.pptx
Data Structures_Searching and Sorting.pptx
RushaliDeshmukh2
 
Introduction to Zoomlion Earthmoving.pptx
Introduction to Zoomlion Earthmoving.pptxIntroduction to Zoomlion Earthmoving.pptx
Introduction to Zoomlion Earthmoving.pptx
AS1920
 
DSP and MV the Color image processing.ppt
DSP and MV the  Color image processing.pptDSP and MV the  Color image processing.ppt
DSP and MV the Color image processing.ppt
HafizAhamed8
 
International Journal of Distributed and Parallel systems (IJDPS)
International Journal of Distributed and Parallel systems (IJDPS)International Journal of Distributed and Parallel systems (IJDPS)
International Journal of Distributed and Parallel systems (IJDPS)
samueljackson3773
 
Mathematical foundation machine learning.pdf
Mathematical foundation machine learning.pdfMathematical foundation machine learning.pdf
Mathematical foundation machine learning.pdf
TalhaShahid49
 
ADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITY
ADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITYADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITY
ADVXAI IN MALWARE ANALYSIS FRAMEWORK: BALANCING EXPLAINABILITY WITH SECURITY
ijscai
 
Reagent dosing (Bredel) presentation.pptx
Reagent dosing (Bredel) presentation.pptxReagent dosing (Bredel) presentation.pptx
Reagent dosing (Bredel) presentation.pptx
AlejandroOdio
 
Explainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptx
Explainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptxExplainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptx
Explainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptx
MahaveerVPandit
 
QA/QC Manager (Quality management Expert)
QA/QC Manager (Quality management Expert)QA/QC Manager (Quality management Expert)
QA/QC Manager (Quality management Expert)
rccbatchplant
 
RICS Membership-(The Royal Institution of Chartered Surveyors).pdf
RICS Membership-(The Royal Institution of Chartered Surveyors).pdfRICS Membership-(The Royal Institution of Chartered Surveyors).pdf
RICS Membership-(The Royal Institution of Chartered Surveyors).pdf
MohamedAbdelkader115
 
Data Structures_Introduction to algorithms.pptx
Data Structures_Introduction to algorithms.pptxData Structures_Introduction to algorithms.pptx
Data Structures_Introduction to algorithms.pptx
RushaliDeshmukh2
 
DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...
DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...
DATA-DRIVEN SHOULDER INVERSE KINEMATICS YoungBeom Kim1 , Byung-Ha Park1 , Kwa...
charlesdick1345
 
ELectronics Boards & Product Testing_Shiju.pdf
ELectronics Boards & Product Testing_Shiju.pdfELectronics Boards & Product Testing_Shiju.pdf
ELectronics Boards & Product Testing_Shiju.pdf
Shiju Jacob
 
Artificial Intelligence (AI) basics.pptx
Artificial Intelligence (AI) basics.pptxArtificial Intelligence (AI) basics.pptx
Artificial Intelligence (AI) basics.pptx
aditichinar
 
new ppt artificial intelligence historyyy
new ppt artificial intelligence historyyynew ppt artificial intelligence historyyy
new ppt artificial intelligence historyyy
PianoPianist
 
Ad

functions.pptx

  • 2. Learning outcomes:  What is Function?  Defining a Function  Calling a Function  Passing by Reference vs Passing by Value  Ways to write a function  Types of functions  Anonymous Functions  Recursive Function
  • 3. Functions  A function is a set of statements sorted out together to play out a specific task.  Python has a enormous number of in-built functions and the user can also make their own functions.  Functions are utilized to sensibly break our code into less complex parts which become simple to keep up and comprehend.  A developer develops a function to abstain from rehashing a similar assignment, or reduce complexity.  In Python, function is a group of related statements that perform a specific task.  Functions help break our program into smaller and modular chunks.  Furthermore, it avoids repetition and makes code reusable.
  • 4. Defining a Function  You can define functions to provide the required functionality.  Here are simple rules to define a function in Python.  Function blocks begin with the keyword def followed by the function name and parentheses.  Any input parameters or arguments should be placed within these parentheses.  You can also define parameters inside these parentheses.  The first statement of a function can be an optional statement - the documentation string of the function or docstring.
  • 5.  The code block within every function starts with a colon (:) and is indented.  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.  Syntax of a Function: def function_name(parameters): statement(s) Indentation should be maintained
  • 7. Defining a Function  Example of Function: def pow (a, b) : result = a**b print(a,"raised to the power", b, "is", result) Here, we created a function called pow(). It takes two arguments, finds the first argument raised to the power of second argument and prints the result in appropriate format.
  • 8. Calling a Function  Defining a function only gives it a name, specifies the parameters that are to be included in the function and structures the blocks of code.  Once a function is defined, you may call by it’s function name with the required arguments/parameters.  Following is the example to call pow() function: def pow (a, b) : result = a**b print(a,"raised to the power", b, "is", result) pow(5,3)
  • 9. Passing by Reference Versus Passing by Value  Python utilizes a system, which is known as “Call by Object Reference” or “Call by assignment”.  In the event that you pass arguments like whole numbers, strings or tuples to a function, the passing is like call-by-value because you can not change the value of the immutable objects being passed to the function.  Whereas passing mutable objects can be considered as call by reference because when their values are changed inside the function, then it will also be reflected outside the function.
  • 10. Passing by Reference Versus Passing by Value  Example: def changeme( mylist ): mylist = [1,2,3,4]; # This would assig new reference in mylist print ("Values inside the function: ", mylist) # Now you can call changeme function mylist = [10,20,30]; changeme( mylist ); print ("Values outside the function: ", mylist)
  • 11.  Example: def changeme( mylist ): mylist.append([1,2,3,4]); print ("Values inside the function: ", mylist) # Now you can call changeme function mylist = [10,20,30]; changeme( mylist ); print ("Values outside the function: ", mylist)
  • 12. Ways to write a function
  • 21. Anonymous Functions  These functions are called anonymous because they are not declared in the standard manner by using the def keyword.  You can use the lambda keyword to create small anonymous functions.  Lambda forms can take any number of arguments but return just one value in the form of an expression.  They cannot contain commands or multiple expressions.  An anonymous function cannot be a direct call to print because lambda requires an expression
  • 24.  Program to add two numbers using function def sum(n1,n2): s=n1+n2 return s x=int(input("Enter the number")) y=int(input("Enter the number")) print(sum(x,y)) Program to add two numbers using lambda function : sum = lambda n1,n2 : n1+n2 x=int(input("Enter the number")) y=int(input("Enter the number")) print(sum(x,y)
  • 25. Recursive Functions  A function that calls itself is called a recursive function and this technique is known as recursion.  This special programming technique can be used to solve problems by breaking them into smaller and simpler sub-problems.  Recursion is a common mathematical and programming concept. It means that a function calls itself.  This has the benefit of meaning that you can loop through data to reach a result.  An example can help clarify this concept. Let us take the example of finding the factorial of a number.  Factorial of a positive integer number is defined as the product of all the integers from 1 to that number.  For example, the factorial of 5 (denoted as 5!) will be 1*2*3*4*5 = 120
  • 26.  This problem of finding factorial of 5 can be broken down into a sub-problem of multiplying the factorial of 4 with 5. 5! = 5*4! Or more generally, n! = n*(n-1)! Now we can continue this until we reach 0! which is 1.