0% found this document useful (0 votes)
3 views16 pages

Functions in Python

Functions IIT Madras CSE pdf
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views16 pages

Functions in Python

Functions IIT Madras CSE pdf
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Functions: Types of function (built-in functions, functions defined in

module, user defined functions), creating user defined function,


arguments and parameters, default parameters, positional parameters,
function returning value(s), flow of execution, scope of a variable (global
scope, local scope).

Introduction
o A function is a group of statement that perform a specific task. A
function executes when it is called.

Advantages of Functions in Python:


 Program development made easy and fast.
 Program testing becomes easy.
 PROGRAM HANDLING EASIER: Only small part of the program is
dealt with at a time.
 REDUCED LoC: as with function the common set of code is written
only once and can be called from any part of program, so it reduces
Line of Code.
 EASY UPDATING: If we want to change in any formula / expression
then we have to make changes to every place, if forgotten then
output will not be the desired output. With function we have to
make changes to only one location.
 Re-usability of code increases.
 Set of functions is stored in a file called MODULE. This approach is
known as MODULARIZATION, makes program easier to
understand, test and maintain.
 Commonly used modules that contain source code for generic need
are called LIBRARIES.
 Modules contains set of functions.

Functions can be categorized into the following three types.


1. Built-in Functions.
2. Modules.
3. User Defined Functions.
Built-in Functions:
Those functions which are already available in python are called built-in
functions. These are always available in the standard library (No need to
import any modules).
1) int() – converts any value (string/float) into an integer.
2) float() – converts integers into floating point numbers.
3) input() – helps to accept any value from the user through keyboard.
It always return string value.
4) eval() – takes string value and evaluates. If arg is not string then
returns error.
5) max() - returns the largest value out of the given arguments.
6) min() – returns the smallest value out of the given arguments.
7) abs() – returns an absolute (positive value) of a no.
8) type() – returns the data type of a variable or a value.
9) len() – return total no. of items in an object.
10) range() – It generates sequence number.

User Defined Functions:


A set of statement that performs a specific task is called function. A
function which is defined or created by the user is called user defined
function.
A user defined function is created by the def keyword followed by
function name, parenthesis and colon.

Syntax of user defined function:

Task 1: Create a function which print “Welcome to Python Functions”

Keyword def marks the start of function header.


Function name must be unique and follows naming rules
same as for identifiers.
Function can take arguments. It is optional.
A colon(:) to mark the end of function header.
Function can contains one or more statement to perform
specific task.
An optional return statement to return a value from the function.
Function must be called / invoked to execute its code.
User Defined function can be…
1) Function with no arguments and , no return value.
2) Function with arguments but no return value.
3) Function with no argument but return value.
4) Function with argument and return value.

Function Name NO PARAMETER, HENCE VOID


return keyword not used.

Function calling, It will invoke Test() to perform its action.

This type of function is also known as void function.

Function with parameters, but no return value:


Parameters are given in the parenthesis separated by comma.
Values are passed for the parameters at the time of function calling.
Extra coding:
Function with parameters but no return value:
WAP to calculate Average of 3 Nos. using function with arguments and no
return value.
Function with parameter and return value:
We can return values from function using return keyword.
The return value must be used at the calling place by:
 Either store it any variable
 Use with print()
 Use in any expression

Function with parameter and return:


Note: The return statement ends a function execution even if it is in the
middle of function. Anything written below return statement will
become unreachable code.

Function not returning value:


Function may or may not return a value. Non returning function is also
known as void function. It may or may not contain return. If it contain
return statement then it will be in the form of:
return #No value after return.

Parameters and Arguments in Function:


Parameters are the value(s) provided in the parenthesis when we write
function header. These are the values required by function to work.
If there are more than one parameter, it must be separated by comma (,).
An argument is a value that is passed to the function when it is called. In
other words arguments are the value(s) provided in function call / invoke
statement.
Parameter is also known as FORMAL ARGUMENTS / PARAMETERS.
Arguments is also known as ACTUAL ARGUMENTS / PARAMETERS.
Note: Function can alter only MUTABLE TYPE values.

Types of Arguments:
There are 4 types of actual arguments allowed in python.
1) Positional arguments
2) Default arguments
3) Keyword arguments
4) Variable length arguments

Positional arguments:
The are arguments passed to a function in correct positional order.
Here x is passed to a and y is passed to b. i.e., in the order of their position.

Default Arguments:
Sometimes we can provide default values for our positional
arguments. In this case if we are not passing any value then default
values will be considered.
Default argument must not followed by non-default arguments.
Default Arguments:

Homework: Convert Decimal to Binary or Octal

Keyword (Named) Arguments:


The default keyword gives flexibility to specify default value for a
parameter so that it can be skipped in the function call, if needed.
However, still we cannot change the order of arguments in function
call. i.e., you have to remember the order of the arguments and pass
the value accordingly.
To get control and flexibility over the values sent as arguments,
python offers KEYWORD ARGUMENTS.
This allows to call function with arguments in any order using name
of the arguments.
Rules for combining all three type of arguments:
 An argument list must first contain positional arguments followed by
keyword arguments.
 Keyword arguments should be taken from the required arguments.
 You cannot specify a value for an argument more than once.
https://csiplearninghub.com/user-defined-functions-in-python/
https://techtipnow.in/function-in-python-class-12/

You might also like