Unit 2 Part 2
Unit 2 Part 2
• functions, programs are much larger than the programs that we have seen so far. To make large
programs manageable and less complicated, they are broken down into subprograms. These
subprograms are called functions.
• The basic principle of Functions is DIVIDE AND CONQUER. Using functions, we can divide
a larger task into smaller subtasks that are manageable.
• A function is a self-contained block of statements. A function is self contained in the sense it
may have its own variables and constants.
• It is designed to do a well-defined task. Since a function is a part of a larger program (i.e. a
subprogram) it has a particular job to perform.
• It has a name. A function can be used (invoked) by the name given to it.
• It may have return type. A function invoked by a calling program may or may not return a value
to it. In case it returns a value, the functions return type is the same as the variables data type.
• A program that has functions should have the following three things in it:
• Function Declaration or Prototype
• Function Call
• Function definition
Notes prepared by Prof. Anushree K. Rewale 117
• Why use it?
• Functions are a structured way to programming. Larger programs get divided into smaller manageable units.
• If a specific block of statements has to be executed multiple times ( for example. taking contact details from 100 users), it can be
written as a function and that function can be repeatedly executed. This implies that redundancy in writing the same piece of code
multiple times is removed.
• Dividing a large program into smaller subprograms using functions help to easily code them and reduces the debugging effort.
function2()
{
----------------
} Notes prepared by Prof. Anushree K. Rewale 118
• The program to the left contains three functions. First one is the main() function, second is
function1() and third is function2().
• The execution of any program begins with the execution of main function. Unless there is a
decision or looping construct the execution of the program proceeds in a serial manner.
• In the diagram to the left, the program execution begins with main(), all the statements get
executed.
• A function gets called when the function name is followed by a semicolon. A function is defined
when function name is followed by a pair of braces in which one or more statements may be
present.
• When the system encounters a call to function1() the program control jumps outside the main()
function to execute the block of statements named function1() shown by arrow number1.
• Once the last statement in function1() is executed the program control is again transferred to
main() and the immediate statement after main is executed, shown by arrow number2.
• When the system encounters a call to function2() the program control jumps outside the main()
function again to execute the block of statements named function2() shown by arrow number 3.
• Once the last statement in function2() is executed the program control is again transferred to
main() and the immediate statement after main is executed, shown by arrow number 4.
Notes prepared by Prof. Anushree K. Rewale 119
• Types of Functions:
• Functions are of two types
1. Built-in functions
2. User defined functions
• Built-in Functions:
• These are also called Standard Library Functions. As the name suggests it is a Library of functions. These are
the functions that are already present .i.e. predefined in the system.
• They have been written, compiled and placed in libraries under header files. We can use these functions in our
programs by just specifying the name of the header files that contains the function of our interest.
• Math Library Functions:
• C++ provides a library of math related functions called Math Library Functions. These functions are placed in
header file <math.h> and it contains 59 functions.
• Example : Print Square Root of Numbers from 1 to 10
#include <iostream>
#include<math>
int main()
{
cout<<“ Number "<<"\t Square Root \n";
for(int i=1;i<=10;i++)
{
cout<<i<<"\t"<<sqrt(i)<<endl;
}
return 0;
Notes prepared by Prof. Anushree K. Rewale 120
}
• User defined functions:
• These are the functions other than the Standard Library Functions. These are created by the users and the
user has the flexibility to choose the function name, the statements that will be executed, the parameters
that will be passed to the user & the return type of the function.
• Any program using functions will have the following three necessary things:
1. Function prototype or function declaration - It is the name of the function along with its return-type and
parameter list.
2. Function call - Any function name inside the main() followed by semicolon (;) is a Function Call.
3. Function Definition - A function name followed by a pair of parenthesis {} including one or more statements.
• Consider the following example:
return-type function1();
int main()
{
function1();
return 0;
}
function1()
{
----------------
return();
Notes prepared by Prof. Anushree K. Rewale 121
}
• The first line of the above example return-type function1(); is called the function prototype or
function declaration. It is used to declare the function to the compiler. This statement is always
written outside(before) the main().
• The statement function1() along with the statements in the parenthesis shown below is called the
function definition. The function definition contains the instructions to be executed when the
function is called. Function definition is always done outside the main().
• The statement function1(); inside main() is a function call. A function gets called when the function
name is followed by a semicolon. A function gets called when the function name is followed by a
semicolon.
• When this statement function1(); is executed the program control gets transferred to the function
definition of function1() which is outside the main(). All the statements inside function1() are
executed and then the control gets transferred to the next statement after the function call.
Notes prepared by Prof. Anushree K. Rewale 122
• Example:
#include <iostream>
int square(int m); // Function Prototype
int main()
{
int num, sqr=0;
cout<<"\nEnter number to find its Square"<<"\t ";
cin>>num;
sqr=square(num); // Function call
cout<<"\nSquare of "<<num<<" = " <<" = " <<sqr;
return 0;
}
int square(int x) // Function definition
{
return x*x; // returns square of x:
} Notes prepared by Prof. Anushree K. Rewale 123
• Function Definition:
• A function is defined when function name is followed by a pair of braces in
which one or more statements may be present.
• A function definition has 2 parts:
• Function head
• Function Body
• Example :
int square(int x)
{
return x*x; //returns square of x:
}
• The function returns the square of the integer passed to it. Thus the call
square(3) would return 9.
Notes prepared by Prof. Anushree K. Rewale 124
• Function Head
• The syntax for the head of a function is
return-type name(parameter-list)
• The above statement tells the compiler three things about the function:
• Name of the function.
• Its return-type i.e type of value to be returned by the function.
• Its parameter list.
• In the example shown above the head of the function is:
int square(int x)
• square is the name of the function,
• int is the type of value that the function is returning to main()
• and the parameter list (int x) contains a single parameter that is passed to the function
square by the main()
Notes prepared by Prof. Anushree K. Rewale 125
• Function Body
• The body of a function is the block of code that follows its head.
• It contains the code that performs the function’s action.
• It includes the return statement that specifies the value that the function sends back to the
place where it was called usually main().
• The body of the square function is
{
return x*x; // returns square of x:
}
• Pass by Reference(Call by Reference): Both actual and formal parameters refer to the same
locations, so any changes made inside the function are reflected in the actual parameters of the caller.
• Such function calls are called ‘call by value’. In call by value the changes made to the formal
parameters do not change the actual parameters.
• The called function creates a new set of variables and copies the values of actual arguments into
formal arguments.
• The function does not have access to the variables declared in the calling program and can only work
on the copies of values i.e. the formal arguments.
• Example:
int a = 20;
a = a + 10;
//function calls
int mul = multiply(10, 20);
int mul = multiply(10, 20,3);
double mul = multiply(2.5, 3.5);
double mul = multiply(1.2, 3);
double mul = multiply(2, 3.5); Notes prepared by Prof. Anushree K. Rewale 136
//function definitions
int multiply(int m, int n)
{
return (m*n);
}
int multiply(int m, int n, int p)
{
return (m*n*p);
}
double multiply(double m , double n)
{
return (m*n);
}
double multiply(double m , int n)
{
return (m*n);
}
double multiply(int m, double n)
{
return (m*n);
}
Notes prepared by Prof. Anushree K. Rewale 137
• Example:
• Program to do arithmetic operations using function and switch case.
Function to Calculate Different Areas.
• Suppose we want to create a function to calculate the area of
different geometric figures. It makes more sense to have a function
area() which calculates different actions on different types of inputs
like :
• When given a single value, calculate the area of a square.
• For two input returns area assumes the figure is a rectangle.
• When a floating-point number is passed to the function, it calculates and returns the area
of a circle.