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

Exploring The Main Function Categories In JavaScript

User Defined Function

Built-in Function

Pure Function

First Class Function

Higher Order Function

Function

1

2

3

4

5

Function

Unleashing The Power Of User-Defined Functions In JavaScript

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

greet("student");

The function keyword is used to declare functions

Unleashing The Power Of User-Defined Functions In JavaScript

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

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

greet("student");

Unleashing The Power Of User-Defined Functions In JavaScript

Inside the function, we passed a parameter

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

greet("student");

We called the parameter here

Unleashing The Power Of User-Defined Functions In JavaScript

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

greet("student");

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

Unleash Your Creativity With User-Defined Functions in JavaScript

function greet(name) {

    return `Hello, ${name}!`;

}

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

Function Declaration

CODE:

OUTPUT:

Unleash Your Creativity With User-Defined Functions in JavaScript

function greet(name) {

    return `Hello, ${name}!`;

}

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

Function Declaration

CODE:

OUTPUT:

Unleash Your Creativity With User-Defined Functions in JavaScript

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

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

Function Expression

CODE:

OUTPUT:

Unleash Your Creativity With User-Defined Functions in JavaScript

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

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

Arrow Function

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

By Content ITV

Functions: The Backbone of Modular codes-I

  • 40