Hello, and welcome.
In this lecture, I want to talk about returning data from functions.
Up till this point, we've we know what a function is we know why it's useful,
we know how to define functions we know how to pass data to functions.
Now we want to talk about how we can return data from functions,
which is also very useful.
In our prior example, when we multiply two numbers, a multiply
function displayed the results of the calculation at the terminal.
Remember, we had that printf statement in there.
Also, we had an example where we were just a simple function earlier.
We just printed out data inside the function.
We wanted to be nice instead of bring it out there because it's
actually doing more than one thing.
It actually printed it out, the data was returned and it printed
out the data in the main function.
You might not always want to have the results your calculations displayed.
The function becomes more flexible, more generic which
in turn allows it to be used more if you can return data.
So functions can return data using specific syntax in c.
We should be familiar with this a little bit because remember
our main function at the very end we're returning data quite
a bit in a lot of the examples.
So we've been exposed to this a bit.
We just haven't really understood what it means.
Let's take another look at the general form of a function.
Remember, this function header.
We had a return type which was the word that was first defined.
And then we had the name, and then we had the parameters separated
by commas inside parentheses.
We already know the name and the parameters.
That first thing is the return type.
The return type specifies the data type of the value
returned by the function.
Functions can return any kind of data.
You just have to let the compiler know what kind of data you're returning if.
It's an int, a float, a double whatever it is, you have to specify
that in your function header.
The return type.
The return type we saw right here, and you can specify that type of
value to be returned by a function as long as it's a legal type in c.
I mentioned the main ones, double, float and so forth.
But you can also return enumerations and pointers.
We haven't discussed pointers, but it makes it very, very flexible
when you can return pointers.
The return type can also be avoid type.
You often see this keyword void.
We now understand what it means.
Void means that no data is going to be returned.
If you see void before the name of the function, you're
not returning any data.
If you see void inside the parameters list, you're not passing any data.
Void is just a general keyword that for return types and for parameters,
you also notice you can use void as a pointer as well, but we'll
get to that when we get to that.
But void is a way of saying nothing.
And the reason you include void as opposed to just not
including anything because it makes the program more readable.
Actually when you pass data to a function and you have no no data
that's being passed, you can have it just be empty parentheses.
It's easy to read if you have void in there.
Same with a return type except the return type you have to have void.
You can't have it just be empty.
Void means no data is going to be returned.
The return statement.
We've talked about how you can identify what data is going
to be returned but how do you actually return the data.
Inside the body of the function, you have to have a statement where you
can say what you're going to return.
Because remember the body of a function can have a lot of data.
It can have local variables, it can have anything you know the function
could be doing a specific calculation, maybe doing multiple calculations,
what do you want to return.
Luckily there's this statement with the keyword return.
If you provide the return with no variable name after or
expression, that means you're just exiting the function.
It's similar to a break in a loop.
You're just returning from the function.
And you can provide this in functions that are defined as void.
It makes it clear when you're exiting the function.
You should really only have the return in one spot at
the bottom of the function.
We'll get to that later, but you can't have it anywhere in a function.
If you see it anywhere in a function, it means you're
jumping out of that function.
The reason why it's good to have it at the bottom is it's very clear
when you're exiting the function.
If you have multiple returns even if you're returning data all
throughout your function, it's hard to say when you're returning.
So what I like to do in my functions is create variables and
only return once at the bottom.
If I need to return something in a function or I mean in a specific
if statement in a function or something I'll set the variable
and then return it at the bottom.
So if you have return and you have nothing after it, that
just means you're exiting the function with no data returned.
This is really not used as much.
You only use it in the void function, doesn't return a value.
The more general form of the return statement is returning
some variable or some expression.
X plus y or returning x.
This form of the return statement has to be used with the return type that's
specified in the function header.
If you're returning an int, whatever that variable or expression
you're returning has to be an int.
It has to evaluate to an int, if you're returning an int.
Now if you're turning a double, it has to value to double.
Remember, you can have many different types of data returned.
The value that is returned to the calling program is the
value that results when the expression is evaluated so.
If you have x plus y, it's returning the result of that expression and
it has to be the same type as the function declaration as I mentioned.
A function that has statements in the function body but does
not return a value must have the return type as void, have to.
You cannot not have that.
If you do not have that and you're not returning any data, you're
going to get an error message.
When you compile the program and it contains a function with
a void return type that tries to return a value compiler error.
Void means no return data, don't return a variable an expression.
If a function does not have a void return type and it has a valid
return type, you're going to get an error message if the compiler tries
to return a different type than a specified or if they just have
returned by with nothing after it.
So pay attention to this.
Those keywords in the beginning of the functional declaration are important.
It directly affects the implementation of the function and the compile
is going to tell you if you screw up and you don't return the right
data or if you don't return any data or if you do return data when
you're not supposed to return data.
If the expression results in a value that's different a different type
than the return type in the function header, c will try to convert it.
The compiler will try to convert it if it can.
So for example if you're returning a float and your expression is I
plus x with two ints, it's going to just convert it to a float.
If you're returning an int and you have an expression that's
doing float calculation, it will convert it to an it.
You're going to lose data in that case, but it will do conversion.
It's not always possible to do the conversion.
In cases where it's not possible, you're going to have an error
that's produced by the compiler.
If you're trying to return an int when you're supposed to return
a string or when you're trying to return it into when you're
supposed to return an enum, it's going to complain the compiler.
You can also have more than one return statement a function,
I mentioned this briefly and why you don't want to do it.
But you can do it.
You can have multiple returns throughout the function.
It just makes the code harder to follow because you're jumping out
of the function all over the place.
When you do return and you do have multiple return values, just make
sure again if you have a function that's returning something, you
have an expression or a value of that type or it can be converted.
Now that we've talked about passing data and returning data,
we know how to invoke a function, but I want to say a couple of
other things about invocation.
You call a function by just using the function name followed by the
arguments that you pass that are in between the parentheses,
we know that, that far we know but I want to formally state it, that's
how you can invoke a function.
When you call the function, the values of the arguments that you
specify in between the parentheses are going to be assigned to
parameters inside the function.
The parameters are representative variables with a data type.
You can then use that variable any side in the function body.
When the function exits or executes certain code, it will use those values
supplied as arguments, as I mentioned.
The arguments you specify when you call a function should agree in
type number and sequence when the parameters in the function header.
Order also matters, I didn't emphasize that, but if it says
int x double y, you first have to pass in the int then the double.
You can't pass in the double and int.
The order is very, very specific.
So make sure you pass the data as an argument when you invoke the
function the same way as the function is declared in the function header
in the parameter list, passing the right types is that as well.
Invoking a function and assigning the data returned.
If a function is returning data, the function can assign that data just by
using the equals operator and after the return as well or you can store
it in a local variable if you want.
It will also work with an expression.
So you can say return x plus y, you can say return x, you
can say return a constant.
This example here is after you invoke the call when you want to
assign the data that's returned.
So you can say int x equals my function call, whatever that function
returns is going to be assigned to x.
And if my function call returns x plus y, the result of x plus
y is going to be assigned to x.
So you can assign the data that's returned from a function call
by just having a variable on the left hand side of the equals and
the function invocation on the right hand side of the equals.
The calling function doesn't have to recognize or process the value
returned from a called function.
You don't need to have that assignment there.
If a function is returning data and you don't care about that data, you
don't have to have the equals at all, you can just invoke the function.
It wouldn't really make sense because if it's returning data it means
that you probably need to look at it, but you don't have to do it.
The compiler is not going to complain if you just invoke a
function and don't handle the data.
It's up to you how you want to use the data that's
returned from function calls.
Here's an example of invoking a function and also returning
data from a function.
In this example, multiply two numbers.
This was just like the example we had previously, but the previous
example didn't return any data.
The previous example displayed output.
In this example, we're not displaying any output.
We're returning the data and then we're displaying the
output in the main function.
Here our multiply two numbers takes two integers as parameters.
We multiply those two integers.
And then instead of printing this amount or the value, we're using
the return keyword and we're then specifying a variable named result and
then we have the semicolon to end it.
When you see that return keyword inside the function body,
you're returning that data.
And remember, you can have that multiple places.
But you only can have it return one value, you can't return multiple values.
So if you have the return keyword in multiple places, it has to be inside an if statement
or loop because you're jumping out right then.
If you have a return followed by another return, it will give you
an error because it would say you have two returns in both cases.
You're always returning to results, you can't do that.
But the return keyword's important.
You're returning a variable.
And then you see in the main function the data that's being
returned is being assigned to another local variable called result.
You'll notice we're using the same names here.
This has to do with function scope which we're going to talk
about in the next lecture, local variables versus global variables.
All you need to pay attention in this lecture is the fact
that this function multiplied two numbers is returning data.
How you return it, you return it using the return keyword followed
by the variable of the expression.
You also notice that the multiply two numbers specifies the return type
at the top of the function header.
It's returning an int.
So we have to return an int.
You also notice that when we assign the data in the main function,
we're assigning it to in int.
Assigning those very easy we just have the equals operator.
If we didn't care about the data, we could just call multiply two numbers
and then do nothing with it but.
After we get the data returned from the function, we then print
it out using a printf function.
This makes the function more cohesive that it takes data.
We're not actually displaying the results inside the function.
There are times when we don't want to display the results, we just want
to multiply two numbers and then we may want to divide the product.
Flexibility with data being passed and data being returned is a big deal.
This is a simple example.
You're going to get exposed to this more in our next challenge.