We now know how to define a function, so let’s look closer at function return values. Calling a function to do some work and return a value is a common use of functions. It is important to understand how to define what type of value is returned and to ensure that it is returned.
Many of the C types we’ve seen so far can be used as a function return type like int
, double
, char
, and even pointers.
int getSecretNumber() { int secretNumber = 7; return secretNumber; }
In the example above:
- The function has a return type
int
- The variable
secretNumber
, of typeint
, is set to7
- The variable is returned using
return secretNumber
The type of the value returned inside the function must match the type defined in the function signature. If there is a mismatch you will get a compilation error telling you of the mismatch.
Once the return statement is executed, the function gives control back to the function that called it and no further code within the function will be executed. For example:
int getSecretNumber(){ int secretNumber = 7; return secretNumber; printf("Don’t tell anyone\n"); }
The printf()
statement will not execute and "Don’t tell anyone"
will not be displayed in the output terminal.
Instructions
Continuing with random number generation, modify a function definition so it can return a random number from 1 - 1000:
The function getRandom1000()
currently has a return type of void
.
- Change the return type so the function can return an integer.
Now at the end of the getRandom1000()
body:
- Return the variable
random1000
Lastly, in the main()
function:
- Replace the
0
with a callgetRandom1000()
and store the return value in the variablerandomNumber
.