Podcast
Questions and Answers
Which of the following best describes the primary purpose of functions in programming?
Which of the following best describes the primary purpose of functions in programming?
- To define global variables that can be accessed from anywhere in the script.
- To create interactive user interfaces with built-in styling.
- To automatically execute code when a page loads.
- To reduce code duplication by encapsulating reusable blocks of code. (correct)
What is the correct syntax for declaring a function in JavaScript?
What is the correct syntax for declaring a function in JavaScript?
- `function functionName() { // body }` (correct)
- `function = functionName() { // body }`
- `function functionName()[ // body ]`
- `function-name() { // body }`
Consider the following code:
`function outerFunction() {
let outerVar = 'Outer';
function innerFunction() {
let innerVar = 'Inner';
console.log(outerVar + innerVar);
}
innerFunction();
}
outerFunction();`
What will be the output of this code?
Consider the following code:
`function outerFunction() { let outerVar = 'Outer'; function innerFunction() { let innerVar = 'Inner'; console.log(outerVar + innerVar); } innerFunction(); }
outerFunction();`
What will be the output of this code?
- `undefined`
- `Inner`
- `Outer`
- `OuterInner` (correct)
What is the scope of a variable declared inside a function?
What is the scope of a variable declared inside a function?
If a variable with the same name is declared both globally and locally within a function, which variable takes precedence inside the function?
If a variable with the same name is declared both globally and locally within a function, which variable takes precedence inside the function?
Which of the following statements accurately describes the use of parameters in a function?
Which of the following statements accurately describes the use of parameters in a function?
Consider the following code:
`let globalVar = 10;
function modifyGlobal() {
globalVar = 20;
}
modifyGlobal();
console.log(globalVar);`
What will be the output of console.log(globalVar)
?
Consider the following code:
`let globalVar = 10;
function modifyGlobal() { globalVar = 20; }
modifyGlobal(); console.log(globalVar);`
What will be the output of console.log(globalVar)
?
What is the main advantage of using functions to organize code, instead of writing code in a linear fashion?
What is the main advantage of using functions to organize code, instead of writing code in a linear fashion?
Which of the following code snippets demonstrates the correct way to call a function named calculateSum
with two arguments, 5
and 3
?
Which of the following code snippets demonstrates the correct way to call a function named calculateSum
with two arguments, 5
and 3
?
Consider the following code:
`function testFunction() {
message = 'Hello';
console.log(message);
}
testFunction();
console.log(message);`
What will be the output?
Consider the following code:
`function testFunction() { message = 'Hello'; console.log(message); }
testFunction(); console.log(message);`
What will be the output?
Flashcards
Functions
Functions
Reusable blocks of code that perform a specific task.
Function Declaration
Function Declaration
Creating a function using the function
keyword, a name, parameters, and code within curly braces.
Function Body
Function Body
The code within the curly braces {}
that defines what the function does.
Calling a Function
Calling a Function
Signup and view all the flashcards
Local Variables
Local Variables
Signup and view all the flashcards
Avoiding Code Duplication
Avoiding Code Duplication
Signup and view all the flashcards
Parameters
Parameters
Signup and view all the flashcards
Built-in Functions
Built-in Functions
Signup and view all the flashcards
Function Reusability
Function Reusability
Signup and view all the flashcards
Single-Point Maintenance
Single-Point Maintenance
Signup and view all the flashcards
Study Notes
- Functions are the building blocks of programs.
- Functions allow code to be called many times without repetition.
- Built-in functions include alert(message), prompt(message, default), and confirm(question).
- New functions can also be created.
Function Declaration
- A function can be created using a function declaration.
- Start with the "function" keyword.
- Followed by the name of the function.
- Followed by a list of parameters between parentheses (can be empty).
- Finally, the function code (the "function body") goes between curly braces.
Function Structure
function name(parameter1, parameter2,... parameterN) { // body }
Calling a Function
- Functions are called by their name.
- Calling a function executes the code within it.
- Example:
function showMessage() { alert( 'Hello everyone!' ); }
showMessage(); // executes the function and shows the message
showMessage(); // executes the function and shows the message
- Functions help avoid code duplication.
- Modifying the code inside a function changes the message in one place
Local Variables
- Variables declared inside a function are only visible inside that function.
- Example:
function showMessage() { let message = "Hello, I'm JavaScript!"; alert( message ); }
showMessage(); // Output: Hello, I'm JavaScript!
alert( message ); // Error: message is not defined (outside the function)
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Description
This lesson covers the basics of JavaScript functions, including function declarations and how to call them. Functions are essential for reusing code and building more complex programs. Discover how to define and execute functions effectively.