The best Notes for exam preparation of 2021-22 kpk class 12 computer science notes chapter number #6 functions.
Functions computer science notes chapter 6
Table of Contents
Q.4) Write a program with a function that takes two int parameters, adds them together, and then returns the sum.
Answer:
int sum(int x, int y); // Function prototype/declaration
int main()
{
int a,b;
cout << “Enter First Number : “;
cin >> a;
cout << “Enter Second Number : “;
cin >> b;
int c = sum(a, b);
cout << “The sum of two numbers is : ” << c << “\n\n\n”;
}
int sum(int x, int y) // Function definition
{
return (x + y);
}
Output:
The output of the program is as follows:

Q.5) Write a program with a function name “mean” to read in three integers from the keyboard to find the arithmetic mean.
Answer:
#include “stdafx.h”
#include <iostream>
#include<conio.h>
using namespace std;
void mean(int x, int y, int z)
{
int sum,arithMean;
sum = x + y + z;
arithMean = sum / 3;
cout << “Arithmetic Mean = ” << arithMean << “\n\n”;
}
int main()
{
int a, b,c;
cout << “Enter FIRST integer : “;
cin >> a;
cout << “Enter SECOND integer : “;
cin >> b;
cout << “Enter THIRD integer : “;
cin >> c;
mean(a, b, c);
return 0;
}
Output:
The output of the program is as follows:

Q.6) 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.
Answer:
#include “stdafx.h”
#include <iostream>
include<conio.h>
using namespace std;
void rectangle(int length, int width)
{
int area;
area = length*width;
cout << “Area of Rectangle is : ” << area << “\n”;
}
int main()//main program
{
int L, W;
cout << “Enter length and breadth of rectangle : “;
cin >> L >> W;
rectangle(L, W);
return 0;
}
Output:
The output of the program is as follows:

Q.7) Write a C++ program having two function names area and perimeter to find the area and parameter of a square.
Answer:
#include “stdafx.h”
#include <iostream>
#include<conio.h>
using namespace std;
void area(int length)
{
int area;
area = length*length;
cout << “Area of Square is = ” << area << “\n\n”;
}
void perimeter(int length)
{
int perimeter;
perimeter = 4 * length;
cout << “Perimeter of Square is = ” << perimeter << “\n\n”;
}
int main()
{
int L;
cout << “Enter LENGTH of Square : “;
cin >> L;
cout << “\n\n”;
area(L);
perimeter(L);
return 0;
}

Q.8) 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.
Answer:
#include “stdafx.h”
#include <iostream>
#include<conio.h>
using namespace std;
void primeComposite(int num1)
{
int x;
for (x = 2; x<num1; x++)
{
if (num1%x == 0)
{
cout << num1 << ” is a composite number.” << endl;
}
else
{
cout << num1 << ” is a prime number.” << endl;
}
}
}
int main()
{
int num1;
cout << “Enter an integer : ” << endl;
cin >> num1;
primeComposite(num1);
return 0;
}
Output:
Enter a positive integer: 23 23 is a prime number.
Q.10) Define function and differentiate between built-in and user-defined functions with the help of examples.
Answer:
FUNCTION
DEFINITION:
“A function is a group of statements that together perform a task.”
Every C++ program has at least one function, which is main(). Additional functions can be defined in the program. Each function performs a specific task.
DIFFERENCE BETWEEN BUILT IN AND USER DEFINED FUNCTIONS
The differences between Built-in and user-defined functions are as follows:
BUILT IN OR LIBRARY FUNCTIONS | USER DEFINED FUNCTIONS |
Library functions are Predefined functions. | User-defined functions are the function which is created by the user as per his own requirements. |
Library functions are part of header file (such as MATH.h) which is called runtime. | User-defined functions are part of the program which compile runtime |
In Library functions, developers give it. | In User-defined functions the name of function id decided by user |
Library functions are Name of function cannot be changed. | In User defined functions name of function can be changed any time |
Example : SIN, COS, Power | Example : fibo, mergeme |
Q.11) How function prototype and declarator differ from each other? Explain with the help of examples.
Answer:
Function prototype/declaration
Function prototype/declaration is done to tell the compiler about the existence of the function. Function’s return type, its name, and parameter list are mentioned. A function prototype/declaration is used before the main() function. It ends with a semicolon (;). The function prototype is used in C++ program only when the function is defined after the definition of main() function. If the function definition lies before the main() function then there is no need for the function prototype.
Example:
In the following program the portion in red indicates the function_prototype/declaration.
#include < iostream>
using namespace std;
int sum (int x, int y); // Function prototype/declaration
int main()
{
int a = 10;
int b = 20;
int c = sum (a, b);
cout« c;
}
int sum (int x, int y) // Function definition
{
return (x + y);
}
Q.12) Define default arguments. Describe the advantages and disadvantages of the default argument.
Answer:
DEFAULT ARGUMENTS
When declaring a function we can specify a default value for each of the last parameters. This value will be used if the corresponding argument is left blank when calling to the function. To do this, we simply have to use the assignment operator and a value for the arguments in the function declaration. If a value for that parameter is not passed when the function is called, the default value is used, but if a value is specified this default value is ignored and the passed value is used instead.
Related Post:
- Chapter #01 computer science Operating Systems
- Chapter #2 computer science (System Development Life Cycle)
- Chapter #3 computer science (Object Oriented Programming using C++)
- Chapter #4 computer science Notes computer science (Control Structures)
- Chapter #5 computer science Notes (Arrays and Strings)
- Chapter #7 computer science Notes (Pointers)
- Chapter #8 computer science Notes (Objects and Classes)
- Chapter #9 computer science Notes (File Handling)