1 of 23

Unit 7 - Lesson 1

Parameters and Return Explore

2 of 23

To do: January 27 - Tomorrow's test all MC!

Consider the following code segment.

numList ← [2, 4, 1, 7, 0, 5, 8]

result ← 0

FOR EACH value IN numList

{

IF(value MOD 2 = 0)

{

result ← result + 2

}

ELSE

{

result ← result + 3

}

}

What value is assigned to result after executing the code segment?

3 of 23

To do: January 27 - Tomorrow's test all MC!

Consider the following code segment.

numList ← [2, 4, 1, 7, 0, 5, 8]

result ← 0

FOR EACH value IN numList

{

IF(value MOD 2 = 0)

{

result ← result + 2

}

ELSE

{

result ← result + 3

}

}

What value is displayed as a result of executing the code segment? 17

4 of 23

Warm-up #2

Examine the mystery procedure below.

PROCEDURE mystery(numberList)

{

count ← 2

REPEAT UNTIL(count > LENGTH(numberList))

{

IF(numberList[count]<numberList[count-1])

{

DISPLAY("false")

}

ELSE

{

DISPLAY("true")

}

count←count + 1

}

}

1. What is the output after the call mystery([3,5,7,10])?

2. What is the output displayed after the call to �mystery([5, 7, 8, 3])?

3. If you see all "true", what does it tell you about the list?

5 of 23

Warm-up #2

Examine the mystery procedure below.

PROCEDURE mystery(numberList)

{

count ← 2

REPEAT UNTIL(count > LENGTH(numberList))

{

IF(numberList[count]<numberList[count-1])

{

DISPLAY("false")

}

ELSE

{

DISPLAY("true")

}

count←count + 1

}

}

1. What is the output after the call mystery([3,5,7,10])?

true true true

2. What is the output displayed after the call to �mystery([5, 7, 8, 3])?

true true false

3. If you see all "true", what does it tell you about the list?

The list is increasing

6 of 23

Go over homework

  • Khan academy - String operations, Programming Quiz 1, Programming Quiz 3 - graded on accuracy
  • Complete the Code.org U5L12 bubble 2 - Random Forecaster App and submit.

Tomorrow's test will be all multiple-choice: Lists and Loops in JavaScript and Pseudocode.

7 of 23

Function Game: Round 1

8 of 23

Function Game: Round 2

9 of 23

Function Game

function ______________(input)�{

}

10 of 23

Parameters

Parameters provide input values to our functions.

function fisherFunction( input )�{� var ret = input * 5 + 2;� return ret;�}

11 of 23

Return value

Functions can be used to generate output values (data) which is returned to where function was called in the program.

function fisherFunction( input )�{� var ret = input * 5 + 2;� return ret;�}

12 of 23

Return value -> data is returned when function is executed

if(fisherFunction(10) == 52){

console.log("yes - return equals 52");

}

else{

console.log("no - return does not equal 52");

}

function fisherFunction( input )�{� var ret = input * 5 + 2;� return ret;�}

13 of 23

Return value -> data is returned when function is executed

It is also possible for functions to return values which can be used to change the logical flow of the program.��We have already seen examples of functions that accept parameters and generate output, including:

randomNumber(min, max)

14 of 23

Example of functions with returns

randomNumber(min, max) → Returns a random number in the closed range from min to max.

App Lab API Documentation

15 of 23

Return value - Describe one possible output!

var heads = 0;

var tails = 0;

var rolls = 10;

for(var i = 0; i < rolls; i++){

if(randomNumber(0,1) == 0){

heads++

} else {

tails++

}

}

console.log(heads + " " + tails);

16 of 23

Check for understanding

17 of 23

Functions with a return command

When you call a function, you are telling the computer to run a piece of code elsewhere in your program.

When that function is finished running, execution of the program returns to the point in the code where the function was called.

18 of 23

Calling functions that returns a value: demo

function fisherFunction( input )�{� var ret = input * 5 + 2;� return ret;�}

19 of 23

Return value

When you use return you are also able to specify a single value that will be "returned" to whatever code called the function in the first place.

The effect is that the function acts like any expression that evaluates to a single value.

20 of 23

Functions with a return command

It is possible to make your program return at any point using the return command. This command tells the function to stop executing immediately, skipping any commands that appear afterwards within the function.

21 of 23

Functions with Returns and Parameters - Takeaways

  • Functions with parameters and return values help us simplify our code
  • Functions can only return one value at a time
  • A function can have:
    • No parameters and no return values
    • Parameters, but no return values
    • Return values, but no parameters
    • Parameters and return values

22 of 23

Unit 7 Key words

  • Parameter - a variable in a function definition. Used as a placeholder for values that will be passed through the function.
  • Argument - the value passed to the parameter when the function is called
  • Return value: A value sent back by a function to the place in the code where the function was called from

23 of 23

Homework

  • Write Functions with Parameters and Returns - Takeaways notes. Then submit image to Hub
  • Write 3 Unit 7 Key Words in IN. Then submit image to Hub
  • Study for MC Test on Lists and Loops (JavaScript and Pseudocode)