The C++ functions are of many types, but we broadly classify them into four categories – functions that return a value, function that does not return a value, function with parameters, and function without parameter. You many find functions that fall under more than one categories.
In this article, you will learn about function each of the function types in more detail.
Function without return
A C++ function need not return anything. It can compute and display the results. In such cases, you can use the keyword void to indicate that a function does return a value.
For example,
void sqaure ( int x);
The above is a valid declaration. You must use the same declaration in function definition.
void square (int x)
{
int value = x * x;
cout << "Square of x =" << " " << x << endl;
}
Example Program:
This example program takes three input parameters and computes cube of a number and prints the output, rather than returning the value.
//Function that does not return values
#include <cstdlib>
#include <iostream>
using namespace std;
int main()
{
//variable and function declaration
int x;
int cube(int);
//reading the value
cout << "Enter a number to find it's Cube:";
cin >> x;
//calling function cube(x)
cube(x);
system("PAUSE");
return EXIT_SUCCESS;
}
//function definition for cube(X)
int cube( int x)
{
//Local variable declaration and initialization
int cube_value = 0;
//compute the cube value
cube_value = x * x * x;
//printing output
cout << "Cube Value =" << " " << cube_value << endl;
}
Output:
Enter a number to find it's Cube:23
Cube Value = 12167
Function with return value
A C++ function if returns a value, then the returned value can be used for further computation. You can assign the return value to another variable like any other variable assignment.
For example,
int a,b,c,d;
int addition(int, int);
int multiplication( int, int);
int equation_value = 0;
a = 10;
c = d = 0;
b = 4;
// equation
c = addition(a, b);
d = multiplication( a, b);
equation_value = c + d;
// printing output
cout << "Equation Value =" << equation_value << endl;
In the above example, you can see that the returned value from function addition()
and multiplication()
is used for computing equation_value.
Example Program:
This example contains code from previous example given above.
//Function with return value
#include <cstdlib>
#include <iostream>
using namespace std;
int main()
{
//Function and Variable Declarations
int a,b,c,d;
int addition(int, int);
int multiplication( int, int);
int equation_value = 0;
//Variable initialization
c = d = 0;
//Reading input values
cout << "Enter A value:";
cin >> a;
cout << endl;
cout << "Enter B value:";
cin >> b;
// Computing equation value
c = addition(a, b);
d = multiplication( a, b);
equation_value = c + d;
// Printing output
cout << endl;
cout << "Equation Value =" << " " << equation_value << endl;
system("PAUSE");
return EXIT_SUCCESS;
}
//function addition
int addition(int a, int b)
{
return(a + b);
}
//function multiplication
int multiplication(int a, int b)
{
return(a * b);
}
Output:
Enter A value:23
Enter B value:5
Equation Value = 143
Function Without Parameters
A function can be without any parameters. All variables required for this type of function is either declared globally, or declared locally inside the function.
For example, consider the following program, that make use of both global and local variable for a function without any parameters.
Note that a function can either return a value or return no value even if it has no parameters.
Example Program: function without parameters
//Function without parameters
#include <cstdlib>
#include <iostream>
//Global Variable Declarations and initialization
int temp = 0;
using namespace std;
int main()
{
//Variable and Function Declaration
int total_count;
int counter();
//Variable Initialization
total_count = 0;
//Calling Function Counter()
total_count = counter();
//Printing Output
cout << "Total Count =" << " " << total_count << endl;
system("PAUSE");
return EXIT_SUCCESS;
}
//Function Definition Counter()
int counter()
{
//Local Variable Declaration
int i, n;
//Read number of values
cout << "How many Values:";
cin >> n;
//start counting
for(i=1;i <= n;i++)
{
// Using the global variable temp
temp = temp + i;
}
return(temp);
}
Output:
How many Values:34
Total Count = 595
The program takes no parameters, but use the global variable temp
and a local variable i
to compute sum of all numbers up to user input number n
. The temp is returned after the function counter()
terminates successfully.
Functions With Parameters
The functions with parameters takes formal and actual parameters. The parameter type must match in function declaration and function definition.
A function with parameter may or may not return a value. It depends on the programmer implementing the function, or the requirement of the programmer.
For example, consider the following program.
Example Program: function with parameters
//Function with Parameters
#include <cstdlib>
#include <iostream>
using namespace std;
int main()
{
//Variable and Function Declaration
float principal,amount;
int year;
float rate,SI;
float simple_interest(float , float , int);
principal = amount = rate = SI = 0.0;
year = 0;
// Reading input values
cout << "Enter Principle:";
cin >> principal;
cout << endl;
cout << "Enter Rate of Interest in percentage(%):";
cin >> rate;
cout << endl;
cout << "Enter Number of Years:";
cin >> year;
//Compute simple interest
SI = simple_interest(principal,rate,year);
amount = SI + principal;
//Printing results
cout << endl;
cout << "Simple Interest =" << " " << SI << endl;
cout << "Total Amount =" << " " << amount << endl;
system("PAUSE");
return EXIT_SUCCESS;
}
//Function definition simple_interest()
float simple_interest(float principal, float rate, int year)
{
float total = 0.0;
rate = rate/100;
total = principal * rate * (float)year;
return(total);
}
Output:
Enter Principle:10000
Enter Rate of Interest in percentage(%):10.56
Enter Number of Years:3
Simple Interest = 3168
Total Amount = 13168
The above example program computes simple interest for principal amount, at a rate in percentage, for a period of time calculated annually. The principal
, rate
, and year
are the parameter for the function simple_interest()
which takes these parameters, computes and returns the simple interest.