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

The Advantages of Function

Uploaded by

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

The Advantages of Function

Uploaded by

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

The Advantages of Function

Thus, following are the advantages of using functions in a program:


• Increases readability, particularly for longer code as by using functions, the program is better organised and easy to
understand.
• Reduces code length as same code is not required to be written at multiple places in a program. This also makes
debugging easier.
• Increases reusability, as function can be called from another function or another program. Thus, we can reuse or build
upon already defined functions and avoid repetitions of writing the same piece of code.
• Work can be easily divided among team members and completed in parallel.

Creating User Defined Function


A function definition begins with def (short for define). The syntax for creating a user defined function is as follows:
• The items enclosed in "[ ]" are called parameters and they are optional. Hence, a function may or
may not have parameters. Also, a function may or may not return a value.
• Function header always ends with a colon (:).
• Function name should be unique. Rules for naming identifiers also applies for function naming.
• The statements outside the function indentation are not considered as part of the function.

def calcAreaPeri(Length,Breadth):
area = length * breadth
perimeter = 2 * (length + breadth)
#a tuple is returned consisting of 2 values area and perimeter
return (area,perimeter)
l = float(input("Enter length of the rectangle: "))
b = float(input("Enter breadth of the rectangle: "))
#value of tuples assigned in order they are returned
area,perimeter = calcAreaPeri(l,b)
print("Area is:",area,"\nPerimeter is:",perimeter)

def trafficLight():
signal = input("Enter the colour of the traffic light: ")
if (signal not in ("RED","YELLOW","GREEN")):
print("Please enter a valid Traffic Light colour in
CAPITALS")
else:
value = light(signal)
if (value == 0):
print("STOP, Your Life is Precious.")
elif (value == 1):
print ("PLEASE GO SLOW.")
else:
print("GO!,Thank you for being patient.")
#function ends here

def light(colour):
if (colour == "RED"):
return(0);
elif (colour == "YELLOW"):
return (1)
else:
return(2)

trafficLight()
print("SPEED THRILLS BUT KILLS")

Scope of a Variable
A variable defined inside a function cannot be accessed outside it. Every variable has a well-defined accessibility. The
part of the program where a variable is accessible can be defined as the scope of that variable. A variable can have one
of the following two scopes: A variable that has global scope is known as a global variable and a variable that has a
local scope is known as a local variable.
(A) Global Variable In Python, a variable that is defined outside any function or any block is known as a global variable.
It can be accessed in any functions defined onwards. Any changemade to the global variable will impact all the
functions in the program where that variable can be accessed.
(B) Local Variable A variable that is defined inside any function or a block is known as a local variable. It can be
accessed only in the function or a block where it is defined. It exists only
till the function executes

print( "Enter values for the cylindrical part of the tent in


meters\n")
h = float(input("Enter height of the cylindrical part: "))
r = float(input("Enter radius: "))
l = float(input("Enter the slant height of the conical part in
meters: "))
csa_conical = 3.14*r*l #Area of conical part
csa_cylindrical = 2*3.14*r*h #Area of cylindrical part

# Calculate area of the canvas used for making the tent


canvas_area = csa_conical + csa_cylindrical
print("The area of the canvas is",canvas_area,"m^2")

#Calculate cost of the canvas


unit_price = float(input("Enter the cost of 1 m^2 canvas: "))
total_cost= unit_price * canvas_area
print("The total cost of canvas = ",total_cost)

#Add tax to the total cost to calculate net amount payable by the
#customer
tax = 0.18 * total_cost;
net_price = total_cost + tax
print("Net amount payable = ",net_price)

#function header
def sumSquares(n): #n is the parameter
sum = 0
for i in range(1,n+1):
sum = sum + i
print("The sum of first",n,"natural numbers is: ",sum)

num = int(input("Enter the value for n: "))

You might also like