SlideShare a Scribd company logo
AXL IT Academy
Functions
In Python, a function can be defined as a named block of code. We use
functions mainly to avoid code repetition. We can define that action only once
using a function and call that function whenever required to do the same
activity.
The benefit of using a function is code reusability and modularity. Function
improves efficiency and reduces errors because of the reusability of a code.
Once we create a function, we can call it anywhere and anytime.
Creating a Function
In Python a function is defined using the def keyword. The “def” stands for
define. It is used for telling the interpreter that it is a user defined function. The
“def” is followed by function name, which is followed by parameters(Optional).
Define the function body with a block of code.
Syntax:
functionNameHere(parameters):
def
#your code here
AXL IT Academy
Note: There is no need to specify curly braces for the function body, in python.
Only indentation is needed to separate code blocks.
Calling a Function
To call a function, use the function name followed by parenthesis, which is
followed by arguments (if any).
:
Note 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.
Arguments
AXL IT Academy
Arguments are specified after the function name, inside the parentheses, for
passing information into function. You can add as many arguments as you
want, just separate them with a comma.
Parameters and Arguments
1. A parameter is the variable listed inside the parentheses in the function
definition.
2. An argument is the value that is sent to the function when it is called.
Recursion
Recursion means that a defined function calls itself. When written correctly
recursion can be a very useful and effective.
AXL IT Academy
Example:
#Recursion function which prints upto 99 without for/while
recur (k):
def
if(k <100):
(k)
print
k+=1
( k )
recur
#calling recur
recur (1)
Function Assignment
We can assign a function to a variable in the same way as we assign a value
to it. You can even use those variables to call the function as well.
def proc():
return "Welcome"
procAlias= proc
print(proc)
print(procAlias)
print(proc ())
AXL IT Academy
print(procAlias())
Return in Function
Return means the task is completed and now you need to return to where you
came, with the specified data. You can return any type of data, you can even
return a function. This should be the last statement in a function.
def proc():
return "Bon Voyage"
print(proc ())
Inner Function
A function which is defined inside another function is known as inner function or
nested function. Nested functions are able to access variables of the enclosing
scope.
Types of function arguments.
There are various ways to use arguments in a function. In Python, we have the
following 4 types of function arguments.
AXL IT Academy
1. Default argument
2. Keyword arguments (named arguments)
3. Positional arguments
4. Arbitrary arguments (variable-length arguments *args and **kwargs)
AXL IT Academy
Default Arguments
Default arguments are arguments those have default values. We assign
default values to the argument using the ‘=’ (assignment) operator at the time
of function definition. You can define a function with any number of default
arguments.
The default value of an argument will be used inside a function if we do not
pass a value to that argument at the time of the function call. Due to this, the
default arguments become optional during the function call.
It overrides the default value if we provide a value to the default arguments
during function calls.
Keyword Arguments
AXL IT Academy
Usually, at the time of the function call, values get assigned to the arguments
according to their position. So we must pass values in the same sequence
defined in a function definition.
For example, when we call show('dass', 19), the value “dass” gets assigned to
the argument name, and similarly, 19 to age and so on as per the sequence.
We can alter this behavior using a keyword argument.
Keyword arguments are those arguments where values get assigned to the
arguments by their keyword (name) when the function is called. It is preceded
by the variable name and an (=) assignment operator. The Keyword Argument
is also called a named argument.
Change the sequence of keyword arguments
Also, you can change the sequence of keyword arguments by using their name
in function calls.
Python allows functions to be called using keyword arguments. But all the
keyword arguments should match the parameters in the function definition.
When we call functions in this way, the order (position) of the arguments can
be changed.
Positional Arguments
Positional arguments are arguments that can be called by their position in the
function definition
By default, Python functions are called using the positional arguments.
Variable-length arguments
AXL IT Academy
We use variable-length arguments if we don’t know the number of arguments
needed for the function in advance.
Types of Arbitrary Arguments:
 arbitrary positional arguments (*args)
 arbitrary keyword arguments (**kwargs)
