DEV Community

Cover image for FUNCTIONS IN PROGRAMMING
Vinoth Kumar
Vinoth Kumar

Posted on

FUNCTIONS IN PROGRAMMING

FUNCTION: It's reusable blocks of code designed to perform specific tasks. They allow you to organize, reuse, and modularize code.
They can take inputs, perform actions, and return outputs.

UNDERSTANDING FUNCTIONS:
In functions, parameters are placeholders defined in the function, while arguments are the actual values you pass when calling the function.

PARAMETER: name (placeholder inside the function).
ARUGUMENT: "Vinoth" (real value given at call time).

EXAMPLE:

function greed(name) {   // 'name' is a parameter
  console.log("Hello " + name);
}

greedy("Vinoth");  // "Vinoth" is an argument
Enter fullscreen mode Exit fullscreen mode

DEFAULT PARAMETER:

  1. Default parameters are used when no argument is provided during the function call.
  2. If no value is passed, the function automatically uses the default value.

EXAMPLE:

function greedy(name = "Guest") {
  console.log("Hello, " + name);
}

greedy();
greedy("Kumar");
Enter fullscreen mode Exit fullscreen mode

RETURN STATEMENT:

  1. The return statement is used to send a result back from a function.
  2. When return executes, the function stops running at that point.
  3. The returned value can be stored in a variable or used directly.

EXAMPLE:

function add(a, b) {
  return a + b; // returns the sum
}

let result = add(50, 25);
console.log(result);
Enter fullscreen mode Exit fullscreen mode

TYPES OF FUNCTIONS:

Top comments (0)