Humanities/Arts Exam  >  Humanities/Arts Notes  >  Computer Science for Class 11  >  NCERT Textbook: Functions

NCERT Textbook: Functions | Computer Science for Class 11 - Humanities/Arts PDF Download

Download, print and study this document offline
Please wait while the PDF view is loading
 Page 1


7.1 Introduct Ion Till now we have written some programs and might 
have realised that as the problem gets complex, the 
number of lines in a program increase, which makes the 
program look bulky and difficult to manage. Consider 
the following problem statement: 
There is a company that manufactures tents as per 
user’s requirements. The shape of the tent is cylindrical 
surmounted by a conical top.
“Once you succeed in writing 
the programs for [these] 
complicated algorithms, they 
usually run extremely fast. 
The computer doesn’t need 
to understand the algorithm, 
its task is only to run the 
programs.”
– R. Tarjan
Chapter 7
Functions
In this chapter
 » Introduction to 
Functions
 » User Defined      
Functions
 » Scope of a Variable
 » Python Standard 
Library
Figure 7.1: Shape of a tent
The company performs the following tasks to fix the 
selling price of each tent. 
1. Accept user requirements for the tent, such as
a) height 
b) radius 
c) slant height of the conical part
2. Calculate the area of the canvas used
3. Calculate the cost of the canvas used for making 
the tent
4. Calculate the net payable amount by the customer 
that is inclusive of the 18% tax
The company has created a computer program for 
quick and accurate calculation for the payable amount 
as shown in program 7-1.
Ch 7.indd   143 08-Apr-19   12:23:12 PM
2024-25
Page 2


7.1 Introduct Ion Till now we have written some programs and might 
have realised that as the problem gets complex, the 
number of lines in a program increase, which makes the 
program look bulky and difficult to manage. Consider 
the following problem statement: 
There is a company that manufactures tents as per 
user’s requirements. The shape of the tent is cylindrical 
surmounted by a conical top.
“Once you succeed in writing 
the programs for [these] 
complicated algorithms, they 
usually run extremely fast. 
The computer doesn’t need 
to understand the algorithm, 
its task is only to run the 
programs.”
– R. Tarjan
Chapter 7
Functions
In this chapter
 » Introduction to 
Functions
 » User Defined      
Functions
 » Scope of a Variable
 » Python Standard 
Library
Figure 7.1: Shape of a tent
The company performs the following tasks to fix the 
selling price of each tent. 
1. Accept user requirements for the tent, such as
a) height 
b) radius 
c) slant height of the conical part
2. Calculate the area of the canvas used
3. Calculate the cost of the canvas used for making 
the tent
4. Calculate the net payable amount by the customer 
that is inclusive of the 18% tax
The company has created a computer program for 
quick and accurate calculation for the payable amount 
as shown in program 7-1.
Ch 7.indd   143 08-Apr-19   12:23:12 PM
2024-25
Computer SCien Ce – Cla SS xi 144
Program 7-1 Program to calculate the payable amount 
for the tent.
#Program 7-1
#Program to calculate the payable amount for the tent without 
#functions
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)
Another approach to solve the above problem is 
to divide the program into different blocks of code as 
shown in Figure 7.2. 
Figure 7.2: Calculation of the cost of the tent
Ch 7.indd   144 08-Apr-19   12:23:12 PM
2024-25
Page 3


7.1 Introduct Ion Till now we have written some programs and might 
have realised that as the problem gets complex, the 
number of lines in a program increase, which makes the 
program look bulky and difficult to manage. Consider 
the following problem statement: 
There is a company that manufactures tents as per 
user’s requirements. The shape of the tent is cylindrical 
surmounted by a conical top.
“Once you succeed in writing 
the programs for [these] 
complicated algorithms, they 
usually run extremely fast. 
The computer doesn’t need 
to understand the algorithm, 
its task is only to run the 
programs.”
– R. Tarjan
Chapter 7
Functions
In this chapter
 » Introduction to 
Functions
 » User Defined      
Functions
 » Scope of a Variable
 » Python Standard 
