Log in
Sign up for FREE
arrow_back
Library

AP CSP Javascript

star
star
star
star
star
Last updated about 3 years ago
16 questions
Note from the author:
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.
Question 1
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

Question 2
2.

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

Question 3
3.

In the following function printThreeTimes:

function printThreeTimes(word){
println(word);
println(word);
println(word);
}

What is the parameter of the function?

Question 4
4.

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

Question 5
5.

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

Question 6
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?

Question 7
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?

Question 8
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?

Question 9
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.
Question 10
10.

How many parameters does the function sum use?

Question 11
11.

What would get printed if we added the code,
sum(4, 3); ?

Question 12
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?

Question 13
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) );

Question 14
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);

Question 15
15.

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

What is printed on the display?

Question 16
16.

List 3 things you have discovered today.