- Most functions in the C library return a value.
Even when that value isn't used it's available.
Returning a value allows functions to generate output.
Most of the time that output is based on the input
or arguments passed to the function.
In this movie I explain how functions return values.
They can simply generate the information
or they can manipulate arguments to produce specific output.
I'll also cover a few rules and regulations
regarding functions in the C language.
This code contains a function called gimmeAnA.
It's a character function returning a char value.
The function has no input according
to its prototype at line three.
The function is called at line nine where its
return value is assigned to the the grade variable.
The function itself at line 15 merely
returns the single character A.
Build and run this code.
And I hope that that's true.
When a function returns a value
the value can be used immediately.
You can edit the source code for this exercise
so that the grade variable is removed.
First delete the assignment and then replace the
variable in printf with the gimmeAnA function.
Save the changes, build and run.
And you still get an A.
Open exercise file return2.
You may recognize this exercise file from an earlier movie.
In this code the product function has been
upgraded so that it returns the product of the
values passed, which makes more sense.
Build and run this code.
Type three values then press enter to view their product.
The code can be cleaned up a bit.
For example, variable p in the
product function is not really needed.
You can eliminate it to merely have the product returned.
Save these changes, build and run,
and the results will be the same.
Well providing you type in the same numbers.
A more realistic example is shown in exercise return3.
Max is kind of the stalwart of learning C.
It's passed two values and returns the larger of the two.
I've also added the isLarger function,
which doesn't return anything, but I wanted to show
how multiple functions can be declared within the code.
Here is max and there is isLarger, build and run.
Type two values, hopefully two
different values separated by a space.
The code displays the larger of the two values.
And now the limitations, in the C language
a function can return only one value.
You cannot return multiple values, nor can I think
of a reason why you would want to.
You can return an array or a
structure variable from a function.
These variable types are covered in a later movie,
but yet they remain single variables.
And now I'd like the explain the main function.
As you can see here in this source code,
main is an integer function.
The return statement is required and
an integer value must be specified.
The parentheses for the main function are empty.
This is a shortcut, a cheat.
The compiler knows the arguments for the main function.
Two of them exist, but you don't need to
specify them if they don't get used.
So the parentheses remain empty in the main function.
This is not something, however, you can get away
with other functions you declare and use.