Unit 6 FUNCTIONS
UNIT 6:
FUNCTIONS
EXERCISE NOTES
1
Unit 6 FUNCTIONS
Q1. What is a function? Explain different types of functions used in C+ + with
examples.
(Refer NBF book Page no. 117 to 119)
Q2. Explain function components with examples.
(Refer NBF book Page no. 120 to 122)
Q3. Define default arguments. Give the advantages and disadvantages of default
argument.
(Refer NBF book Page no. 134 to 135)
Q4. What is meant by the term function overloading? How a function can be
overloaded in C+ +? Explain it with the help of an example program.
(Refer NBF book Page no. 136)
Q5. What is function signature? Explain its different parts.
(Refer NBF book Page no. 120)
Q6. Explain the scope of different types of variables used in functions.
(Refer NBF book Page no. 122 to 125)
Q7. What are parameters? Explain their types with examples.
(Refer NBF book Page no. 125 to 126)
2
Unit 6 FUNCTIONS
LAB ACTIVITIES:
(i) Write a program with a function that takes two int parameters, adds them
together, and then returns the sum.
Program:
#include<iostream>
using namespace std;
int sumup(int a, int b);
int main()
{
int a , b,
cout << "Enter First int Parameter:"
cin >> a;
cout << “Enter Second int Parameter:";
cin >> b;
int sum = sumup(a, b);
cout << “Sum of both parameters is " << sum << endl;
}
int sumup(int a , int b)
{
return a+ b;
}
(i) Write a program with a function name "mean" to read in three integers from the
keyboard to find the arithmetic mean.
Program.
#include<iostream>
using namespace std;
void mean(int a, int b, int c);
int main()
3
Unit 6 FUNCTIONS
{
Int a , b, c;
mean(a, b, c);
}
void mean(int a , int b, int c)
{
cout << “Enter three integers ";
cin >>a>>b>>c;
double means = (a + b + c) /3.0 ;
cout << "Arithmetic Mean is : " << means << endl;
}
(iii) Write a C++ program having a function name rectangle to read the length and
width of a rectangle from the keyboard and find the area of the rectangle. The
result should be returned to the main program for displaying on the screen.
Program:
#include<iostream>
using namespace std,
int area(int len, int wid);
int main()
{
int length, width, area of _rectangle;
cout << “"Enter length:”;
cin >> length;
cout << "Enter width: “;
cin >> width;
area_of_rectangle = area(length, width);
cout << "Area of the rectangle is: " << area_of_rectangle << endl;
}
int area(int len, int wid)
{
return len * wid;
}
4
Unit 6 FUNCTIONS
(iv) Write a C++ program having two function names area and perimeter to find the
area and perimeter of a square.
Program:
#include<iostream>
using namespace std;
int area(int a),
int perimeter(int a);
int main()
{
int area_of_square, perimeter_of_square, a;
cout << “enter the Length of 3 Square: "
cin > a;
area_of_square = area(a);
cout << "Area of square is : " << area_of_square << endl;
perimeter of _square = perimeter(a);
cout << "Perimeter of square: " << perimeter_of_square << endl;
}
int area(int a)
{
return a*a;
}
int perimeter(int a)
{
return 4*a;
}
(v) Write a C+ + program to read a number from the keyboard and then pass it to a
function to determine whether it is prime or composite.
Program:
#include <iostream>
using namespace std;
char isPrime(int num);
5
Unit 6 FUNCTIONS
int main()
{
int num;
char flag;
cout<<"Enter any number (it should be positive integer) ";
cin>>num;
flag = isPrime(num);
if (flag==t)
cout<<num<<” is a prime number”;
else
cout<<num<<” is not a prime number, it is composite number”;
return 0;
}
char isPrime(int num)
{
char flag=t;
for(int i = 2; 1 <= num / 2; i++)
{
if(num % 1 == 0)
{
flag = f;
break;
}
}
return flag;
}
(vi) Write a C+ + program to get an integer number from keyboard in main program
and pass it as an argument to a function where it calculate and display the table.
Program:
#include<iostream>
using namespace std;
void table(int a);
6
Unit 6 FUNCTIONS
int main()
{
int a=0;
cout<<"Enter Number:”;
cin»a;
table(a);
}
void table(int a)
{
cout << endl;
for (int i = 1; i <= 10; i++)
{
cout << a << " * << i << " =" <<a*i < endl;
}
cout << endl;
}