The *args and **kwargs allow you to pass multiple positional arguments or
keyword arguments to a function.
Arbitrary positional arguments (*args)
We can declare a variable-length argument with the * (asterisk) symbol. Place
an asterisk (*) before a parameter in the function definition to define an
arbitrary positional argument.
we can pass multiple arguments to the function. Internally all these values are
represented in the form of a tuple.

More Related Content

PDF
Python functions
Learnbay Datascience
 
PPTX
Learn more about the concepts Functions of Python
PrathamKandari
 
PPTX
Functions in Python Programming Language
BeulahS2
 
PPT
Py-Slides-3 difficultpythoncoursefforbeginners.ppt
mohamedsamydeveloper
 
PPTX
INTRODUCTION TO PYTHON PROGRMMING AND FUNCTIONS
KalaivaniD12
 
PPT
Powerpoint presentation for Python Functions
BalaSubramanian376976
 
PPTX
Functions and Modules.pptx
Ashwini Raut
 
PPTX
Functions in python slide share
Devashish Kumar
 
Python functions
Learnbay Datascience
 
Learn more about the concepts Functions of Python
PrathamKandari
 
Functions in Python Programming Language
BeulahS2
 
Py-Slides-3 difficultpythoncoursefforbeginners.ppt
mohamedsamydeveloper
 
INTRODUCTION TO PYTHON PROGRMMING AND FUNCTIONS
KalaivaniD12
 
Powerpoint presentation for Python Functions
BalaSubramanian376976
 
Functions and Modules.pptx
Ashwini Raut
 
Functions in python slide share
Devashish Kumar
 

Similar to functionnotes.pdf (20)

PPTX
Python programming - Functions and list and tuples
MalligaarjunanN
 
PPT
python slides introduction interrupt.ppt
Vinod Deenathayalan
 
PPTX
functions in python By Eng. Osama Ghandour الدوال فى البايثون مع مهندس اسامه ...
Osama Ghandour Geris
 
PDF
functions notes.pdf python functions and opp
KirtiGarg71
 
PPTX
Lecture 08.pptx
Mohammad Hassan
 
PDF
Python Function.pdf
NehaSpillai1
 
PPTX
04. WORKING WITH FUNCTIONS-2 (1).pptx
Manas40552
 
PPTX
Python Functions.pptx
AnuragBharti27
 
PPTX
Python Functions.pptx
AnuragBharti27
 
PDF
ESIT135 Problem Solving Using Python Notes of Unit-2 and Unit-3
prasadmutkule1
 
PPTX
Functions in C++
home
 
PPTX
functions in python language to run programming
VineelaThonduri
 
PPTX
UNIT 3 python.pptx
TKSanthoshRao
 
PPT
Python programming variables and comment
MalligaarjunanN
 
PPTX
Functions in C
Kamal Acharya
 
PPTX
function of C.pptx
shivas379526
 
PPTX
use of Functions to write python program.pptx
rahulsinghsikarwar2
 
PPT
functions modules and exceptions handlings.ppt
Rajasekhar364622
 
PDF
Chapter Functions for grade 12 computer Science
KrithikaTM
 
PDF
Userdefined functions brief explaination.pdf
DeeptiMalhotra19
 
Python programming - Functions and list and tuples
MalligaarjunanN
 
python slides introduction interrupt.ppt
Vinod Deenathayalan
 
functions in python By Eng. Osama Ghandour الدوال فى البايثون مع مهندس اسامه ...
Osama Ghandour Geris
 
functions notes.pdf python functions and opp
KirtiGarg71
 
Lecture 08.pptx
Mohammad Hassan
 
Python Function.pdf
NehaSpillai1
 
04. WORKING WITH FUNCTIONS-2 (1).pptx
Manas40552
 
Python Functions.pptx
AnuragBharti27
 
Python Functions.pptx
AnuragBharti27
 
ESIT135 Problem Solving Using Python Notes of Unit-2 and Unit-3
prasadmutkule1
 
Functions in C++
home
 
functions in python language to run programming
VineelaThonduri
 
UNIT 3 python.pptx
TKSanthoshRao
 
