Functions:

The Backbone of Modular codes-1

Learning Outcome

5

Recognize how functions improve code reusability and structure

4

Call functions and interpret returned results

3

Pass values to functions using parameters

2

Write and define simple JavaScript functions

1

Understand what control statements are in JavaScript

How Can We Avoid Writing The Same Code Repeatedly?

We can avoid this by creating reusable functions

Input

Output

Reusable blocks of code that perform specific tasks

What is Function?

Why Do We Need Functions ?

Why We Need Functions: Simplifying, Reusing, And Organizing Code

Encapsulate Code

Promote Reusability

Simplify Maintenance

Enable Abstraction

Improve Readability

1

2

3

4

5

Need for Functions

SYNTAX

function greet(name) {
console.log("Hello, " + name + "!");
}

greet("student");

The function keyword is used to declare functions

After the `function` keyword, we declare the name of the function

function greet(name) {
console.log("Hello, " + name + "!");
}

greet("student");

SYNTAX

Inside the function, we passed a parameter

function greet(name) {
console.log("Hello, " + name + "!");
}

greet("student");

We called the parameter here

SYNTAX

function greet(name) {
console.log("Hello, " + name + "!");
}

greet("student");

Inside this function block `{ }`, we write the code to be executed

SYNTAX

const greet = function(name) {
    return `Hello, ${name}!`;
}

console.log(greet("Students"));

Function Expression

CODE:

OUTPUT:

Summary

5

Functions are a core building block of JavaScript programs

4

Return statements send values back to the caller

3

Parameters allow data to be passed into functions

2

They reduce repetition and improve readability

1

Functions group reusable code into blocks

Quiz

 What is a function in JavaScript?

A.  A variable that stores numbers

B.  A data type

C. A block of reusable code

D. A loop

Quiz-Answer

A. A variable that stores numbers

B. A data type

C. A block of reusable code

D. A loop

 What is a function in JavaScript?

Functions: The Backbone of Modular codes-I (OG)

By Content ITV

Functions: The Backbone of Modular codes-I (OG)

  • 20