Breaking News

Return Values of Functions In C++

So far we have only seen functions with void as the return data type. In this section you will learn about what is the use of return data type and how it can be used.
 
Return values are used in the function syntax and so far we have mentioned void as the return value of a function. Void means: no value is returned by the function.
 
Return value, as the name suggests, is used to return some value. When a function completes its execution it can return a single value to the calling program.
 
The syntax is:
 
Return variable;
 
When a function returns a value, the data type of the value must be specified in the function.
 
Consider the following program that uses return value concept:
 
 
Out put of the program
 
 
In the above program the only statement of interest is
 
sum = add(int num1, int num2);
 
As you have already learnt, the right side of the equal sign is actually the function call. This is being assigned to a variable called sum. Hence, it can clearly be seen that the function add has to be executed and some value should be returned. This value is assigned to sum.
 
As you can see, in the declarator instead of void we have mentioned int as return data type. This means that the function add will return (or give back) an integer quantity to the calling program (in this case the caller is main function).
 
The calling program is the one that calls a function. In this case the main function calls the function add. When the compiler executes the add function, it knows that the add function has to return a value to the main function (because the return data type of add is int).
 
Therefore, the sum of num1 and num2 will be returned to main.
 
This will be assigned to sum. Earlier when the return data type was void, nothing was returned by the function.
 
You have to mention the return data type in the function declaration also.
 
void main ( ) and int main ( )
 
Remember We said that void main ( ) is the same as writing:
 
int main ( )
{
……….// program code
return0;
}
 
we’ll now show you a little benefit of using int main( ).
 
This isn’t an actual benefit but We just want to show you the difference. For exact reasons as to why we use int main ( ) check out the next section.
 
Consider the following program:
 
 
Out put of the program
 
 
What do you think happens when the user types 1 as the value of test?
 
Simple since test is equal to 1, the compiler will go into the body of if. It will display You typed 1 on the screen. Then the compiler will return a value of 1. Generally a return value of a non-zero number is used when errors are encountered.
 
You can’t use this return value anywhere else. In other functions (functions other than main), you can make use of the return value. A return value of 0 indicates a normally terminated program. After returning the value, the program exits. This is the point to be noted.
 
Which means that the compiler will not even read the remaining few lines and hence nothing else will display on the screen. The compiler will quit once an integer has been returned to main.
 
On the other hand if you type anything other than 1 for test, the compiler will skip the if body, display the last statement and then return 0. The program gets terminated. Learn more in the next section.
 
Better to use int main()
 
We thought that since we have taken a discussion of void main and int main, we could now go one step further into the topic.
 
Which one is better? Or which one should we use and why?
 
Void is generally used to declare functions that do not return values.
 
You might have had this question about where does main function return its value to? Generally, a return value has to be returned to the calling process.
 
When you use int main ( ), the main function returns an integer to the operating system (since the OS is what calls the program and in turn the main function).
 
So OS is the one that calls your main ( ) function. Returning a value from main ( ) is like an exit function. The value is returned to the operating system and the program ends.
 
What will the OS do with your returned value?
 
Actually, the OS never wants to know what you return. The program which started your program might need to know. In any OS, a program when running is called a “process”. When booting up, the operating system starts the first process .
 
Thereafter the first process starts other processes such as a shell (shell is just a program which reads commands from the user and converts it to system calls).
 
So when you write a program and execute it in the shell, the shell starts your program.
 
So now, the shell is the parent process of your program (and your program is the child of the shell); Now, in the same way, suppose you want one of your programs to load another program to do a particular job and you want to know just whether the job was successful or not,
 
Then the OS get the exit code of the child process and gives it to the parent process; just like returning from a function.
 
So it is a standard to give the exit code as ‘0’ for a success and any non-zero integer for a error. When programming in C/C++ you can give this exit code when you return from the main function.
 
So you’ve to declare main as ‘int main()’ to do that. If you declare it as ‘void main( )’ C++ wont allow you to set a value to be returned to your parent program.
 
So the variable which should contain the return code will be filled by no one., which means that memory can be in any state (unpredictable).
 
Hence you have no control on what value your parent process gets when your child program exits. In MSDOS the shell is called ‘Command.com’. In Windows the shell is’explorer.exe’.
 
Hence it’s always better to use int main ( ) along with a return value at the end of the main ( ) function.A return value of zero indicates a normally terminated program.
 
A non-zero value is used in case of errors. Of course this does not mean that a program written with void main ( ) won’t work; but it is better to avoid writing such programs:
 
 
Out put of the program
 
 
All the other programs that We have written so far have used void main ( ). They will all work if you useint main ( ) with a return value as done in the above example.
 
We advise you to go on with int main ( ) rather than void main ( ) whenever you write C++ programs.

Check Also

Insertion & Extraction In C++

Insertion is the operation of sending characters to a stream, expressed by the overloaded insertion …