TYPE OF FUNCTIONS

The type of a function determines the kind of a type will be data that function returs to the calling code and he is assigned in declaration of the function. The function type is defined before the name of the function as shown in next example.
double square(double a);
float squareroot(float a);
char *data();
As we can see from previous examples, the square function is of type double, the squareroot function is of type float and the data function is of type char * (char is short for character) with the pointer so it returns a pointer to a character. Function generally can be user type defined. The precise type that function returns is determined by a command return in the definition of the function
float abs(float x) {
       return(x >= 0) ? x : -x;
}
In this example the conditional operator works in the following way:  
If an argument x is greater or equal to zero, the result of an operator is equal to the value of an argument, and otherwise the result is argument with changed sign. With use of return command result of the operator is declared for the return value, execution of a function is terminated and the value is returned in program. The parameter of return value can be any number, variable or term which is calculated before terminating the functions. Be careful when you’re defining functions because the type of the result must be equal to the type of the function.

If the result of the command return is different from the type of functions, the result is transformed into a type of function. Let’s make a change in our function called absolute():
#include "stdafx.h"
#include "iostream"
using namespace std;
double absolute(float x){
       return (x >= 0) ? x : -x;
}
int _tmain(int argc, _TCHAR* argv[])
{
       float b;
       cout << "Enter a value of variable b:" << endl;
       cin >> b;
       cout << "The absolute vale of a b is:" << absolute(b) << endl;
       system("pause");
       return 0;
}

So the function will return data of double type even though the result of command return is float type. Of course there is no sense in defining defining function in this way because with transformation of result we don't get on precision of returning value. But there is a sense of defining a function in the following form:
#include "stdafx.h"
#include "iostream"
using namespace std;
long absolute(float x){
       return (x >= 0) ? x : -x;
}
int _tmain(int argc, _TCHAR* argv[])
{
       float b;
       cout << "Enter a value of variable b:" << endl;
       cin >> b;
       cout << "The absolute vale of a b is:" << absolute(b) << endl;
       system("pause");
       return 0;
}
Which will return an integer part of the argument. The result of conditional joining is again of float type but the function is declared as a long type, so the result will be only an integer type of a function
IMPORTANT: The function as a result can return data of any kind of type with the exception of arrays and function but it can return arrays and reference on that types.
In calling code the result of a function can be any kind of arbitrary expression. In particular, the previously defined function square() we can use in arithmetic expression on the left side of the joining operator, treating her as every other data double. For example:
#include "stdafx.h"
#include "iostream"
using namespace std;

double square(float); //this is only declaration of a function

int _tmain(int argc, _TCHAR* argv[])
{
       float a;
       cout << "Enter the value for variable a =" << endl;
       cin >> a;
       cout << "The result of a^2 = " << square(a) << endl;
       float cube = square(a) * a;
       cout << "The result of a^3 = " << cube << endl;
       float fifth = square(square(a));
       cout << "The result of a^5 = " << fifth << endl;
       system("pause");
       return 0;
}

double square(float x) {
       return x*x;
}
You can even settle a function call as an argument of a function call:
float xOn4 = square(square(x));
The result of the function can even be ignored. Function square() can be called like this:
       square(5);
Although the meaning of the previous command is questionable, this type of function call is allowed. If function doesn’t have to turn back value that can be arranged using the declaration type void. This type of declaration is used for generating messages or results that depends on argument values. For example, if we need a function which will print a value of squared number and the value will not be returned to calling code, we will declare and define function squre() like:
void printsquare(float x){
       cout << (x*x) << endl;
       return;
}
Because function type void doesn’t return anything to code inside function main the command return inside the printsquare() function shouldn’t contain any data. Moreover in these cases the command return can be ignored.
IMPORTANT: The return command is mandatory for exiting the function which in general returns some result. For function type void the return command can be left out – in that case compiler will realize that exit from this function is at the end of her definition.
Function of type void can’t be used in arithmetic operations, so we’ll always call previously defined function in the following way:
       printsquare(10);
The next type of function call will cause the error during execution of program:
       float a = printsquare(10); // error
The return from the function is possible from every point inside the function. So command return can be positioned on more points inside the function. Let’s take a closer look at following example:
void statement(int answer) {
       switch (answer){
       case 0:
             cout << "No";
             return;
       case 1:
             cout << "Yes";
             return;
       }
       cout << "Maybe yes and maybe not";
}

In previous function we see that we have two explicit return commands, and default return at the end of the function body. 
Share on Google Plus