Podcast
Questions and Answers
Which of the following statements is MOST accurate regarding the primary benefit of using functions in programming?
Which of the following statements is MOST accurate regarding the primary benefit of using functions in programming?
- Functions enhance code readability by automatically adding comments throughout the script.
- Functions automatically optimize code execution speed by compressing the script size.
- Functions eliminate the need for variable declarations, simplifying the overall code structure.
- Functions facilitate code reusability, reducing redundancy and improving maintainability. (correct)
Consider the following JavaScript code:
`function outerFunction() {
let outerVar = 'Outer';
function innerFunction() {
let innerVar = 'Inner';
alert(outerVar);
}
innerFunction();
alert(innerVar);
}
outerFunction();`
What will be the output of this code?
Consider the following JavaScript code:
`function outerFunction() { let outerVar = 'Outer'; function innerFunction() { let innerVar = 'Inner'; alert(outerVar); } innerFunction(); alert(innerVar); }
outerFunction();`
What will be the output of this code?
- Alerts 'Outer', then alerts 'Inner'.
- Throws an error because neither `outerVar` nor `innerVar` are globally defined.
- Alerts 'Inner', then alerts 'Outer'.
- Alerts 'Outer', then throws an error because `innerVar` is not defined in the `outerFunction` scope. (correct)
What is the correct syntax to define a function named calculateSum
that accepts two parameters, a
and b
, and returns their sum?
What is the correct syntax to define a function named calculateSum
that accepts two parameters, a
and b
, and returns their sum?
- `calculateSum(a, b) => { return a + b; }`
- `calculateSum = function(a, b) { return a + b; }`
- `function calculateSum(a, b) => a + b;`
- `function calculateSum(a, b) { return a + b; }` (correct)
Given the following JavaScript code:
`let globalVar = 10;
function modifyGlobal() {
globalVar = 20;
}
function checkValue() {
let globalVar = 5;
console.log(globalVar);
}
modifyGlobal();
checkValue();`
What value will be logged to the console, and why?
Given the following JavaScript code:
`let globalVar = 10;
function modifyGlobal() { globalVar = 20; }
function checkValue() { let globalVar = 5; console.log(globalVar); }
modifyGlobal(); checkValue();`
What value will be logged to the console, and why?
What is the significance of the parameters in a function definition?
What is the significance of the parameters in a function definition?
Consider the following JavaScript function:
`function example() {
let x = 10;
if (true) {
let x = 20;
console.log(x);
}
console.log(x);
}
example();`
What will be printed to the console?
Consider the following JavaScript function:
`function example() { let x = 10; if (true) { let x = 20; console.log(x); } console.log(x); }
example();`
What will be printed to the console?
In JavaScript, what is the purpose of the return
statement within a function?
In JavaScript, what is the purpose of the return
statement within a function?
Examine the code snippet below:
`function modifyArray(arr) {
arr.push(4);
arr = [5, 6, 7];
return arr;
}
let myArray = [1, 2, 3];
let newArray = modifyArray(myArray);
console.log(myArray);`
What will be the output of console.log(myArray)
?
Examine the code snippet below:
`function modifyArray(arr) { arr.push(4); arr = [5, 6, 7]; return arr; }
let myArray = [1, 2, 3]; let newArray = modifyArray(myArray);
console.log(myArray);`
What will be the output of console.log(myArray)
?
What is the difference between a function declaration and a function expression in JavaScript?
What is the difference between a function declaration and a function expression in JavaScript?
Consider the following code involving a recursive function:
`function factorial(n) {
if (n <= 1) {
return 1;
}
return n * factorial(n - 1);
}
console.log(factorial(5));`
What value will be printed to the console?
Consider the following code involving a recursive function:
`function factorial(n) { if (n <= 1) { return 1; } return n * factorial(n - 1); }
console.log(factorial(5));`
What value will be printed to the console?
Flashcards
What are Functions?
What are Functions?
Reusable blocks of code that perform a specific task.
Function declaration syntax
Function declaration syntax
The function
keyword, a name, parentheses ()
, and curly braces {}
.
What does function's call do?
What does function's call do?
Executes the code within a defined function.
Local Variable
Local Variable
Signup and view all the flashcards
What problem do functions solve?
What problem do functions solve?
Signup and view all the flashcards
What is one advantage of using functions?
What is one advantage of using functions?
Signup and view all the flashcards
Examples of built-in functions
Examples of built-in functions
Signup and view all the flashcards
Function Declaration Structure
Function Declaration Structure
Signup and view all the flashcards
How do you call a function?
How do you call a function?
Signup and view all the flashcards
Scope of Local Variables.
Scope of Local Variables.
Signup and view all the flashcards
Study Notes
- Functions are the building blocks that allow code to be called without repetition
- Built-in functions previously seen include
alert(message)
,prompt(message, default)
andconfirm(question)
Function Declaration
- Functions can be created using a function declaration
- The
function
keyword goes first, followed by the name of the function - Next is a list of parameters between parentheses, comma-separated, can be empty
- Finally is the code of the function, also named the function body, between curly braces
function name(parameter1, parameter2,... parameterN) {
// body
}
- New functions are called by their name
- Calling
showMessage()
executes the code of the function - Functions help to avoid code duplication
- Changing the message that is shown only requires modification in one place, so if we need to change the way a message is shown, we only need to change the function
Local Variables
- A variable declared inside a function is only visible inside that function
function showMessage() {
let message = "Hello, I'm JavaScript!"; // local variable
alert( message );
}
showMessage(); // Hello, I'm JavaScript!
alert( message ); // error! The variable message is local to the function
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Description
Functions are the building blocks that allow code to be called without repetition. Functions can be created using a function declaration. Functions help to avoid code duplication.