0% found this document useful (0 votes)
9 views

01.Funtions in Python

The document provides an overview of functions in Python, including their definition, types (library functions, functions defined in modules, and user-defined functions), and examples of usage. It explains how to define and call functions, the concept of parameters, the return statement, and the scope and lifetime of variables. Additionally, it includes examples of built-in functions and demonstrates how to work with functions from the math and random modules.

Uploaded by

technokkbusiness
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

01.Funtions in Python

The document provides an overview of functions in Python, including their definition, types (library functions, functions defined in modules, and user-defined functions), and examples of usage. It explains how to define and call functions, the concept of parameters, the return statement, and the scope and lifetime of variables. Additionally, it includes examples of built-in functions and demonstrates how to work with functions from the math and random modules.

Uploaded by

technokkbusiness
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

CHAPTER-1

FUNCTIONS IN PYTHON
1.1 Definition: Functions are the subprograms that perform specific task. Functions are the
small modules.

1.2 Types of Functions:


There are two types of functions in python:
1. Library Functions (Built in functions)
2. Functions defined in modules
3. User Defined Functions

Built in functions

Functions defined in
Types of functions modules

User defined functions

1. Library Functions: These functions are already built in the python library.

2. Functions defined in modules: These functions defined in particular modules. When


you want to use these functions in program, you have to import the corresponding module
of that function.

3. User Defined Functions: The functions those are defined by the user are called user
defined functions.

1. Library Functions in Python:


These functions are already built in the library of python.
For example: type( ), len( ), input( ) etc.

https://pythonschoolkvs.wordpress.com/ Page 2
2. Functions defined in modules:
a. Functions of math module:
To work with the functions of math module, we must import math module in program.
import math
S. No. Function Description Example
1 sqrt( ) Returns the square root of a number >>>math.sqrt(49)
7.0
2 ceil( ) Returns the upper integer >>>math.ceil(81.3)
82
3 floor( ) Returns the lower integer >>>math.floor(81.3)
81
4 pow( ) Calculate the power of a number >>>math.pow(2,3)
8.0
5 fabs( ) Returns the absolute value of a number >>>math.fabs(-5.6)
5.6
6 exp( ) Returns the e raised to the power i.e. e3 >>>math.exp(3)
20.085536923187668

b. Function in random module:


random module has a function randint( ).
 randint( ) function generates the random integer values including start and end values.
 Syntax: randint(start, end)
 It has two parameters. Both parameters must have integer values.

Example:
import random
n=random.randint(3,7)

*The value of n will be 3 to 7.

3. USER DEFINED FUNCTIONS:


The syntax to define a function is:
def function-name ( parameters) :
#statement(s)

https://pythonschoolkvs.wordpress.com/ Page 3
Where:

 Keyword def marks the start of function header.


 A function name to uniquely identify it. Function naming follows the same rules of
writing identifiers in Python.
 Parameters (arguments) through which we pass values to a function. They are optional.
 A colon (:) to mark the end of function header.
 One or more valid python statements that make up the function body. Statements must
have same indentation level.
 An optional return statement to return a value from the function.

Example:

def display(name):

print("Hello " + name + " How are you?")

1.3 Function Parameters:


A functions has two types of parameters:
1. Formal Parameter: Formal parameters are written in the function prototype and function
header of the definition. Formal parameters are local variables which are assigned values from
the arguments when the function is called.
2. Actual Parameter: When a function is called, the values that are passed in the call are
called actual parameters. At the time of the call each actual parameter is assigned to the
corresponding formal parameter in the function definition.
Example :
def ADD(x, y): #Defining a function and x and y are formal parameters
z=x+y
print("Sum = ", z)
a=float(input("Enter first number: " ))
b=float(input("Enter second number: " ))
ADD(a,b) #Calling the function by passing actual parameters
In the above example, x and y are formal parameters. a and b are actual parameters.

https://pythonschoolkvs.wordpress.com/ Page 4
1.4 Calling the function:
Once we have defined a function, we can call it from another function, program or even the
Python prompt. To call a function we simply type the function name with appropriate
parameters.

Syntax:
function-name(parameter)
Example:
ADD(10,20)

OUTPUT:
Sum = 30.0

How function works?

def functionName(parameter):
… .. …
… .. …
… .. …
… .. …
functionName(parameter)

… .. …
… .. …

The return statement:


The return statement is used to exit a function and go back to the place from where it was
called.
There are two types of functions according to return statement:
a. Function returning some value (non-void function)
b. Function not returning any value (void function)
https://pythonschoolkvs.wordpress.com/ Page 5
a. Function returning some value (non-void function) :
Syntax:
return expression/value
Example-1: Function returning one value
def my_function(x):
return 5 * x

Example-2 Function returning multiple values:


def sum(a,b,c):
return a+5, b+4, c+7
S=sum(2,3,4) # S will store the returned values as a tuple
print(S)

OUTPUT:
(7, 7, 11)
Example-3: Storing the returned values separately:
def sum(a,b,c):
return a+5, b+4, c+7
s1, s2, s3=sum(2, 3, 4) # storing the values separately
print(s1, s2, s3)
OUTPUT:
7 7 11

b. Function not returning any value (void function) : The function that performs some
operationsbut does not return any value, called void function.
def message():
print("Hello")

m=message()
print(m)

https://pythonschoolkvs.wordpress.com/ Page 6
OUTPUT:
Hello
None

1.5 Scope and Lifetime of variables:

Scope of a variable is the portion of a program where the variable is recognized. Parameters
and variables defined inside a function is not visible from outside. Hence, they have a local
scope.

There are two types of scope for variables:

1. Local Scope

2. Global Scope

1. Local Scope: Variable used inside the function. It can not be accessed outside the function.
In this scope, The lifetime of variables inside a function is as long as the function executes.
They are destroyed once we return from the function. Hence, a function does not remember the
value of a variable from its previous calls.

2. Global Scope: Variable can be accessed outside the function. In this scope, Lifetime of a
variable is the period throughout which the variable exits in the memory.

Example:

def my_func():
x = 10
print("Value inside function:",x)

x = 20
my_func()
print("Value outside function:",x)

OUTPUT:

Value inside function: 10

Value outside function: 20

https://pythonschoolkvs.wordpress.com/ Page 7

You might also like