- [Instructor] A function may return a value
back to its caller with a return statement.
Here is func.cpp from chapter five of the exercise files.
In this example, the return type
of the function is set to void.
And this simply means
that the function does not return any value.
We can change that to int
and I can give it an int parameter as well.
And then I can come down here
and I can say return, i times two.
And now I'm returning an integer value.
I'm taking that integer parameter
multiplying it by two and returning the result.
And so I can come down here into main
and you notice that it says
no matching function call for func
And that's because if you'll remember
a function is identified by its entire signature
which includes both the return type and the parameters.
And here, my function definition has a parameter
but my function call does not pass one.
And so if I put an integer in there, just say 42.
Now that error goes away and I'm calling my function.
So I'm going to use the return value
to initialize another variable.
I'm going to say int x equals func.
And so now that return value, which is 42 times two.
So it will be 84.
That return value is being assigned to a new integer
as its initialization that is recalled x.
And we can print that out with a print f
and say, x is %d.
And then when I build and run,
it'll say x is 84 as expected
just like passing values to a function
return values are returned on the stack.
This means that you cannot return anything large.
And if you need to return a larger object
you can do so with a reference
let's take a look at how to do that.
I'm going to come up here.
I'm going to include the string library
and this is the STL string library.
And so now my function is going to return a standard string
and I'm going to use the reference operator there
to make it a reference to a string.
So I don't have to pass that whole string
on the stack back to the caller.
I'm just passing a reference
and I'll take a string as a parameter,
or rather in this case
I think I'll just go ahead and define one inside here.
I'll say const static.
And again, I'm using static so that it's not stored
on the stack, string as equals.
This is from func
and I'm just going to return s.
And now down here int main, of course
none of this actually applies anymore.
So I can say const standard string s
equals funk like that.
And I can print it out if I want
I can say %s and s.c string.
And now when I build and run
we get the result s is, this is from func
which is exactly what we expect.
So just to reiterate, I'm using static storage
for my string, so that my string is not stored on the stack.
If I don't say static here
my string gets stored on the stack and we don't want that.
We don't want to put big things on the stack.
That's the whole point here
and I'm returning a reference to it.
So I'm not passing the result on the stack.
Also, you cannot return a reference
to an automatic variable.
If I take out this static and try to build
you notice I get a warning
that I'm returning a reference to stack.
And if I actually try to run that code
it may or may not crash because the stack is temporary.
And so I always use static for anything
that I'm going to return as a reference.
I also always use const so that I don't have
unintended side effects.
It's generally good practice to always use const
unless you absolutely need to change a variable.
So returning a value from a function
is much like passing values to a function
and keep in mind that those return values
are also passed on the stack.