Im still a newb when it comes to programming so im just curious, i understand as far to where return(0); at the end of a function tells the program their were no errors and to end the program right? But what else can you use return(???); for? And how exactly does it effect your code? Im going to do some searching on wiki right now. Thanks.

Recommended Answers

All 4 Replies

The return statement, with a value, simply returns that value to the caller from the current function.

In the case of your main( ) function, this is "called" by the operating system, and main( ) returns a value to the OS upon completion. By convention, return value 0 indicates the program ended normally, if there is no need for the program to return any particular values. In some cases, other return values may be used to indicate various errors that occurred in the program, but I don't know that there's any standard definition of what those would be. You can define them in terms of your program.

More commonly, you'll be concerned with the return values of the other functions you write. That's how they communicate their results back.

i think i understand what your saying. Im going to do some experimenting with that.

So hold on. Say in the end of int w/e i return(var = a-53{that legal?})
then in a different function that would be called after the return was already declared, you might have some line of code in their like var - 6 or w/e, var being what was defined in the other function? Sorry if this doesnt make sense.

If it doesnt, i think i understand now and is just hard to explain lol.

First, think of value-returning functions in the same sense as a mathematical function. For some given input, a process is executed and you get a value back, like F(x) = x^2 - 2. For any value substituted for the x, you get back that value squared, minus 2.

Consider the following:

#include <iostream>
using namespace std;

int addem( int a, int b );

int main()
{
	int num1 = 10;
	int num2 = 23;
	int sum;

	sum = addem( num1, num2 );

        cout << num1 << " + " << num2 << " = " << sum << endl;

	return 0;  //program exits here, 0 goes to operating system
}

int addem( int a, int b )
{
	int total;
	
	total = a + b;

	return total;

        total = a - b;
}

//or
/*
int addem( int a, int b );
{
	return a + b;
}
*/

main( ) returns a 0 to the operating system - that's a standard action.
addem( ) returns the sum of its arguments. In the first version, yous see a variable declared, which then is assigned the sum. This value is returned. At the point where a return statement is executed, control leaves the function, so the statement total = a - b; will never, ever be executed. The second version (commented out) shows how it's not always necessary to use additional variables - the result of the arithmetic can be directly returned.

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.