can anyone explain to me what return does? I know that return(0) stops the program at any given point, but what does returning others do?

can anyone explain to me what return does? I know that return(0) stops the program at any given point, but what does returning others do?

Well, return is used to give the return value of a function. When you use return 0, that means that the program was successful. If you don't add a return statement, the compiler will automatically insert a return 0; in the program.

OK, I'll try to explain this as simply and accurately as I can.

First though, a note on functions:
All functions must return something...Anything, even if that something is actually nothing (if a function returns nothing then you use the 'void' return type).

Return doesn't end the program unless the call to return is made in the main function.

A more accurate description of what the return keyword does is this:
The return keyword exits the 'current function' (the function we're returning from), returning back up the call-stack to the 'calling function' (i.e. the code that called the 'current function' we're returning from).

I'll try to explain this in more detail, but first take a look at this very quick code snippet as this will form the basis of my explanation:

#include<iostream>

// here's the declaration of a function which returns a boolean (true or false) value
bool somefunction();

// Here's the main function, the entry point of the program
// returns an int value
int main()
{
    if(somefunction())
        return (0); // end the program normally
    else 
        return(1); // end the program but flag an error condition
}

// Here's the definition of someFunction
bool someFunction()
{
    // we'll just return true here
    return true;
}

Now imagine this:
From your OS, you compile and run the above program.

The main() function is the entry point of the program, so when you run your program, your OS loads the program into memory and execution begins in main.

The block of code beginning with the if statement in the first line of main says this:
"IF we call somefunction() and its return value is true,
THEN we end the program by returning the value 0.
ELSE we end returning the value 1".

So someFunction is called from the if statement in main. In other words, the code execution jumps from the if statement in main into the body of someFunction.

Now we are inside someFunction, so in terms of my definition of the return keyword the 'current function' is someFunction and the 'calling function' is main().

SomeFunction returns 'true'.
Now according to my definition of the return keyword, the code execution will return from the 'current function' (somefunction) to the 'calling function' (main) passing the value 'true'.

So the return keyword in somefunction ultimately causes the code execution to jump back to the if statement in main. (as this is where somefunction was called from).

So now we're back inside the if statement inside main, after the call to someFunction. Because the call to someFunction returned true, the condition of the if statement is met. So the program ends by returning the value 0 to the OS/shell.

In terms of my definition of return, from the return in main, the 'current function' is main and the 'calling function' is the OS.
So in other words, the execution of the program jumps out of the main function of the program, thus ending the program and handing control back to the OS, passing the OS the value 0.

NOTE: In most, if not all OSes; if a program's main function returns an int, 0 indicates that program execution ended normally.
Any other value indicates that the program execution ended abnormally. In other words an error occurred.

Likewise if the main function of a program returns bool, then conventionally, false indicates that the program ended normally, whereas true indicates that the program ended because of an error condition.

And I think that's about it. I've gone fairly in depth on the topic. Some of the terminology I've used might not be 100% accurate. But I 've explained the basic principle as best I can!