Python programming variables and comment
MalligaarjunanN
 
Functions in C
Kamal Acharya
 
function of C.pptx
shivas379526
 
use of Functions to write python program.pptx
rahulsinghsikarwar2
 
functions modules and exceptions handlings.ppt
Rajasekhar364622
 
Chapter Functions for grade 12 computer Science
KrithikaTM
 
Userdefined functions brief explaination.pdf
DeeptiMalhotra19
 
Ad

Recently uploaded (20)

PPTX
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 
PDF
GDG Cloud Munich - Intro - Luiz Carneiro - #BuildWithAI - July - Abdel.pdf
Luiz Carneiro
 
PPTX
OA presentation.pptx OA presentation.pptx
pateldhruv002338
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PDF
Make GenAI investments go further with the Dell AI Factory
Principled Technologies
 
PDF
Get More from Fiori Automation - What’s New, What Works, and What’s Next.pdf
Precisely
 
PPTX
Simple and concise overview about Quantum computing..pptx
mughal641
 
PDF
Brief History of Internet - Early Days of Internet
sutharharshit158
 
PDF
Doc9.....................................
SofiaCollazos
 
PDF
AI-Cloud-Business-Management-Platforms-The-Key-to-Efficiency-Growth.pdf
Artjoker Software Development Company
 
PPTX
The-Ethical-Hackers-Imperative-Safeguarding-the-Digital-Frontier.pptx
sujalchauhan1305
 
PDF
Automating ArcGIS Content Discovery with FME: A Real World Use Case
Safe Software
 
PDF
OFFOFFBOX™ – A New Era for African Film | Startup Presentation
ambaicciwalkerbrian
 
PPTX
Agile Chennai 18-19 July 2025 Ideathon | AI Powered Microfinance Literacy Gui...
AgileNetwork
 
PDF
Software Development Methodologies in 2025
KodekX
 
PDF
How ETL Control Logic Keeps Your Pipelines Safe and Reliable.pdf
Stryv Solutions Pvt. Ltd.
 
PDF
Responsible AI and AI Ethics - By Sylvester Ebhonu
Sylvester Ebhonu
 
PPTX
AI in Daily Life: How Artificial Intelligence Helps Us Every Day
vanshrpatil7
 
PDF
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
 
PDF
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 
GDG Cloud Munich - Intro - Luiz Carneiro - #BuildWithAI - July - Abdel.pdf
Luiz Carneiro
 
OA presentation.pptx OA presentation.pptx
pateldhruv002338
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
Make GenAI investments go further with the Dell AI Factory
Principled Technologies
 
Get More from Fiori Automation - What’s New, What Works, and What’s Next.pdf
Precisely
 
Simple and concise overview about Quantum computing..pptx
mughal641
 
Brief History of Internet - Early Days of Internet
sutharharshit158
 
Doc9.....................................
SofiaCollazos
 
AI-Cloud-Business-Management-Platforms-The-Key-to-Efficiency-Growth.pdf
Artjoker Software Development Company
 
The-Ethical-Hackers-Imperative-Safeguarding-the-Digital-Frontier.pptx
sujalchauhan1305
 
Automating ArcGIS Content Discovery with FME: A Real World Use Case
Safe Software
 
OFFOFFBOX™ – A New Era for African Film | Startup Presentation
ambaicciwalkerbrian
 
Agile Chennai 18-19 July 2025 Ideathon | AI Powered Microfinance Literacy Gui...
AgileNetwork
 
Software Development Methodologies in 2025
KodekX
 
How ETL Control Logic Keeps Your Pipelines Safe and Reliable.pdf
Stryv Solutions Pvt. Ltd.
 
Responsible AI and AI Ethics - By Sylvester Ebhonu
Sylvester Ebhonu
 
AI in Daily Life: How Artificial Intelligence Helps Us Every Day
vanshrpatil7
 
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
 
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
Ad

