Functions

This question might sound like that from a newbie, but I really what to get it right without missing things up.

The return statement is use to exit a function base on the "type" of the function. But what does it mean to say "a function returns a value" or "a function returns more than one value"?

Hoping to get responses.

Thanks
A function returns a value as its output. That value is received by whatever code called the function.

Here's a simple example:

1
2
3
4
5
6
7
8
9
10
int myfunc() // my func returns an int
{
  return 5;
}

int main()
{
  int myvar = myfunc();  // assign myvar to a myfunc() function call
  cout << myvar;  // prints 5
}


here, myfunc returns a value of 5.

On the "myvar =" line, we are setting myvar to equal whatever value myfunc returned. In this case, since myfunc returned 5, that 5 is put in myvar.

That's all it means.

Also, functions cannot return more than one value.
closed account (zb0S216C)
Functions are a group of statements that are executed when that function is called. When the function call is encountered by main( ), main( ) stops in its place and executes the function that was called. For example:

1
2
3
4
5
6
7
8
9
10
11
12
void Function( void )
{
    // Once main( ) stops, the following statements are executed.
    std::cout << "Inside Function( )" << std::endl;
}

int main( )
{
    Function( ); // main( ) will stop here.
    
    // When Function( ) returns, main( ) will resume from here.
}

Once main( ) reaches the call to Function( ), main( ) will stop its execution until it either executes all the statements within the body of the called function (usually the case when the return type-specifier is void), or, when a return statement is encountered of the called function. Once the function is finished, main( ) will resume from the point where is stopped.

Functions consist of 4 parts:

Return Type-Specifier:
This indicates the type of data that the function will return after its execution. A return type-specifier of void indicates that the function doesn't return anything. If a function has a return type-specifier other than void, it must return a piece of data. However, a function cannot return more than one piece of data. This includes arrays.

Identifier:
This gives the function a name. The identifier lets the compiler/linker know which function you wish to call.

Parameter List:
This is a group of objects called Parameters. Each parameter is declared the same way as a normal variable. For example:
1
2
3
4
void Function( int ParameterOne )
{
    //...
}

Here, ParameterOne is a parameter. The data given to it during the function call, is called an Argument. Parameters can be pointers, references, references to pointers, etc.

Body:
The body is where the statements reside. It is also where the return statement lives. The body is the place where main( ) jumps to when you call the function within main( ).

Functions can be declared as prototypes. Prototype functions are functions without the body. However, you must define the function body later on, preferably after main( ). For example:
1
2
3
4
5
6
7
8
9
10
11
12
void Function( int ); // This is a function prototype.

int main( )
{
    Function( 10 );
    return 0;
}

void Function( int Param )
{
    // ...
}


I hope this helps you :)

Wazzak
Last edited on
@Disch-- You said
Also, functions cannot return more than one value.
ico


I actually got from the tutorial found on this website i.e www.cplusplus.com , in the topic "Function II" that "...Passing by reference is also an effective way to allow a function to return more than one value."

I understood all you've said about "function" generally, but to say a "function" returns more than one value seems to me like performing more than one function.

What is your take?
Passing by reference allows you to return multiple values from a function. Allow me to demostrate:

1
2
3
int myfunc(int number){
    return number*2;
}


Here we have a function that returns an integer (number) multiplied by two. Should be simple, right?

OK, time to make our way to the next level. Imagine you want to create a function that modifies already existing variable, like this:

1
2
int number = 4;
MultiplyByTwo(number); // modifies "number" by multiplying it by two. 


And here's function's body:

1
2
3
void MultiplyByTwo(int number){
    number *= 2;
}


However, if you try this out, it doesn´t work. Why? Because you pass "number" by value, not by reference. Reference is like a "link", it doesn´t let go of it´s reference target (which is "number" in our case). So, how do we solve this problem? How we pass by reference? Well, here we go:

1
2
3
void MultiplyByTwo(int& number){
    number *= 2;
}


You should notice character '&' now. This is our operator that allows us to pass variable "number" by reference. Now "number" will be modified, because when we pass "number" by reference, it tells the compiler to transfer "number" into MultiplyByTwo -function. Reference is like a "link", keep that in mind.

Now you still may wonder why there is no return -statement. Well, won't need it. "number" is modified and our job is done. "number" has now value 8, no need to return anything. You can still return a value from this function, though (whatever it could be). Then our function would return 2 values, by reference and by return value. There can be only one return value, but there can be multiple references. These references allow us to return more than one value from a function.

Did you understand me, crosslink? I also tried to learn references & pointers by myself & found them pretty complicated. They definitely aren´t easy to understand, but once you understand them, you find them very useful.
Last edited on
Passing by reference allows you to return multiple values from a function.


Personally, I dislike this expression. It's misleading.

Passing by reference allows you to have multiple outputs in a function. However only one of those outputs can be returned.

returning is a specific thing, and even has a language keyword associated with it. So, IMO, using it to describe any function output leads to confusion.

</opinion>
Hmmm...

I think HenriK is correct with his more general use of the word return. It's a use you just can't escape!

To quote a bit of the MSDN entry for GetUserName

1
2
3
4
BOOL WINAPI GetUserName(
  __out    LPTSTR lpBuffer,
  __inout  LPDWORD lpnSize
);


If the current thread is impersonating another client, the GetUserName function returns the user name of the client that the thread is impersonating.


But a function only has a single return value. The same MSDN entry states that

If the function succeeds, the return value is a nonzero value


rather than "the function returns a nonzero value", to make things clear.

Andy

P.S. And I could well imagine a poorly written explanation of strtok() saying it returns more than value; that you repeatedly call it until it returns NULL. :-(
Last edited on
Thanks for correcting my post, Disch. I hope you still understood what I was looking after & I hope crosslink will understand my explanation, too.
Thanks for all your responses. @Henrik, I better understand the concept, and @Disch, most esp for this comment
Passing by reference allows you to have multiple outputs in a function.


Although, this might not look like a best programming practice but it will aid in clarifying somethings. If I assign a variable to this expression and a type to the function:

1
2
3
4
5
int MultiplyByTwo(int number){
int result = number * 2;

return result;
}


Will this not work? Eliminating the use of "reference"?

belated reply...

crosslink - your return value solution is better than the reference version above.

If you're feeling lazy, you can omit the temporary "result", of course.
1
2
3
int MultiplyByTwo(int number){
    return number * 2;
}


(Note that the compiler will optimize the temporary out anyway, so it's not a problem if you prefer to use one, from an efficiency point of view. It just save a few key strokes!)
Last edited on
I agree, andywestken.

I'd recommend avoiding temporary objects/variables as well as possible & even though you use them, please consider how to scope them (give them as small scope as possible to deal around). If you haven't dealt with classes yet, please do so soon, because object-oriented programming really makes programming more pleasant.
For me the rule is to keep just enough local variables that the debugger can show you -- in the watch window -- what's going on in given scope!

With MulitplyByTwo() there's no real need for the temporary "result", as most people know their 2 times table!
Last edited on
You guys have been wonderful. Yeah to save time and for compatibility, "result" wouldn't be necessary although it makes the code easier to understand.

Thank you all once again, hope we do it again some other time.
Topic archived. No new replies allowed.