To sum up:
The return keyword tells your program to return from the 'current function' (or the function we're returning from) back up the callstack to the 'calling function' (the code that called the 'current function').

Also, any value specified after the return keyword is returned to the calling function. But only if it's type is the same as the functions return type. So if your function is supposed to return an int, then the value returned by the return keyword must be an int, otherwise your compiler will throw an error message when you compile!

Hope that made sense and wasn't too complex!
Cheers for now,
Jas.

I kind of understand, so for this example, what does return T do if its not in a main function?

int Add(Time Total[],int MaxSize)
{
	double Hrs, Mins;
	int T=0,SumMins=0;
	double SumHrs=0;
	
	cout << "Enter amount of hours:" << endl;
	
	for (T=1;T<MaxSize;T++){
		cout << "Day " << T << " :" << endl;
		cout.width(10); cout << "Hours: ";
		cin >> Hrs;
		cout.width(10); cout << "Minutes: ";
		cin >>Mins;
		cin.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' );
		
	
		Total[T].SetHours(Hrs);
		Total[T].SetMinutes(Mins);

		SumHrs=SumHrs+Total[T].GetHours();
		SumMins=SumMins+Total[T].GetMinutes();
	}
	//Find Minutes remainder
	int TotalMins=SumMins%60;
	//Find Hours From Minutes
	int SumHM=SumMins/60;
	
	cout << endl;
	cout << "Total Hours: " << SumHrs+SumHM << "." << TotalMins << endl;
	cout << endl;
	cout << endl;
	
	return T;
}

What is MaxSize since it has not been declared anywhere? You need to store the amount of hours in a variable which I guess would be where MaxSize comes from. The total number of hours and minutes is returned back to the main program.

I kind of understand, so for this example, what does return T do if its not in a main function?

Well, lets look at that in terms of my definition of what return does:
return exits the 'current function' and returns to the 'calling function' passing an appropriate value.
In this case, the return is in the Add function, so the 'current function' is the Add function, the 'calling function' is whatever bit of code that called the Add() function and the value being returned is T.

So at the return statement in the the Add function, code execution jumps back to whatever bit of code called the Add function and passes the value of T.


Assuming that your function works (I haven't actually read through the rest of the code, I'm just gonna explain the return thing for now):
If you had a void function called process that called the add function like this:

void process()
{
    // not sure how you're using the add function so apologies if this is wrong.
    // the main thing here is the actual function call..
    const int maxSize=200;
    int result;
    Time myTime[200];

    // This is the main thing we're interested in:
    result = Add(myTime, maxSize);

    return;
}

At the line where the Add function is called, the code is basically saying:
"Call the Add function, passing myTime and maxSize and assign the returned value to the variable result"

So execution jumps from the process() function, into your Add function. The two parameters are passed in and your add function is executed. When execution reaches the return statement, execution jumps back into the process() function passing the value of T.
The returned value of T is then stored in the result variable declared in the process function().


Likewise, if you called Add from main:
execution will jump from main, into the Add function. The function will be executed until the return keyword is reached. When execution hits the return keyword, the execution jumps back into main passing the value of T.

Does that clear things up for you?

Cheers for now,
Jas.

>In most, if not all OSes; if a program's main function returns an int, 0
>indicates that program execution ended normally.
>Any other value indicates that the program execution ended
>abnormally. In other words an error occurred.

That's required behavior for a C++ implementation. It has nothing to do with the OS, because the calling code is (rather than the OS itself) a C++ runtime that interprets the return value of main and converts it to something suitably equivalent for the system.

>Likewise if the main function of a program returns boo
The main function doesn't return bool, and suggesting that it does, or that the integer value returned represents a boolean value, is terribly confusing.

>In most, if not all OSes; if a program's main function returns an int, 0
>indicates that program execution ended normally.
>Any other value indicates that the program execution ended
>abnormally. In other words an error occurred.

That's required behavior for a C++ implementation. It has nothing to do with the OS, because the calling code is (rather than the OS itself) a C++ runtime that interprets the return value of main and converts it to something suitably equivalent for the system.

Fair point! I was probably oversimplifying my explanation there. But as ever, you've summed things up far more succinctly and accurately than my own fumbling attempt! Thank you Narue! :)

>Likewise if the main function of a program returns boo
The main function doesn't return bool, and suggesting that it does, or that the integer value returned represents a boolean value, is terribly confusing.

I know that, but surpisingly I've seen a few odd programs (emphasis on the odd) that actually use the bool return type for main...Thankfully I don't think that this unusual practice is particularly widespread or particularly well advised for that matter. Certainly not an option I'd choose, but something I have seen used on my travels. So I thought I'd share just in case!

I've only seen it a handful of times. The one time I came across it professionally, you can be sure that I changed the return type to int! Bool main?? Weird!
I probably should have made a note of that in my post!
Or better still, not posted the bit about bool main at all! heh heh!

Cheers for now,
Jas.

>I've seen a few odd programs (emphasis on the odd)
>that actually use the bool return type for main...

Once again, it's a requirement that main return the int type. bool main() is just as wrong as void main() . Some people might use it, but that doesn't mean you should take it into consideration in your explanations unless you're correcting someone else. ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.