functionnotes.pdf

  • 1. AXL IT Academy Functions In Python, a function can be defined as a named block of code. We use functions mainly to avoid code repetition. We can define that action only once using a function and call that function whenever required to do the same activity. The benefit of using a function is code reusability and modularity. Function improves efficiency and reduces errors because of the reusability of a code. Once we create a function, we can call it anywhere and anytime. Creating a Function In Python a function is defined using the def keyword. The “def” stands for define. It is used for telling the interpreter that it is a user defined function. The “def” is followed by function name, which is followed by parameters(Optional). Define the function body with a block of code. Syntax: functionNameHere(parameters): def #your code here
  • 2. AXL IT Academy Note: There is no need to specify curly braces for the function body, in python. Only indentation is needed to separate code blocks. Calling a Function To call a function, use the function name followed by parenthesis, which is followed by arguments (if any). : Note 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. Arguments
  • 3. AXL IT Academy Arguments are specified after the function name, inside the parentheses, for passing information into function. You can add as many arguments as you want, just separate them with a comma. Parameters and Arguments 1. A parameter is the variable listed inside the parentheses in the function definition. 2. An argument is the value that is sent to the function when it is called. Recursion Recursion means that a defined function calls itself. When written correctly recursion can be a very useful and effective.
  • 4. AXL IT Academy Example: #Recursion function which prints upto 99 without for/while recur (k): def if(k <100): (k) print k+=1 ( k ) recur #calling recur recur (1) Function Assignment We can assign a function to a variable in the same way as we assign a value to it. You can even use those variables to call the function as well. def proc(): return "Welcome" procAlias= proc print(proc) print(procAlias) print(proc ())
  • 5. AXL IT Academy print(procAlias()) Return in Function Return means the task is completed and now you need to return to where you came, with the specified data. You can return any type of data, you can even return a function. This should be the last statement in a function. def proc(): return "Bon Voyage" print(proc ()) Inner Function A function which is defined inside another function is known as inner function or nested function. Nested functions are able to access variables of the enclosing scope. Types of function arguments. There are various ways to use arguments in a function. In Python, we have the following 4 types of function arguments.
  • 6. AXL IT Academy 1. Default argument 2. Keyword arguments (named arguments) 3. Positional arguments 4. Arbitrary arguments (variable-length arguments *args and **kwargs)
  • 7. AXL IT Academy Default Arguments Default arguments are arguments those have default values. We assign default values to the argument using the ‘=’ (assignment) operator at the time of function definition. You can define a function with any number of default arguments. The default value of an argument will be used inside a function if we do not pass a value to that argument at the time of the function call. Due to this, the default arguments become optional during the function call. It overrides the default value if we provide a value to the default arguments during function calls. Keyword Arguments
  • 8. AXL IT Academy Usually, at the time of the function call, values get assigned to the arguments according to their position. So we must pass values in the same sequence defined in a function definition. For example, when we call show('dass', 19), the value “dass” gets assigned to the argument name, and similarly, 19 to age and so on as per the sequence. We can alter this behavior using a keyword argument. Keyword arguments are those arguments where values get assigned to the arguments by their keyword (name) when the function is called. It is preceded by the variable name and an (=) assignment operator. The Keyword Argument is also called a named argument. Change the sequence of keyword arguments Also, you can change the sequence of keyword arguments by using their name in function calls. Python allows functions to be called using keyword arguments. But all the keyword arguments should match the parameters in the function definition. When we call functions in this way, the order (position) of the arguments can be changed. Positional Arguments Positional arguments are arguments that can be called by their position in the function definition By default, Python functions are called using the positional arguments. Variable-length arguments
  • 9. AXL IT Academy We use variable-length arguments if we don’t know the number of arguments needed for the function in advance. Types of Arbitrary Arguments:  arbitrary positional arguments (*args)  arbitrary keyword arguments (**kwargs) The *args and **kwargs allow you to pass multiple positional arguments or keyword arguments to a function. Arbitrary positional arguments (*args) We can declare a variable-length argument with the * (asterisk) symbol. Place an asterisk (*) before a parameter in the function definition to define an arbitrary positional argument. we can pass multiple arguments to the function. Internally all these values are represented in the form of a tuple.