If you're seeing this message, it means we're having trouble loading external resources on our website.

If you're behind a web filter, please make sure that the domains *.kastatic.org and *.kasandbox.org are unblocked.

## AP®︎/College Computer Science Principles

### Course: AP®︎/College Computer Science Principles>Unit 3

Lesson 7: Procedures

# Procedures with return values

AP.CSP:
AAP‑3 (EU)
,
AAP‑3.A.7 (EK)
,
AAP‑3.A.8 (EK)
,
AAP‑3.C (LO)
,
AAP‑3.C.2 (EK)
Procedures can compute values for the rest of the program to use by sending back return values.

## The need for return values

Our example procedures so far have all had side effects. That means they've affected their environment in some way, like the way that `println()` displays output on the screen.
``````var score  = (numCorrect/30) * 100;
println(score);``````
Many times, we don't want to immediately display a value to the screen. Instead, we want to remember the result of a calculation and use it in the future somehow. In that case, we tell the procedure to return a value to the code that called it.
In the JavaScript language, we can return values from a function by typing `return` followed by an expression to return.
``````function calcScore(numCorrect) {
var score  = (numCorrect/30) * 100;
return score;
}``````
Now that the function returns a value, we can use store its result in a variable, like this:
``````var sallyScore = calcScore(27);
var wilburScore = calcScore(24);``````
We can also directly call the function inside other functions:
``````println(calcScore(27));
println(calcScore(24));``````
Or we can call the function inside a larger expression:
``````println("Sally earned: " + calcScore(27));
println("Wilbur earned: " + calcScore(24));``````
📝 See similar code in: App Lab | Snap | Python
Our call to `calcScore()` always returns a value, so we can use it anywhere that expects a value. That's a lot of places!
In fact, we've already been using a number of built-in procedures that return values, like for math and string operations:
``````var maxNum = Math.max(33, 100);
var firstName = "Harry Potter".substr(0, 5);
var yell = "im hungry".toUpperCase();```````
Now, we know how to define our own procedures with return values, and we can build up a library of useful procedures that compute values for our program's needs.
✏️ The program below calculates how many "me" would need to be stacked to reach certain heights, like the moon or the statue of liberty. Change it to calculate the number of "you" instead and add another structure, like Mt. Everest or the sun.
📝 See similar code in: App Lab | Snap | Python

## Watch out for early returns

The following bit of code has a bug:
``````function calcLineSlope(x1, y1, x2, y2) {
var yDiff = y2 - y1;
var xDiff = x2 - x1;
return slope;
var slope = yDiff / xDiff;
}``````
Do you see what it is? Here's a hint: whenever the computer executes a `return` statement, it exits the function immediately. Once it's exited the function, it won't execute any more code in the function.
That means that the computer only executes this code:
``````function calcLineSlope(x1, y1, x2, y2) {
var yDiff = y2 - y1;
var xDiff = x2 - x1;
return slope;
}``````
It never gets to the line that calculates the slope, so it returns an undefined value to whoever calls it. The line of code after it is "dead code", code that will never ever be executed.
In the correct version of the code, the return statement is the very last line in the function:
``````function calcLineSlope(x1, y1, x2, y2) {
var yDiff = y2 - y1;
var xDiff = x2 - x1;
var slope = yDiff / xDiff;
return slope;
}``````
📝 See similar code in: App Lab | Snap | Python

## Return values in pseudocode

This pseudocode represents a procedure that takes two parameters named `parameter1` and `parameter2`, contains programming instructions, and then returns the value of `expression`.
``````PROCEDURE name (parameter1, parameter2)
{
<instructions>
RETURN (expression)
}``````
Here's pseudocode for the `calcLineSlope` procedure and calls:
``````PROCEDURE calcLineSlope (x1, y1, x2, y2)
{
yDiff ← y2 - y1
xDiff ← x2 - x1
slope ← yDiff / xDiff
RETURN slope
}

slope1 ← calcLineSlope(1, 2, 4, 5)
slope2 ← calcLineSlope(0, -1, 3, 10)``````