0% found this document useful (0 votes)
7 views5 pages

Function in Python

Computer science class 12

Uploaded by

murshida.hzb
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)
7 views5 pages

Function in Python

Computer science class 12

Uploaded by

murshida.hzb
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/ 5

FUNCTION IN PYTHON

DEFINATION: A function is the organized block of reusable codes which can be


called whenever required.
TYPES OF FUNCTION
1. BUILT IN FUNCTION:
It is also known as library function. These are predefined function in python
program system that are ready to use.
Eg: type(), input(), print()

2. USER DEFINED FUNCTION:


It helps the user to define their own function according to their own
requirements.
Eg: finding factorial, checking number is prime or not.

3. FUNCTION DEFINED IN MODULES:


In python there are some predefined specific modules that are required to
access by importing their modules.
Eg: sin(), cos(), pow(), sqrt() requires math module
Mean(), mode() requires statistics modules.
DEFINING A FUNCTION
1. Function contains def keyword to provide required functionality.
def is followed by the function name and parenthesis.
2. All the parameters or the arguments should be placed within the parenthesis
3. There should be colon (: ) at the end of def line.
4. Function body contains argument, some sets of statements that can return
result.
Syntax:
def function_name(formal parameters):
function body
return
eg:
def sum(x,y):
z=x+y
print (z)
return
CALLING A FUNCTION
To execute a function the user have to call the function name along with the
actual parameters.
Rules for calling function
1. Write the desired function name.
2. In function parenthesis pass the parameters. Parameters are the values
that are passed in the function.
3. Function can be called multiple times with different expression as the
actual parameters.
Syntax:
def function_name(formal parameters):
function body
return
function_name(actual parameters)
eg:
def sum(x,y): #formal parameters/ parameters
z=x+y
print (z) function body
return
sum(x=int(input(“enter 1st number”)), y=int(input(“enter 2nd number”)))
# actual parameters/ arguments
PARAMETERS It is the variable used to define value in function definition
ARGUMENTS It is value passed in function during function call

PASSING PARAMETERS
1. Default parameter values:
Defining the value in the def
Eg:
def function (a,b, c=10) default parameters
2. Keyword argument:
Defining the value during the function call

Eg:
def sum(a,b):
x=a+b
y=a-b
print(“sum is:”,x)
print(“difference is:”,y)
sum(a=10, b=6)
sum(a=5, b=8)

3. USING MULTIPLE ARGUMENT TYPE TOGETHER:


Defining multiple arguments during function call

Eg:
def sum(a,b):
x=a+b
print(“sum is:”,x)
sum(a=10, b=6)
RETURNING VALUES FROM FUNCTION
1. NON-VOID FUNCTIONS:
It is used to do specific task. It returns the value on completion of
task. It is also called fruitful function.
Eg:
def work( ):
print(“HELLO”)
return “HELLO”

2. FUNCTION NOT RETURNING ANY (VOID FUNCTION)


When a function has no return value
It is also called non-fruitful function.
EG:
def sum(a,b):
x=a+b
print(“sum is:”,x)
sum(a=10, b=6)
3. RETURNING MULTIPLE VALUE

def sum(a,b):
x=a+b
print(“sum is:”,x)
sum(a=10, b=6)

SCOPE OF VARIABLES
A variable defined inside a function cannot be accessed outside.
Scope of variables shows the area where the variables can be used.
There are two types of scope of variables:
1. LOCAL VARIABLE (LOCAL SCOPE):
A variable that is defined inside any function or block is known as local
variable.
It is accessed only by the statements of the function

Eg:
def msg( ):
message=” hello” #local variable defined inside function
print( message)
msg( )

Error program:
def msg( ):
message=” hello” #local variable defined inside function
print( message)
msg( )
print(message) # this will cause an error because a local variable
cannot be accessed while calling function
2. GLOBAL VARIABLE( GLOBAL SCOPE):
A variable that is defined outside of all function or block is known as
local variable.
It is accessed by all the function without declaring inside the body.
Eg:
total=0 #global variable
def sum(a,b):
total=a+b
print(“sum is “, total)
sum(a=int(input(“enter the number”)), a=int(input(“enter the
number”))
print(“outside sum”, total)

You might also like