What are the Local Variables and what are the Global Variables?
var y = 10
function double(x){
var result = 2*x;
return result;
}
var answer = double(y)
console.log(answer)
• Global variables: y, answer
• Local variables: x, result
What are the parameters of the function?
function calculateArea(length, width) {
return length * width;
}
length and width
Which statement allows us to return values from functions?
return
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
Do functions need to have return statements?
No, return statements are only needed if you need to pass back a value to the calling statement.
Explain the difference between a local variable and a global variable
A local variable is defined within a function or block and is accessible only within that specific scope.
In contrast, a global variable is declared outside of any function or block, making it accessible throughout the entire program.
True or false: Functions must have parameters.
False
True or False:
A return statement stops the execution of the function
True
What is the output of this code?
function calculateSum(num){
var total = 0;
for(var i = 1; i <= num; i++){
total += i;
}
println(total);
}
calculateSum(3);
6
What is printed by the following program?
function main() {
let a = 5;
let b = 10;
let c = 15;
printValues(a, b, c);
}
function printValues(third, first, second) {
console.log(first);
console.log(second);
console.log(third);
}
main();
10
15
5
Which variables exist at point A? In other words, which variables are in scope at point A?
var x = 10;
function doSomething(){
var y = 20;
// Point A
}
function start() {
var z = 30;
doSomething();
}
x and y
How many parameters go into the function sum, and how many return values come out of the function sum?
function sum(first, second, third){
var result = first + second + third;
println(first);
println(second);
println(third);
return result;
}
3 parameters go in, 1 return value comes out
What is returned and printed for the value of b?
function start(){
var a = mystery(10, 14);
var b = mystery(82, 28);
println(a);
println(b);
}
function mystery(x, y) {
if (x < y){
return x;
}else{
return y;
}
}
28
What does API stand for in computer science?
Application Programming Interface
The following function draws a rectangle on the canvas. What is the minimum number of arguments that must be used when calling the drawSquare() function?
function drawSquare(size, color) {
let square = new Rectangle(size, size);
square.setColor(color);
square.setPosition(100, 100);
add(square);
}
2
What will the following program print?
var num = 5;
function modifyNumber() {
var num = 10;
println(num);
}
modifyNumber();
println(num);
10
5
What arguments are passed to the printNumbers method?
function start(){
printNumbers(12, 17, 65);
}
function printNumbers(first, second, third){
println(first);
println(second);
println(third);
}
12, 17, and 65
What would the following print?
function mystery1(x){
var result = x + 1;
return result;
}
function mystery2(x, y){
var result = x + y;
return result;
}
function mystery3(x){
x = mystery1(x);
var result = x * x;
return result;
}
function start() {
var y = mystery2(15, 10);
println(y);
}
25
Which of the following is true for an API?
I. Allows different devices to connect with each other
II. Allows for Interactivity between devices and software
III. Messenger program that takes requests and delivers the response back to the user
IV. Allows the creation of applications that access the features or data of an operating system, application, or other services
V. Specifies how software components should interact
VI. Used when programming graphical user interface (GUI) components
ALL
function subtract(x, y) {
console.log(x + " minus " + y + " equals: " );
let difference = x - y;
return difference;
}
console.log(subtract(10, 5));
10 minus 5 equals:
5
What is printed by the following program?
var name = "Global";
function showName() {
var name = "Local";
println(name);
}
println(name);
showName();
println(name);
Global
Local
Global
function printNumbers(x, y, z){
println(y);
println(z);
println(x);
}
function start(){
var zero = 1;
var one = 2;
var two = 0;
printNumbers(zero, one, two);
}
2
0
1
What is the return value for this function?
function drawCircle(radius, color, x, y){
var circle = new Circle(radius);
circle.setColor(color);
circle.setPosition(x, y);
add(circle);
}
no return value
“It’s a bird! It’s a plane! No, it’s Superman!”
We want to write a function isSuperman that takes in two parameters isBird and isPlane and returns true if it is in fact Superman, and false otherwise.
If it’s not a bird and it’s not a plane, it must be Superman.
function isSuperman(isBird, isPlane){
return !isBird && !isPlane;
}
function main() {
let x = sumThree(1, 2, 3);
console.log(x);
}
function sumThree(one, two, three) {
let result = one + two + three;
console.log(one);
console.log(two);
console.log(three);
return result;
}
main();
1
2
3
6