C++ → Basics → Input → Arithmetic → Conditions → Loops → Array → Multidimensional Arrays → Functions
The functions in C++ Construction and Examples. We can also make functions outside the main function.
First of all consider this simple C++ program.
#include <iostream>
using namespace std;
int main()
{
cout << "Print it on the screen!" << endl;
return 0;
}
This small program prints the sentence on the screen. In the next example we make this program outside the main function, and then will call it inside the main function. So, when we call the function, it prints the sentence on the screen.
#include <iostream>
using namespace std;
void myfunc() //our new function
{
cout << "Print it on the screen!" << endl;
}
int main()
{
myfunc(); //call the function
return 0;
}
Here we made our new function before the main function. But, the best method is to make functions after the main function. Like,
#include <iostream>
using namespace std;
void myfunc(); //function deceleration
int main()
{
myfunc(); //call the function function
return 0;
}
void myfunc() //our new function
{
cout << "Print it on the screen!" << endl;
}
Notice that in this case we have initialize the function first (before the main function). And then made the function after the main function.
Call the Function for any times you want
We can call a function for any number of times we want. For example
#include <iostream>
using namespace std;
void myfunc();
int main()
{
myfunc();
cout << "\nID Name" << endl;
myfunc();
}
void myfunc()
{
for (int i=0; i<20; i++)
{
cout << "*";
}
}
The output of the above program is
********************
ID Name
********************
Passing parameters to the functions
We can also pass parameters to the functions. Like
#include <iostream>
using namespace std;
void favoriteNumber(int); //function deceleration
int main()
{
favoriteNumber(40); //call the function
return 0;
}
void favoriteNumber(int a) //our new function
{
cout << "My favorite number is: " << a << endl;
}
In function deceleration void favoriteNumber(int); there is only parameter type (int). And in function void favoriteNumber(int a); there is also variable name (int a). Also note that there is one variable in the function, so, when we call the function favoriteNumber(40); we have given only one number (integer).
Functions of type int
In the above functions we have not return any value. When we have to return some values, we make functions of type int, double etc. For example, the following program adds two given numbers and return their sum.
#include <iostream>
using namespace std;
int addition(int, int); //two parameters of int type
int main()
{
int result = addition(4,8); //two integer parameters
cout << result << endl; //display result
return 0;
}
int addition(int a, int b) //two parameters with names
{
return a + b;
}
The return type of return a+b; is of integer (because it is adding two integers). That is why, when we call the function we have write int result = addition (4,8);
Functions with input
The following program take two inputs from the user and add them. Note that as we are taking input from the user, so we do not need to insert parameters.
#include <iostream>
using namespace std;
int inputAddition(); //no parameters
int main()
{
int result = inputAddition();
cout << result << endl; //display result
return 0;
}
int inputAddition()
{
int a;
int b;
cout << "Please insert number a: " << endl; cin >> a;
cout << "Please insert number b: " << endl; cin >> b;
return a + b;
}
Similarly, the following program will ask user to insert ten numbers and will show sum of these values. We use array to input ten values.
#include <iostream>
using namespace std;
int inputAddition (); //function deceleration
int main()
{
int result = inputAddition(); //call the function
cout << "Addition of 10 numbers = " << result << endl;
return 0;
}
int inputAddition() //our function
{
int myarray [10]; //array of size ten
int addition = 0;
cout << "Kindly insert any 10 numbers:" << endl;
for (int i =0; i<10; i++)
{
cin >> myarray[i];
addition = addition + myarray[i]; //add the numbers
}
return addition;
}