Twa kɔ nsɛm atitiriw so
Log in
Sign up for FREE
arrow_back
Laabri

AP CSP Javascript

star
star
star
star
star
Last updated over 3 years ago
16 Nsɛmmisa
Hyɛ no nsow a efi ɔkyerɛwfo no hɔ:

Functions with Parameters and Return Values

Functions with Parameters 1
1
1
1
1
1
1
1
1
1
Function with Parameters 2
1
1
Function with Return Values
1
1
1
1
1

Watch the video above https://www.youtube.com/watch?v=31HHVsWU1Fc&t=283s until 4:00 then remix the code below to practice afterwards.

https://studio.code.org/projects/applab/yWChCh8k-p6EzCGxbZ0pa3qptDUt9A4BGtnjFKwBR-I

Use the image attached to declare and call functions in Applab.

Asemmisa {{asɛmmisaAhyɛnsode}}
1.

Why do we write functions?

I. Make our code easier to understand by giving a readable name to a group of instructions II. Avoid writing repeated code III. Make our code reusable

Asemmisa {{asɛmmisaAhyɛnsode}}
2.

When we put a variable between parenthesis, what is it called?

Asemmisa {{asɛmmisaAhyɛnsode}}
3.

In the following function printThreeTimes:

function printThreeTimes(word){

println(word);

println(word);

println(word);

}

What is the parameter of the function?

Asemmisa {{asɛmmisaAhyɛnsode}}
4.

On which line is the "call" to the function doubleNumber on the video? (video: 4:50 )

Asemmisa {{asɛmmisaAhyɛnsode}}
5.

What would I get if I called doubleNumber(10); ? (video: 5:01 )

Asemmisa {{asɛmmisaAhyɛnsode}}
6.

After you watched all the video, Define the printNumbers function that takes three integers and print them in given order

function printNumbers(two, one, zero){

println(two);

println(one);

println(zero);

}

Call printNumbers function whenever printNumberBtn is clicked (hint:onEvent("printNumberBtn", "click", function(){ call functions here } );

var zero =0;

var one=1;

var two= 2;

printNumbers(zero, one, two);

What will be displayed on the screen?

Asemmisa {{asɛmmisaAhyɛnsode}}
7.

Revise the printNumbers function to print given parameters in below order.

function printNumbers(two, one, zero){

println(two);

println(zero);

println(zero);

println(one);

}

Revise the code inside printNumberBtn click event function as below.

printNumbers(12, 17, 65);

What will be displayed on the screen?

Asemmisa {{asɛmmisaAhyɛnsode}}
8.

“It’s a bird! It’s a plane! No, it’s Superman!”

If it’s not a bird and it’s not a plane, it must be Superman.

We want to write a function isSuperman that takes in two boolean parameters isBird and isPlane and returns true if it is in fact Superman, not a bird and not a plane, and false otherwise.

Which of the following functions is the correct implementation of isSuperman?

Asemmisa {{asɛmmisaAhyɛnsode}}
9.

True or False: Functions must have parameters.

Watch the video above and please use your remixed project.

https://studio.code.org/projects/applab/yWChCh8k-p6EzCGxbZ0pa3qptDUt9A4BGtnjFKwBR-I

Watch the video above https://www.youtube.com/watch?v=obwNNtwh7uw until 2:20 then code along after.

https://studio.code.org/projects/applab/yWChCh8k-p6EzCGxbZ0pa3qptDUt9A4BGtnjFKwBR-I

Image attached shows how to call doubleNumber(x) inside doubleBtn click event function. Implement sumBtn event function with sum(num1, num2) calls inside your code.

Asemmisa {{asɛmmisaAhyɛnsode}}
10.

How many parameters does the function sum use?

Asemmisa {{asɛmmisaAhyɛnsode}}
11.

What would get printed if we added the code,

sum(4, 3); ?

Asemmisa {{asɛmmisaAhyɛnsode}}
12.

Revise your doubleNumber() function to return value instead of printing it.

Replace all code inside doubleNumberBtn to below segment.

var numApples=4;

var twiceAsMany=doubleNumber(numApples);

println("Before : "+numApples);

println("After : "+twiceAsMany);

Will will be printed when this code is run?

Asemmisa {{asɛmmisaAhyɛnsode}}
13.

What happens if you try to print a function call when the function doesn't have a return value?

console.log( printNumbers(13,15,17) );

console.log ( sum(13,15,17) );

Asemmisa {{asɛmmisaAhyɛnsode}}
14.

Code along with the video3 and revise the function doubleNumber to return a value.( video:2:15)

What happens if you try to call the function with a return value like below?

doubleNumber(6);

randomNumber(1,10);

Asemmisa {{asɛmmisaAhyɛnsode}}
15.

Revise doubleNumber(x) function and call it inside doubleNumberBtn as shown below.

What is printed on the display?

Asemmisa {{asɛmmisaAhyɛnsode}}
16.

List 3 things you have discovered today.