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
PPTX
Learn more about the concepts Functions of Python
PPTX
Functions in Python Programming Language
PPT
Py-Slides-3 difficultpythoncoursefforbeginners.ppt
PPTX
INTRODUCTION TO PYTHON PROGRMMING AND FUNCTIONS
PPT
Powerpoint presentation for Python Functions
PPTX
Functions and Modules.pptx
PPTX
Functions in python slide share
Python functions
Learn more about the concepts Functions of Python
Functions in Python Programming Language
Py-Slides-3 difficultpythoncoursefforbeginners.ppt
INTRODUCTION TO PYTHON PROGRMMING AND FUNCTIONS
Powerpoint presentation for Python Functions
Functions and Modules.pptx
Functions in python slide share

Similar to functionnotes.pdf (20)

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

Recently uploaded (20)

PDF
NewMind AI Weekly Chronicles - August'25-Week II
PDF
Univ-Connecticut-ChatGPT-Presentaion.pdf
PDF
Encapsulation_ Review paper, used for researhc scholars
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PPTX
A Presentation on Artificial Intelligence
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PPTX
Programs and apps: productivity, graphics, security and other tools
PPTX
Spectroscopy.pptx food analysis technology
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPTX
cloud_computing_Infrastucture_as_cloud_p
PPTX
Machine Learning_overview_presentation.pptx
PDF
Machine learning based COVID-19 study performance prediction
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
August Patch Tuesday
PDF
Empathic Computing: Creating Shared Understanding
PPTX
1. Introduction to Computer Programming.pptx
PDF
Approach and Philosophy of On baking technology
NewMind AI Weekly Chronicles - August'25-Week II
Univ-Connecticut-ChatGPT-Presentaion.pdf
Encapsulation_ Review paper, used for researhc scholars
Digital-Transformation-Roadmap-for-Companies.pptx
A Presentation on Artificial Intelligence
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Spectral efficient network and resource selection model in 5G networks
Assigned Numbers - 2025 - Bluetooth® Document
Programs and apps: productivity, graphics, security and other tools
Spectroscopy.pptx food analysis technology
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
cloud_computing_Infrastucture_as_cloud_p
Machine Learning_overview_presentation.pptx
Machine learning based COVID-19 study performance prediction
MIND Revenue Release Quarter 2 2025 Press Release
August Patch Tuesday
Empathic Computing: Creating Shared Understanding
1. Introduction to Computer Programming.pptx
Approach and Philosophy of On baking technology
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.