Library
Figure 7.1: Shape of a tent
The company performs the following tasks to fix the 
selling price of each tent. 
1. Accept user requirements for the tent, such as
a) height 
b) radius 
c) slant height of the conical part
2. Calculate the area of the canvas used
3. Calculate the cost of the canvas used for making 
the tent
4. Calculate the net payable amount by the customer 
that is inclusive of the 18% tax
The company has created a computer program for 
quick and accurate calculation for the payable amount 
as shown in program 7-1.
Ch 7.indd   143 08-Apr-19   12:23:12 PM
2024-25
Computer SCien Ce – Cla SS xi 144
Program 7-1 Program to calculate the payable amount 
for the tent.
#Program 7-1
#Program to calculate the payable amount for the tent without 
#functions
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)
Another approach to solve the above problem is 
to divide the program into different blocks of code as 
shown in Figure 7.2. 
Figure 7.2: Calculation of the cost of the tent
Ch 7.indd   144 08-Apr-19   12:23:12 PM
2024-25
Functions 145
The process of dividing a computer program into 
separate independent blocks of code or separate 
sub-problems with different names and specific 
functionalities is known as modular programming. 
In this chapter, we will learn about the benefits of 
this approach.
7.2 Funct Ions 
In programming, the use of function is one of the 
means to achieve modularity and reusability. Function 
can be defined as a named group of instructions that 
accomplish a specific task when it is invoked. Once 
defined, a function can be called repeatedly from 
different places of the program without writing all the 
codes of that function everytime, or it can be called from 
inside another function, by simply writing the name of 
the function and passing the required parameters, if 
any (Section 7.3). The programmer can define as many 
functions as desired while writing the code. The program 
7-1 is rewritten using user defined functions as shown 
in program 7-2.
Program 7-2 Program to calculate the payable 
amount  for the tent using user 
defined functions.
#Program 7-2
#Program to calculate the cost of tent
#function definition 
def cyl(h,r):
     area_cyl = 2*3.14*r*h    #Area of cylindrical part
     return(area_cyl)
#function definition
def con(l,r):
     area_con = 3.14*r*l      #Area of conical part
     return(area_con)
#function definition
def post_tax_price(cost):     #compute payable amount for the tent
     tax = 0.18 * cost;
     net_price = cost + tax
     return(net_price)
print("Enter values of cylindrical part of the tent in meters:")
h = float(input("Height: "))
r = float(input("Radius: "))
Ch 7.indd   145 08-Apr-19   12:23:12 PM
2024-25
Page 4


7.1 Introduct Ion Till now we have written some programs and might 
have realised that as the problem gets complex, the 
number of lines in a program increase, which makes the 
program look bulky and difficult to manage. Consider 
the following problem statement: 
There is a company that manufactures tents as per 
user’s requirements. The shape of the tent is cylindrical 
surmounted by a conical top.
“Once you succeed in writing 
the programs for [these] 
complicated algorithms, they 
usually run extremely fast. 
The computer doesn’t need 
to understand the algorithm, 
its task is only to run the 
programs.”
– R. Tarjan
Chapter 7
Functions
In this chapter
 » Introduction to 
Functions
 » User Defined      
Functions
 » Scope of a Variable
 » Python Standard 
Library
Figure 7.1: Shape of a tent
The company performs the following tasks to fix the 
selling price of each tent. 
1. Accept user requirements for the tent, such as
a) height 
b) radius 
c) slant height of the conical part
2. Calculate the area of the canvas used
3. Calculate the cost of the canvas used for making 
the tent
4. Calculate the net payable amount by the customer 
that is inclusive of the 18% tax
The company has created a computer program for 
quick and accurate calculation for the payable amount 
as shown in program 7-1.
Ch 7.indd   143 08-Apr-19   12:23:12 PM
2024-25
Computer SCien Ce – Cla SS xi 144
Program 7-1 Program to calculate the payable amount 
for the tent.
#Program 7-1
#Program to calculate the payable amount for the tent without 
#functions
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)
Another approach to solve the above problem is 
to divide the program into different blocks of code as 
shown in Figure 7.2. 
Figure 7.2: Calculation of the cost of the tent
Ch 7.indd   144 08-Apr-19   12:23:12 PM
2024-25
Functions 145
The process of dividing a computer program into 
separate independent blocks of code or separate 
sub-problems with different names and specific 
functionalities is known as modular programming. 
In this chapter, we will learn about the benefits of 
this approach.
7.2 Funct Ions 
In programming, the use of function is one of the 
means to achieve modularity and reusability. Function 
can be defined as a named group of instructions that 
accomplish a specific task when it is invoked. Once 
defined, a function can be called repeatedly from 
different places of the program without writing all the 
codes of that function everytime, or it can be called from 
inside another function, by simply writing the name of 
the function and passing the required parameters, if 
any (Section 7.3). The programmer can define as many 
functions as desired while writing the code. The program 
7-1 is rewritten using user defined functions as shown 
in program 7-2.
Program 7-2 Program to calculate the payable 
amount  for the tent using user 
defined functions.
#Program 7-2
#Program to calculate the cost of tent
#function definition 
def cyl(h,r):
     area_cyl = 2*3.14*r*h    #Area of cylindrical part
     return(area_cyl)
