javascript

Working With Functions

A function is a group of reusable code that can be called anywhere in your program. Functions eliminate the need of writing the same code again and again.

Functions also allow a programmer to divide a big program into a small and manageable chunks.

The way to define a function in Javascript is to use the “function” keyword.

function functionName() {
 //statements here ...
}

You could also use ES6 Arrow Functions as well

Passing Parameters

Parameters can be passed while calling a function. These type of functions are called Functions with parameters.

Example

function greet(name, job){
document.write(`Name is ${name} and occupation is ${job}`);
}

<body>
<input type="button" >

The Return Statement

A Javascript function can have an optional return statement. This is required if you want to return a value from a function. This statement should be the last statement in a function.

Example

function add(a, b){
 return a + b;
}

console.log(add(2,3)) //5

Nested Functions

In a nested function, a function can be called inside another function. And it goes on and on depending on the logic of the program.

Example

function showResult(){
console.log(add(2,3)) //Our function from the previous example
}

showResult() //5

Read more about recursive functions here

The End.

Recent Posts

South Korea bans downloads of DeepSeek AI until further notice

The South Korean government announced on Monday that it had temporarily halted new downloads of…

5 days ago

Which programming language is best for software development?

As a software developer, choosing the right programming language for software development can be a…

1 week ago

What is a server farm?

A server farm, also known as a server cluster or data center, is a collection…

1 week ago

Elon Musk’s Starlink satellite internet service is awaiting authorization to begin operations in Pakistan.

Pakistan's mobile and broadband internet speeds rank in the bottom 10 percent globally, according to…

2 weeks ago

React Native Styling Guide

React Native is a popular framework for building cross-platform mobile applications using JavaScript and React.…

2 weeks ago

You can now customize your Android home screen shortcut with any widget of your choice

Google is not only passionate about developing innovative apps and services but also about finding…

2 weeks ago