Log in from a computer to take this course

You'll need to log in from a computer to start Learn C. But you can practice or keep up your coding streak with the Codecademy Go app. Download the app to get started.

apple storegoogle store
Learn

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 type int, is set to 7
  • 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

1.

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.
2.

Now at the end of the getRandom1000() body:

  • Return the variable random1000
3.

Lastly, in the main() function:

  • Replace the 0 with a call getRandom1000() and store the return value in the variable randomNumber.

Take this course for free

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?