#function definition
def con(l,r):
     area_con = 3.14*r*l      #Area of conical part
     return(area_con)
#function definition
def post_tax_price(cost):     #compute payable amount for the tent
     tax = 0.18 * cost;
     net_price = cost + tax
     return(net_price)
print("Enter values of cylindrical part of the tent in meters:")
h = float(input("Height: "))
r = float(input("Radius: "))
Ch 7.indd   145 08-Apr-19   12:23:12 PM
2024-25
Computer SCien Ce – Cla SS xi 146
csa_cyl = cyl(h,r)  #function call 
l = float(input("Enter slant height of the conical area in meters: "))
csa_con = con(l,r)  #function call
#Calculate area of the canvas used for making the tent
canvas_area = csa_cyl + csa_con
print("Area of canvas = ",canvas_area," m^2")
#Calculate cost of canvas
unit_price = float(input("Enter cost of 1 m^2 canvas in rupees: "))
total_cost = unit_price * canvas_area
print("Total cost of canvas before tax = ",total_cost)
print("Net amount payable (including tax) = ",post_tax_price(total_
cost))
If we compare program 7-1 and 7-2, it is evident that 
program 7-2 looks more organised and easier to read.
7.2.1 The Advantages of Function
Suppose in further the company decides to design 
another type of tent whose base is rectangular, while 
the upper part remains the same. In such a scenario, 
some part of the existing code can be reused by calling 
the function con(l,r). If the company develops other 
products or provides services, and where 18% tax rate 
is to be applied, the programmer can use the function 
post_tax_price(cost)directly.
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.                                                                       
7.3 u ser d e FIned Funct Ions Taking advantage of reusability feature of functions, 
there is a large number of functions already available 
Ch 7.indd   146 08-Apr-19   12:23:13 PM
2024-25
Page 5


7.1 Introduct Ion Till now we have written some programs and might 
have realised that as the problem gets complex, the 
number of lines in a program increase, which makes the 
program look bulky and difficult to manage. Consider 
the following problem statement: 
There is a company that manufactures tents as per 
user’s requirements. The shape of the tent is cylindrical 
surmounted by a conical top.
“Once you succeed in writing 
the programs for [these] 
complicated algorithms, they 
usually run extremely fast. 
The computer doesn’t need 
to understand the algorithm, 
its task is only to run the 
programs.”
– R. Tarjan
Chapter 7
Functions
In this chapter
 » Introduction to 
Functions
 » User Defined      
Functions
 » Scope of a Variable
 » Python Standard 
Library
Figure 7.1: Shape of a tent
The company performs the following tasks to fix the 
selling price of each tent. 
1. Accept user requirements for the tent, such as
a) height 
b) radius 
c) slant height of the conical part
2. Calculate the area of the canvas used
3. Calculate the cost of the canvas used for making 
the tent
4. Calculate the net payable amount by the customer 
that is inclusive of the 18% tax
The company has created a computer program for 
quick and accurate calculation for the payable amount 
as shown in program 7-1.
Ch 7.indd   143 08-Apr-19   12:23:12 PM
2024-25
Computer SCien Ce – Cla SS xi 144
Program 7-1 Program to calculate the payable amount 
for the tent.
#Program 7-1
#Program to calculate the payable amount for the tent without 
#functions
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)
Another approach to solve the above problem is 
to divide the program into different blocks of code as 
shown in Figure 7.2. 
Figure 7.2: Calculation of the cost of the tent
Ch 7.indd   144 08-Apr-19   12:23:12 PM
2024-25
Functions 145
The process of dividing a computer program into 
separate independent blocks of code or separate 
sub-problems with different names and specific 
functionalities is known as modular programming. 
In this chapter, we will learn about the benefits of 
this approach.
7.2 Funct Ions 
In programming, the use of function is one of the 
means to achieve modularity and reusability. Function 
can be defined as a named group of instructions that 
accomplish a specific task when it is invoked. Once 
defined, a function can be called repeatedly from 
different places of the program without writing all the 
codes of that function everytime, or it can be called from 
inside another function, by simply writing the name of 
the function and passing the required parameters, if 
any (Section 7.3). The programmer can define as many 
functions as desired while writing the code. The program 
7-1 is rewritten using user defined functions as shown 
in program 7-2.
Program 7-2 Program to calculate the payable 
amount  for the tent using user 
defined functions.
#Program 7-2
#Program to calculate the cost of tent
#function definition 
def cyl(h,r):
     area_cyl = 2*3.14*r*h    #Area of cylindrical part
     return(area_cyl)
