C++ Function and the return Statement
Some Features of C++ Entities – Part 2
Forward: In this part of the series, I talk about the structure of a C++ function and the effects of the return statement in a function.
By: Chrysanthus Date Published: 26 Aug 2012
Introduction
The Function Structure
A function is of the form:
returnType functionName (type1 ident1, type2 ident2, …)
{
//statements
return [value]; // optional
}
In the structure,
returnType functionName (type1 ident1, type2 ident2, …)
is called the interface and
(type1 ident1, type2 ident2, …)
is called the function signature.
The return statement is optional, and is usually the last statement, in many situations. If the return statement (line) is omitted, then the returnType of the function interface has to be, void.
The return Statement
The return statement itself can simply be:
return;
In this case nothing is returned. In such a case the returnType should be void.
If the returnType is not void, then the return statement should be:
return value;
Here, value, can be the actual datum, e.g, a number such as, 56. It can also be a pointer (or an address).
The return statement means, stop executing the function just after the return statement and return to the caller. If the function is not the main function, then the caller is the calling function. The following program illustrates this:
#include <iostream>
using namespace std;
int fn()
{
cout << 1 << "\n";
cout << 2 << "\n";
return 56;
cout << 3 << "\n";
cout << 4 << "\n";
}
int main()
{
int var = fn();
cout << var;
return 0;
}
Read and try the code. Execution of the function definition, fn(), stops at the return of 56. The statements that follow in the function definition are not executed. The calling function is, “fn();” in the main function definition. The called function is the definition of fn().
The return Statement and the if-construct in a Function
You can decide whether or not the statements below the return statement in a function, should be executed, using the if-construct. In the next two programs, the return statement is inside the block of an if-construct in the function.
In the following program, the statements below the if-construct are executed because the if-condition is false (read and try it).
#include <iostream>
using namespace std;
float flt = 2.5;
int fn()
{
cout << 1 << "\n";
cout << 2 << "\n";
if (flt == 8.7)
{
return 56;
}
cout << 3 << "\n";
cout << 4 << "\n";
}
int main()
{
fn();
return 0;
}
In the above function “fn()”, there is no effective return statement.
#include <iostream>
using namespace std;
float flt = 2.5;
int fn()
{
cout << 1 << "\n";
cout << 2 << "\n";
if (flt == 2.5)
{
return 56;
}
cout << 3 << "\n";
cout << 4 << "\n";
}
int main()
{
int var = fn();
cout << var;
return 0;
}
Returning from outside the Function
A function does not only have to return what has been created inside the function definition. It can also return what has been created outside the function. The following program illustrates this, with the fn() function:
#include <iostream>
using namespace std;
float flt = 2.5;
float fn()
{
//some statements
return flt;
}
int main()
{
float var = fn();
cout << var;
return 0;
}
You return an instantiated object in the same way that you return a simple object of an int. The following program illustrates this:
#include <iostream>
using namespace std;
class Calculator
{
public:
int num1;
int num2;
Calculator(int ident1, int ident2)
{
num1 = ident1;
num2 = ident2;
}
int add ()
{
int sum = num1 + num2;
return sum;
}
};
Calculator fn()
{
Calculator myObject(2,3);
return myObject;
}
int main()
{
Calculator theObject = fn();
int result = theObject.add();
cout << result;
return 0;
}
The function, fn() returns the instantiated object, myObject, which is assigned to the new object, theObject in main.
Return in main
Everything being equal, the last statement in the main function should be:
return 0;
Here, the integer, 0, is returned to the caller, which is the operating system. I will say more about this in a different series.
That is it for this part of the series. We stop here and continue in the next part.
Chrys
Related Courses
C++ CourseRelational Database and Sybase
Windows User Interface
Computer Programmer – A Jack of all Trade – Poem
NEXT