CBSE Class 11 Computer Science Functions In C++ Notes

Download CBSE Class 11 Computer Science Functions In C++ Notes in PDF format. All Revision notes for Class 11 Computer Science have been designed as per the latest syllabus and updated chapters given in your textbook for Computer Science in Standard 11. Our teachers have designed these concept notes for the benefit of Grade 11 students. You should use these chapter wise notes for revision on daily basis. These study notes can also be used for learning each chapter and its important and difficult topics or revision just before your exams to help you get better scores in upcoming examinations, You can also use Printable notes for Class 11 Computer Science for faster revision of difficult topics and get higher rank. After reading these notes also refer to MCQ questions for Class 11 Computer Science given our website

Functions in C++

Objectives :


• to analyze how modularity is implemented in a program at its lowest level.


  To appreciate the use and importance of function in C++


  to program different types of functions and implement them practically.


  To understand difference between User Define Function and Library function.


3.1: Why to use functions ?

Who likes unnecessary repetition of task ? No body in this world. Every body thinks of re usability these days in every aspect of life. What will you do if your cycle wheel rim breaks down on fine day? Do you will throw the whole cycle or sell the cycle as scrap? No exactly not because cycles are designed in such a way that each of its parts can be repaired or replaced. So you will get a new cycle rim from market and will get it fitted in your cycle! This design of cycle where each of its parts have its own unique functionality and could be reassembled together to form a complete cycle is known as


Modular approach of designing. Each of the cycles part can thought as a Module which serves some purpose in the whole cycle but is very essential for proper functioning of the cycle. The whole concept is nothing but based on “Divide and Rule philosophy”. A bigger system is divided into smaller components so that each of these smaller parts could handled easily and effectively. These smaller parts when integrated gives rise to the bigger system. 

Just think GOD has also created human beings as a modular entity ! We humans have a body which is integration of organ system and each of these organ system is again integration of some organs. So here organs are acting as modules. These modules (organs) could be taken care of individually when we often fall ill.

Can you rightly describe what is opposite word for modularity ? Any system which is not modular is known as monolithic (अखंड) or indivisible. A monolithic system does not have any parts or modules, from top to bottom it is one piece.

Software industry has also adopted the modular approach of design where a big software is divided into several modules. Each of the modules are designed for performing specialized task in the whole software. These modules interact with other modules of the system to carry out essential functionality of the whole system.

Each module during its course of execution repeats same type of task, so whenever the whole system requires a specific type of task , for which a particular module is responsible , it calls or invokes that module and the task is done. This calling of module to perform a certain action , can be done several number of times while the software as a whole executes.

Let us understand the above concept with the help of a real life example. Suppose our KVS is going to develop a centralized software for managing all Kvs across the country. While designing such a software KVS has to divide the whole operation of the software into three big modules called as : Admin , Academic and Accounts, each of these modules could be again broken down into many simple and small sub-modules like Admin Module can have Admission , Construction , Recruitment, etc. whereas the Academics can again have sub-modules like Student Registration, Examination , Results etc. 

class_11_Computer_science_concept_1

When the whole software is divided into modules as seen in the above case scenario then the following benefits could be harvested :

i) Each module can be tracked individually and separately without taking much care of other modules.

ii) A module is a reusable piece of program which could used again and again. Suppose that Navodaya Vidyalaya now wants to make a software like KVS then they can re-use the same modules of KVS with some changes (customization).

iii) If error is found in one module the functionality of that particular module and its associated modules would be disturbed, the whole software will not suffer. Thus errors can be tracked easily and debugged in much less time, because programmer will know which module is causing error, so he will debug that particular module only not the whole software (much like when you visit doctor suffering from common cold , the doctor does not checks your brain!)

iv) System up gradation (the process of changing from older system to newer one) becomes much easier because only those modules which needs up gradation will be dealt leaving other things as it is. So we see that modularization of a system gives us much independence and flexibility in terms of fast program development , easier debugging , and re-usability.

 

How Functions in C++ are related to program modules :

 

Just as a software is divided into modules , each modules into sub-modules , a sub-module is further divided into several functions. So we may call a function as a micro-level module of a bigger software.

 

A function in C++ :

 

- is smaller section of code of bigger module/program.

 

- is re-usable piece of code.

 

- is very specific in nature as it performs a specific task.

 

- is often called many times in a program.

 

Thus a C++ function have all the advantages which a module has in a software.

 

3.2: Types of function :

 

Functions in C++ are of two basic types :

 

a) User Defined : written by a programmer as per his/her requirement domain.

 

b) Library Function : already available with C++ compiler and stored as Library, from

 

where they can be called and used in any C++ program.

 

3.2.1 : User Defined Functions :

 

A user define function in C++ is always created by programmer according to his/her program requirements. Suppose, if some programmer is making software for school management then his list of user defined functions may have functions such as : getFee( ) , calcResult( ) , setExam( ) , all these functions will be used only in school management software not any where else, as they are specially tailored function for school management software.

 

