Chapter 8 PDF
Chapter 8 PDF
A function is a block of code that performs a specific task. C++ allows you to define
functions according to your need. These functions are known as user-defined functions.
For example: Suppose, you need to create a circle and colour it depending upon the radius
and colour.
User-defined functions help to decompose a large program into small segments which
makes program easy to understand, maintain and debug. If repeated code occurs in a
program, function can be used to include those codes and execute when needed by calling
that function.
Return-type function-name(parameters)
Statements;
A function may or may not return any value. Function name specifies the unique name for
the function. In the function add, int a and int b are integer type variables which will receive
some data from the main function, when the main function is called. The function works on
the data received.
int r=a+b;
return r; }
The data type of data being returned from return statement and the return type of the
function should be the same. The function should be declared before the main function. At
the time of calling the function, main function should be able to recognise which function is
the data to be passed and received from.
The data type of the variable storing the value at the time of calling function the function
should be same as the return data type of the function.
1) Write a C++ program using User Defined Function that accepts 2 numbers and
displays the sum.
#include <iostream.h>
#include<conio.h>
int add(int a, int b)
{
return (a+b);
}
int main()
{
int a, b, result;
cout<<"Enter the two numbers to be added: ";
cin>>a>>b;
getch();
2. Write a C++ program using User Defined Function that accepts 2 numbers and an
arithmetic operator from a given menu. According to the operator selected the
program calculates and displays the sum, difference, product and quotient and
prompts the user if he wants to continue or exit.
Use separate user defined function for each calculation.
#include<iostream.h>
#include<conio.h>
int add(int a, int b)
{
int r=a+b;
return r;
}
int sub(int a, int b)
{
int r=a-b;
return r;
{
int mul(int a, int b)
{
int r=a*b;
return r;
}
int div(int a, int b)
{
int r=a/b;
return r;
}
void main()
{
clrscr();
int n1,n2,result;
char op;
cout<<" Calculator for 5 calculations : \n ";
for(int j=0;j<5;j++)
{
cout<<" \n Enter 1st number : ";
cin>>n1;
cout<<" \n Enter 2nd number : ";
cin>>n2;
cout<<" \n Enter the operator +,-,*,/ or E to Exit : ";
cin>>op;
if(op == '+')
{
result=add(n1,n2);
cout<<" \n Addition is = "<<result<<"\n";
}
else if(op == '-')
result=sub(n1,n2);
cout<<" \n Subtraction is = "<<result<<"\n";
}
else if(op == '*')
{
result=mul(n1,n2);
cout<<" \n Multiplication is = "<<result<<"\n";
}
else if(op == '/')
}
result=div(n1,n2);
cout<<" \n Division is = "<<result<<"\n";
}
else if((op == 'e') || (op == 'E'))
{
cout<<" \n Thank You ";
break;/*To end the loop*/
}
else
{
cout<<" \n Invalid input ";
break;
}
getch();
}
3. Write a program to find the greater of two numbers using user defined function.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int p,q;
cout<<"Program to compare two numbers"<<endl;
cout<<"Enter first number";
cin>>p;
cout<<"Enter second number";
cin>>q;
compare(p,q); //calling function
getch( );
}
Array
An array is used to store a collection of data of same data type. The maximum number an
array can store depends upon the size of the array. The element in array starts from 0 index.
Eg : Datatype arrayName[arraySize]
int arr[5]
4. Write a program using an array to take five numbers from the user and display them.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int num[5], i;
cout<<"Enter five numbers into an array"<<endl;
for(i = 0; i < 5; i++)
{
cin>>num[i];
}
cout<<endl;
cout<<"Displaying Array Elements"<<endl;
for(i = 0; i < 5; i++)
{
cout<<num[i];
}
getch();
}
5. Write a program using an array to take five numbers from the user and display their
sum and average.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int num[5], i, sum = 0, avg;
cout<<"Enter five numbers into an array"<<endl;
for(i = 0; i < 5; i++)
{
cin>>num[i];
}
cout<<endl;
cout<<"Addition of array elements"<<endl;
for(i = 1; i < 5; i++)
{
sum = sum + num[i];
}
avg = sum/5;
cout<<”Sum is :”<<sum<<” \n Average is :”<<avg;
getch();
}