Category: Functions
Returns a value from a function.
When you define a function you give a name to a set of actions you want the computer to perform. When you call a function you are telling the computer to run (or execute) that set of actions. When that function is finished running, execution of the program returns to the point in the code where the function was called. When you use return you are able to specify a single value that will be "returned" to whatever code called the function in the first place. This is how your functions can generate output that other parts of your program can use.
// Call functions to generate two die rolls and sum the result. Display the value on the console. console.log(rollDie() + rollDie()); function rollDie() { // Define a function that uses randomNumber(1,6) to randomly generate a die roll, 1 to 6, and return the value. var roll = randomNumber(1,6); return roll; }
Example: Area of Circle Calculate and return the area of a circle of a specified radius.
// Calculate and return the area of a circle of a specified radius. var area = computeCircleArea(10); console.log(area); function computeCircleArea(radius) { return Math.PI * Math.pow(radius, 2); }
return ___;
Name | Type | Required? | Description |
---|---|---|---|
___ | any | Yes | The return value can be a number, boolean or a string, or a variable containing a number, boolean or string, or the number, boolean or string returned by a function, or the numeric, boolean or string result of the evaluation of an expression. |
The return value can be a number, boolean or a string.
Found a bug in the documentation? Let us know at documentation@code.org
Found a bug in the documentation? Let us know at documentation@code.org