0% found this document useful (0 votes)
8 views11 pages

FUNCTIONS

The document explains the concept of functions in programming, detailing their definition, advantages, and various types such as predefined, module, and user-defined functions. It includes examples of different function categories, argument types, and the scope of variables, illustrating how to create and call functions in Python. Additionally, it covers the rules for combining positional, default, and keyword arguments in function calls.

Uploaded by

Prachi Joshi
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)
8 views11 pages

FUNCTIONS

The document explains the concept of functions in programming, detailing their definition, advantages, and various types such as predefined, module, and user-defined functions. It includes examples of different function categories, argument types, and the scope of variables, illustrating how to create and call functions in Python. Additionally, it covers the rules for combining positional, default, and keyword arguments in function calls.

Uploaded by

Prachi Joshi
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/ 11

FUNCTIONS

FUNCTION:- It is a block of code that is execute when it is


called.
def keyword is use to create a function. functions are use
to divide a big program into different different sub
programs.
The main advantage of functions are code reusability and
reduce complexity.
Ex:-
def function_name([arguments]):
statements
Types of Function
1. Predefined Function: no need to import module.
2. Module Function: it is also predefined function but for
ise these function we need to import module.
3. User Defined Function: Create by User.
Categories of Function:-
1. def fun(): no return type no argument.
2. def fun(): with return type no argument.
3. def fun(variable): no return type with argument.
4. def fun(variable): with return type with argument.

Q1. WAP to input two numbers and display addition [no


return no argument]
Code:-
def addition():#Definition
x=int(input("Enter First Number"))
y=int(input("Enter Second Number"))
z=x+y
print("Addition = ",z)
addition()#Calling
Q2. WAP to input two numbers and display addition [no
return with argument]
Code:-
def add(x,y): #Function Header
z=x+y
print("Addition : ",z)
x=int(input("Enter First Number"))
y=int(input("Enter Second Number"))
add(x,y) #Calling
Q3. WAP to input two numbers and display addition[with
return no argument]
Code:-
def add(): #Function Header
x=int(input("Enter First Number"))
y=int(input("Enter Second Number"))
z=x+y
return z
z = add() #Calling
print("Addition : ",z)
Q4. WAP to input two numbers and display addition.
[with return with argument]
Code:-
def add(x,y): #Function Header
z=x+y
return z
x=int(input("Enter First Number"))
y=int(input("Enter Second Number"))
z = add(x,y) #Calling
print("Addition : ",z)
Q5. WAP to input a number and display its factorial
[using all categories]
Arguments:- Python refers to the value being passed as
Arguments.
Parameters:- Value being received as Parameter.
def addition(x,y):#[here x and y are called Parameter]
z=x+y
print("Addition)
x=int(input("Enter First Number : "))
y=int(input("Enter Second Number : "))
addition(x,y)#[here x and y are Argument]
Note:- Arguments can be one of these value types
a) Literals(Constant).
b) Variable.
c) Expression
Ex:-
fun(x,y)
fun(12,24)
fun(x+y,x-y)
PASSING PARAMETERS.
1. Positional Arguments/Mandatory Arguments/Required
Arguments.
2. Default Argument.
3. Keyword(Named) Arguments.

1. Positional Arguments.
def fun(pri,rate,time):
si=pri*rate*time/100
return si
Choose the right Caller
fun(45000,4,2) #True
fun(45000,4) #Wrong
fun(45000) #Wrong
fun(45000,4,2,7)#Wrong

2. Default Argument.
NOTE:- in default argument if left side variable is default
than all variable should be default.
def fun(pri,rate=7,time=2):
si=pri*rate*time/100
return si
Choose the right Caller
fun(45000,4,6)#True
fun(45000,4) #True
fun(45000)#True
fun(45000,4,2,7)#Wrong
3. Keyword[Named] Arguments:- python offers a way of
writing function calls where you can write any argument
in any order provided you name the arguments when
calling the function.
def fun(pri,rate,time):
print("Principal : ",pri)
print("Rate : ",rate)
print("Time : ",time)
fun(rate=4,time=2,pri=45000)
Rules for combining all three types of arguments.
a)an argument list must first contain
positional(required) arguments followed by keyword
argument.
b)keywords arguments should be taken from
required argument.
c)you can not specify a value for an argument more
than once.
Example:-
def interest(prin,cc,time=2,rate=0.02):
si=prin*rate*time/100
return si
Choose the right Caller
interest(prin=3000 , cc=5) #TRUE
interest(rate=0.02 , prin=5000 , cc=4) #TRUE
interest(cc=4 , rate=0.12 , prin=8000) #TRUE
interest(5000 , 3 , rate=0.05) #TRUE
interest(rate=0.08,5000,4) #WRONG
[keywords argument before positional argument]
interest(5000 , prin=300 , cc=2) #WRONG
[Morethan one value can not be provided]
interest(5000 , principal=300 , cc=2) #WRONG
interest(500 , time=2 , rate=0.05) #WRONG
SCOPE:- Parts of program within which a name is legel
and accesible, is called scope of the name.

1. Local SCOPE
def fun():
x=200 #Local Scope
print("x : ",x)
fun()
print("x : ",x) #Error
2. Global SCOPE:- A name declared in top level
segment(_main_) of a program is said to have a global
scope and is usable inside the whole program and all
blocks.
def fun():
print("x : ",x)
x=200 #Global Scope
fun() #Caller
print("x : ",x) #No Error
NOTE- same variable name in local scope as well as in
global scope.
Ex-1
def state1():
tigers=15 #[Local Scope]
print(tigers)
tigers = 95 #[Global Scope]
print(tigers)
state1()
print(tigers)
Output:
95
15
95
Ex-2
def state1():
global tigers
tigers=15 #[Local Scope]
print(tigers)
tigers = 95 #[Global Scope]
print(tigers)
state1()
print(tigers)
Output:
95
15
15

You might also like