PYTHON Functions-I
Python Functions
Functions are named block of code(s) that are designed to do specific task (job).
Functions are nothing but a group of related statements that perform a specific task under a
function header name.
Advantages of functions:
It avoids repetition and makes code reusing (calling).
It provides better modularity (reduce the program) for your application.
It is mainly used for Modular programming system (module creation).
Types of Functions
o User-defined Functions
o Built-in Functions
o Lambda Functions
o Recursion Functions
Lambda functions: Functions that are anonymous (un-named) function.
User-defined functions: Functions defined by the users themselves.
Recursion functions: Function that calls itself is known as recursive.
Built-in Functions: Standard functions, library functions or default functions
Defining Functions (or) User define functions
Function blocks begin with the keyword “ def ” followed by function name and parenthesis ( ).
Syntax for User defined function
def function_name ( parameter1, parameter2, …..) :
Block of statement1
Block of statement2
……………………
……………………
Block of statement n
Example
By E. V. VIJAYARAJ M.Sc (Maths) B.Ed., M.C.A., M.Phil., D.C.S.Engg., Page 1
PYTHON Functions-I
Two parts of user defined function:
i) Function header or declaration
ii) Function body or block or definition
i) Function header or declaration
The keyword “ def ” followed by function name(defined by user) with may (or) may not be
input parameters or arguments should be placed within these parentheses ( )when before you
define a function.
Then the code block always comes after a colon (:) and is indented (to start definition (block)).
ii) Function body or block or definition
A block is one or more lines of code, grouped together.
Statements in a block are written with indentation (by tab key or four spaces).
All the statements of the block should be at same indent level.
Note: The code block always comes after a colon (:) and is indented.
The statement “return expression” exits a function, optionally passing back an expression to the
caller.
A “return” with no arguments is the same as return None (or) return
By E. V. VIJAYARAJ M.Sc (Maths) B.Ed., M.C.A., M.Phil., D.C.S.Engg., Page 2
PYTHON Functions-I
Note: Python keywords should not be used as function name.
Passing Parameters in Functions
Syntax:
def function_name (parameter(s) separated by comma):
Note:
We can pass all sorts of data (any type) to the functions.
Difference between parameter(s) and argument(s)
Parameters are the variables used in the function definition whereas arguments are the values we pass
to the function parameters when calling from another function.
Advantages of User-defined Functions
Functions help us to divide a program into modules. This makes the code easier to manage.
It implements code reuse. Every time you need to execute a sequence of statements, all you need
to do is to call the function.
Functions, allows us to change functionality easily, and different programmers can work on
different functions.
Function Arguments
Arguments are used to call a function and there are primarily 4 types of functions:
By E. V. VIJAYARAJ M.Sc (Maths) B.Ed., M.C.A., M.Phil., D.C.S.Engg., Page 3
PYTHON Functions-I
1. Required Arguments
“Required Arguments” are the arguments passed to a function in correct positional order.
Here, the number of arguments in the function call should match exactly with the number of
parameters in function definition.
Example:
By E. V. VIJAYARAJ M.Sc (Maths) B.Ed., M.C.A., M.Phil., D.C.S.Engg., Page 4
PYTHON Functions-I
Note: First one return keyword with statement only will be taken.
Example for single line function using semicolon ( ;)
By E. V. VIJAYARAJ M.Sc (Maths) B.Ed., M.C.A., M.Phil., D.C.S.Engg., Page 5
PYTHON Functions-I
2. Keyword Arguments
Keyword arguments will invoke the function after the parameters are recognized by their parameter
(variable ) names.
The value of the keyword argument is matched with the parameter name and so, one can also put
arguments in improper order (not in order).
Example
By E. V. VIJAYARAJ M.Sc (Maths) B.Ed., M.C.A., M.Phil., D.C.S.Engg., Page 6
PYTHON Functions-I
Error occur: for keyword variable names are different in between call function (arguments) and function
(parameters)
Example for mixing keyword arguments with required argument
3. Default Arguments
In Python the default argument is parameter that takes a default value if no value is provided in the
function call (as argument).
So first priority value of argument (function call) than default value of parameter (function definition).
By E. V. VIJAYARAJ M.Sc (Maths) B.Ed., M.C.A., M.Phil., D.C.S.Engg., Page 7
PYTHON Functions-I
Note: The rightmost default parameters (arguments) must have default value initialization. But no need
leftmost has initialization value.
By E. V. VIJAYARAJ M.Sc (Maths) B.Ed., M.C.A., M.Phil., D.C.S.Engg., Page 8
PYTHON Functions-I
Note: Default parameter value can be given from right to left .
By E. V. VIJAYARAJ M.Sc (Maths) B.Ed., M.C.A., M.Phil., D.C.S.Engg., Page 9