JavaScript Functions: Declaration and Calling
10 Questions
0 Views

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

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?

  • `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?

  • `undefined`
  • `Inner`
  • `Outer`
  • `OuterInner` (correct)

What is the scope of a variable declared inside a function?

<p>Local, accessible only within the function where it is declared. (B)</p> Signup and view all the answers

If a variable with the same name is declared both globally and locally within a function, which variable takes precedence inside the function?

<p>The local variable. (B)</p> Signup and view all the answers

Which of the following statements accurately describes the use of parameters in a function?

<p>Parameters are used to pass data into a function, allowing it to operate on different values. (A)</p> Signup and view all the answers

Consider the following code:

`let globalVar = 10;

function modifyGlobal() { globalVar = 20; }

modifyGlobal(); console.log(globalVar);`

What will be the output of console.log(globalVar)?

<p><code>20</code> (D)</p> Signup and view all the answers

What is the main advantage of using functions to organize code, instead of writing code in a linear fashion?

<p>Functions improve code readability and maintainability by breaking it into logical parts. (B)</p> Signup and view all the answers

Which of the following code snippets demonstrates the correct way to call a function named calculateSum with two arguments, 5 and 3?

<p><code>calculateSum(5, 3);</code> (B)</p> Signup and view all the answers

Consider the following code:

`function testFunction() { message = 'Hello'; console.log(message); }

testFunction(); console.log(message);`

What will be the output?

<p><code>Hello</code> will be printed twice (C)</p> Signup and view all the answers

Flashcards

Functions

Reusable blocks of code that perform a specific task.

Function Declaration

Creating a function using the function keyword, a name, parameters, and code within curly braces.

Function Body

The code within the curly braces {} that defines what the function does.

Calling a Function

To execute or run a function and perform its defined task.

Signup and view all the flashcards

Local Variables

Declaring a variable inside a function makes it only accessible within that function.

Signup and view all the flashcards

Avoiding Code Duplication

A way to avoid writing the same code multiple times by using functions.

Signup and view all the flashcards

Parameters

Values passed into a function when it is called, allowing the function to operate on different data.

Signup and view all the flashcards

Built-in Functions

Built-in functionalities available in JavaScript, like showing alerts or prompts.

Signup and view all the flashcards

Function Reusability

A function can be called multiple times throughout a script.

Signup and view all the flashcards

Single-Point Maintenance

Modifying the code in one function updates the functionality wherever that function is called.

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.

Quiz Team

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.

More Like This

Javascript Functions
10 questions

Javascript Functions

DextrousMendelevium avatar
DextrousMendelevium
Javascript Functions: Declaration and Syntax
10 questions
Javascript Function
10 questions

Javascript Function

DextrousMendelevium avatar
DextrousMendelevium
Use Quizgecko on...
Browser
Browser