What is a Function?
-A function is a block of code designed to perform a specific task.
- Instead of writing the same code again and again, we can put it inside a function and reuse it.
function functionName() {
// code to execute
}
function greet() {
console.log("Hello");
}
greet(); // calling the function
O/P: Hello
Functions with Parameters
- Parameters allow you to pass values into a function.
function greet(name) {
console.log("Hello " + name);
}
greet("Anees");
O/P: Hello Anees
Functions with Return Value
- A function can return a value using return.
function add(a, b) {
return a + b;
}
let result = add(2, 3);
console.log(result);
O/P: 5
Top comments (0)