#function definition
def con(l,r):
     area_con = 3.14*r*l      #Area of conical part
     return(area_con)
#function definition
def post_tax_price(cost):     #compute payable amount for the tent
     tax = 0.18 * cost;
     net_price = cost + tax
     return(net_price)
print("Enter values of cylindrical part of the tent in meters:")
h = float(input("Height: "))
r = float(input("Radius: "))
Ch 7.indd   145 08-Apr-19   12:23:12 PM
2024-25
Computer SCien Ce – Cla SS xi 146
csa_cyl = cyl(h,r)  #function call 
l = float(input("Enter slant height of the conical area in meters: "))
csa_con = con(l,r)  #function call
#Calculate area of the canvas used for making the tent
canvas_area = csa_cyl + csa_con
print("Area of canvas = ",canvas_area," m^2")
#Calculate cost of canvas
unit_price = float(input("Enter cost of 1 m^2 canvas in rupees: "))
total_cost = unit_price * canvas_area
print("Total cost of canvas before tax = ",total_cost)
print("Net amount payable (including tax) = ",post_tax_price(total_
cost))
If we compare program 7-1 and 7-2, it is evident that 
program 7-2 looks more organised and easier to read.
7.2.1 The Advantages of Function
Suppose in further the company decides to design 
another type of tent whose base is rectangular, while 
the upper part remains the same. In such a scenario, 
some part of the existing code can be reused by calling 
the function con(l,r). If the company develops other 
products or provides services, and where 18% tax rate 
is to be applied, the programmer can use the function 
post_tax_price(cost)directly.
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.                                                                       
7.3 u ser d e FIned Funct Ions Taking advantage of reusability feature of functions, 
there is a large number of functions already available 
Ch 7.indd   146 08-Apr-19   12:23:13 PM
2024-25
Functions 147
in Python under standard library (section 7.5). We can 
directly call these functions in our program without 
defining them. However, in addition to the standard 
library functions, we can define our own functions while 
writing the program. Such functions are called user 
defined functions. Thus, a function defined to achieve 
some task as per the programmer's requirement is 
called a user defined function.
7.3.1 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.
Program 7-3 Write a user defined function to add 2   
numbers and display their sum.
#Program 7-3
#Function to add two numbers
#The requirements are listed below:
 #1. We need to accept 2 numbers from the user. 
 #2. Calculate their sum
 #3. Display the sum.
#function definition
def addnum():
    fnum = int(input("Enter first number: "))
    snum = int(input("Enter second number: "))
    sum = fnum + snum
    print("The sum of ",fnum,"and ",snum,"is ",sum)
    
#function call
addnum()
Ch 7.indd   147 15-Jun-21   11:16:13 AM
2024-25
Read More
33 docs|11 tests
Related Searches

pdf

,

NCERT Textbook: Functions | Computer Science for Class 11 - Humanities/Arts

,

practice quizzes

,

NCERT Textbook: Functions | Computer Science for Class 11 - Humanities/Arts

,

past year papers

,

shortcuts and tricks

,

study material

,

Important questions

,

Summary

,

Semester Notes

,

Exam

,

NCERT Textbook: Functions | Computer Science for Class 11 - Humanities/Arts

,

Objective type Questions

,

video lectures

,

Free

,

ppt

,

Extra Questions

,

mock tests for examination

,

MCQs

,

Sample Paper

,

Previous Year Questions with Solutions

,

Viva Questions

;