Lesson goal: Functions that return values

Previous: Introduction | Home | Next: Convert between Celsius and Fahrenheit

The last lesson showed you how functions can be used to perform the same job over and over again. Sometimes it is useful for a function to return some result back to the main programmed that called it. You can do this by putting a return statement as the last line in your function.

function name(input 1,input 2,...)
BODY
return(some-value)
end
Move the mouse over a dotted box for more information.

Take a look at the default code below (hit [Reload] on your browser to reset the page if needed). It defines a function called add3 that takes in three numbers and adds them together. It then returns their sum back to the main calling line in the program.

Now you try. Run and tweak the this code.

Type your code here:


See your results here:

This program won't run. You have to fill in something in the ( ) of the return( ) line. Do you know what it will be? Dismiss.
Show a friend, family member, or teacher what you've done!

Here is a share link to your code:

Does your code work? Want to run it on your iPhone?

Here's your code:

  1. Use [Control]-[C] (Windows) or [⌘]-[C] (MacOS) to copy your code.

  2. Paste it using [Control]-[V] (Windows) or [⌘]-[V] (MacOS) into this page

  3. Then click the "Use on iPhone" button that you'll see.