We have just seen how to vary the course of a program using loops and connections. Before that, I talked about variables. These are elements that are found in all programming languages. This is also the case of the notion that we will discuss in this chapter: functions.
All C ++ programs exploit functions and you have already used several, without necessarily knowing it.
The purpose of the functions is to cut out its program into small reusable elements, much like bricks. They can be assembled in a certain way to create a wall, or in another way to make a shed or even a skyscraper. Once the bricks are created, the programmer “just has to” assemble them.
Advantages of Function
Creating functions in a program is beneficial. They
- Avoid repetition of codes.
- Increase program readability.
- Divide a complex problem into many simpler problems.
- Reduce chances of error.
- Makes modifying a program becomes easier.
- Makes unit testing possible.
Components of Function
A function usually has three components. They are:
- Function prototype/declaration
- Function definition
- Function call
A simple function example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | #include <iostream> using namespace std; /* This function adds two integer values * and returns the result */int sum(int num1, int num2){ int num3 = num1+num2; return num3; } int main(){ //Calling the function cout<<sum(1,99); return 0; } |
User-defined functions
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | #include <iostream> #include <cmath> using namespace std; //Declaring the function sum int sum(int,int); int main(){ int x, y; cout<<"enter first number: "; cin>> x; cout<<"enter second number: "; cin>>y; cout<<"Sum of these two :"<<sum(x,y); return 0; } //Defining the function sum int sum(int a, int b) { int c = a+b; return c; } |
C++ Functions Examples
Example 1: Library Function
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | #include <iostream> #include <cmath> using namespace std; int main() { double number, squareRoot; cout << "Enter a number: "; cin >> number; // sqrt() is a library function to calculate square root squareRoot = sqrt(number); cout << "Square root of " << number << " = " << squareRoot; return 0; } |
Output:
1 2 3 4 | <samp>Enter a number: 26 Square root of 26 = 5.09902</samp> |
Example 2: User Defined Function
C++ program to add two integers. Make a function add()
to add integers and display sum in main() function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | #include <iostream> using namespace std; // Function prototype (declaration) int add(int, int); int main() { int num1, num2, sum; cout<<"Enters two numbers to add: "; cin >> num1 >> num2; // Function call sum = add(num1, num2); cout << "Sum = " << sum; return 0; } // Function definition int add(int a, int b) { int add; add = a + b; // Return statement return add; } |
Output:
1 2 3 4 5 | <samp>Enters two integers: 8 -4 Sum = 4</samp> |
Example 3: Check Prime Number
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | #include <iostream> using namespace std; int checkPrimeNumber(int); int main() { int n; cout << "Enter a positive integer: "; cin >> n; if(checkPrimeNumber(n) == 0) cout << n << " is a prime number."; else cout << n << " is not a prime number."; return 0; } int checkPrimeNumber(int n) { bool flag = false; for(int i = 2; i <= n/2; ++i) { if(n%i == 0) { flag = true; break; } } return flag; } |
Example 4:
Similar to the addition
function in the previous example, this example defines a subtract
function, that simply returns the difference between its two parameters. This time, main
calls this function several times, demonstrating more possible ways in which a function can be called.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | // function example #include <iostream> using namespace std; int subtraction (int a, int b) { int r; r=a-b; return r; } int main () { int x=5, y=3, z; z = subtraction (7,2); cout << "The first result is " << z << '\n'; cout << "The second result is " << subtraction (7,2) << '\n'; cout << "The third result is " << subtraction (x,y) << '\n'; z= 4 + subtraction (x,y); cout << "The fourth result is " << z << '\n'; } |
Example 5: C++ program to swap two values using function (call by reference)
This program swaps the value of two integer variables. Two integer values are entered by user which is passed by reference to a function swap() which swaps the value of two variables. After swapping these values, the result is printed.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | #include <iostream> #include <conio.h> using namespace std; void swap(int &, int &); // function prototype int main() { int a,b; cout<<"Enter two numbers: "; cin>>a>>b; cout<<"Before swapping"<<endl; cout<<"a = "<<a<<endl; cout<<"b = "<<b<<endl; swap(a,b); // function call by reference cout<<"After swapping"<<endl; cout<<"a = "<<a<<endl; cout<<"b = "<<b<<endl; getch(); return 0; } void swap(int &x, int &y) // function definition { x=x+y; y=x-y; x=x-y; } |
Output
1 2 3 4 5 6 7 8 9 | Enter two numbers: 19 45 Before swapping a = 19 b = 45 After swapping a = 45 b = 19 |
Example 6: C++ program to use a static member function
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | #include<iostream> #include<conio.h> using namespace std; class test { static int x; int id; public: test() { x++; id=x; } static void static_display() { cout<<"x = "<<x<<endl; } void display() { cout<<"id= "<<id<<endl; } }; int test::x; int main() { test o1,o2; test::static_display(); o1.display(); test::static_display(); o2.display(); getch(); return 0; } |
This program is similar to the previous program but here we have used static member function to display the content of static data member. Static member function are called using class name and scope resolution operator (::) as called in the program:
1 2 3 | test::static_display(); |
Since, static member function can’t access other members of a class except static data member, so a separate function is used to display them.
Output
1 2 3 4 5 6 | x = 2 id= 1 x = 2 id= 2 |