Let us see the syntax to declare a user defined function :

 

Function Declaration :

 

<return type> function_name( <parameter list> ) ;

 

where :

 

return type := is the value which the function returns , if function does not returns any

 

value then we may write there void.

 

function_name := any valid C++ identifier name

 

parameter list := declaration of variables of different data types separated by comma these values are inputs passed from outside to the function.

 

The function declaration as per the syntax given above is also called as prototype declaration. In C++ it is compulsory to declare prototype of a function before defining and using it .

 

The parameter variable in the declaration are also called Formal parameters.

 

Function Definition :
 
While function definition described about the structure of a function , its inputs and output type , the definition of function actually implements the code of the function. While defining a function we add C++ code to its block as per requirement.
Syntax : <return type> function_name( <parameter list> )
{ … }
Example : Declare and define a function which finds the sum of two integers and returns it.
int getSum( int , int ); // declaration of a function
int getSum( int a , int b ) // definition of the function
{
int r = a+b;
return r;
}
 
The above function declaration has a return type as integer , because the function is meant to return a sum of two numbers. Two numbers to be added are passed to the function as input parameter. The parameter list is having two int separated by a comma ( , ) it is not compulsory to write a variable names of the parameters in declaration. A semicolon is terminating the declaration of function.
The definition of function is having code written within its scope where the sum is calculated over the passed parameters a and b and the result is returned using a keyword return. It is compulsory that the return data type must be same as that of the datatype of the variable returned using return statement.
 
Workout yourself :
 
Declare the prototype of a function which :
i) multiplies three integers and return the product
ii) checks whether a passed integer parameter is even or odd
iii) prints your name 20 times.
 
Consider the few more definition of functions related to various program :
Function 3.1 :
// function to check whether a given number is prime or not
int isPrime(int );
int isPrime(int num )
{
int count = 0;
for( int i = 1 ; i <= num ; i++)
if( num % I == 0)
count++;

 

if (count > 2 )

 

return 0 ; // more than two factors means it is not prime , hence a false value is returned

 

else

 

return 1 ; // exactly two factors i.e. 1 and num itself means num is prime , hence return a

 

// true value i.e. 1

 

}

 

In the above function if the passed parameter to the function i.e. num would be a prime it will have exactly two factors counted out in variable count and if not would have more than 2 factors. After we conduct a looping over the num to check its divisibility by every value from 1 to num we get count incremented whenever num is divisible by i (looping-variable ). So on the termination of loop the variable count stores the total number of times num gets divisible in the loop.
We check this count value to find whether it is more than two or not, if it is more than two it means num has more than two factors and hence it does not satisfies to be a prime , hence we return an

 

integer 0 to designate that it is not prime , other wise we return 1.
Instead of returning 1 and 0 from function you directly print using cout that num is prime or not, but then don't forget to change the return type of the function isPrime( ) to void.


Please click the link below to download pdf file for CBSE Class XI Computer Science Functions in C++ Concepts.

Tags: 

 


Click to View or Download pdf file
Click for more Computer Science Study Material

Latest NCERT & CBSE News

Read the latest news and announcements from NCERT and CBSE below. Important updates relating to your studies which will help you to keep yourself updated with latest happenings in school level education. Keep yourself updated with all latest news and also read articles from teachers which will help you to improve your studies, increase motivation level and promote faster learning

CBSE asks schools to load Class 12 Internal Assessment Marks

This has a reference to the letter no CBSE/COORD/2021 dated 11.02.2021 regarding conduct of the School-Based Assessment (Internal Assessment/Practical/ Project) of Class-XII and uploading the marks by 11.06.2021. It has been observed that some schools have not been...

CBSE Class 12 Evaluation Criteria 2021

SUMMARY OF THE POLICY FOR TABULATION OF MARKS FOR CLASS XII BOARD EXAMINATIONS 2021   1(a) Due to cancellation of the Board examinations, the assessment of theory portion of 80/70/60/50/30 marks will be done by the school based on the following:     Class...

FAQs on Policy for Tabulation of Marks for Class 10 Board Exams

ON POLICY Question.1 How CBSE will declare the result of Class-X? Answer. Results of Class X Board will be declared on the basis of an objective criterion developed by the Board vide Notification no.CBSE/CE/2021 dated 01.05.2021. Question.2. If any candidate is not...

Moderation of Marks Class 11 and 12 Board Exams

The portal for moderation and finalization of results for Class-12 is being opened from 16.07.2021 to 22.07.2021. As Board has to declare the result latest by 31.07.2021, schools have been requested to follow the schedule strictly and complete the moderation within...

CBSE Board Examination Scheme of Assessment 2021 2022

A. Academic session to be divided into 2 Terms with approximately 50% syllabus in each term: The syllabus for the Academic session 2021-22 will be divided into 2 terms by following a systematic approach by looking into the interconnectivity of concepts and topics by...

×
Studies Today