Created: 2023-01-11 16:36
Status: #concept
Subject: Programming
Tags: C Variable Method Argument Parameter Scope Callback Function

Function

Definition

  • Multiple lines of code grouped together in order to abstract a certain functionality - "building blocks".
  • Functions inside Objects are called Methods.

When we call a function inside itself, we use the concept of Recursion.

// a function that takes 2 float numbers and returns their average
double average(double a, double b)
{
   return (a + b) / 2;
}

C: Parts of a Function

  1. Return Type is the C Data Type that the function will return to the Function Call.
    • we cannot return multiple values like other programming langauges
    • we also cannot return arrays that are locally scoped to the function body, we must use Dynamic Memory Allocation and Pointers.
  2. Function Body is the code found inside the curly braces { } of the function.
  3. Parameters are the placeholder Local Variables that are Initialized when the program passes Arguments into the function call.
    • when the arguments passed have a different type than the parameter, then C Type Conversion occurs and adjusts to the parameter's type.
We have to always define a function or create a Function Prototype before we using it.

We can use void as a Return Type or as a Function Parameter to make the function return nothing or accept nothing, respectively.

When a function is "fruitless" or returns nothing void, then we must AVOID using the return statement with a value.

Likewise, when a function is "fruitful" and the function call should evaluate to a certain data type, we must use the return statement with a value.

// A function that takes no arguments and returns nothing to the function call
void hello(void)
{
	printf("Hello World!");
}

JavaScript

function myFunction() {
  alert('hello');
}

myFunction();
// calls the function once

Default Parameters

You can write default parameters by assigning a value inside the function parameters.

function hello(name = 'Chris') {
  console.log(`Hello ${name}!`);
}

hello('Ari'); // Hello Ari!
hello();      // Hello Chris!

Anonymous Functions (IIFE)

You can make a function that can't be reused by enclosing it within the Grouping Operator ().

// Normal Anonymous Function
(function () {
  alert('hello');
})

// Assigning to a Variable
let x = (function () {
  alert('hello');
})
x(); // function call

// Calling After Declaring (not reusable)
(function () {
  alert('hello');
})()

Arrow Functions

They are similar to Anonymous Functions and are still IIFEs, but have a different syntax.

Instead of doing function(params) {} we can simply do (params) => {}.
(arg1, arg2, ..., argN) => expression;

  • You can omit the {} of the Function definition if the Function Body is only 1 line.
  • You can omit the () of the Parameter if it's the only one.
  • You can omit the return statement if the function is a single line & if they function needs to return something.
textBox.addEventListener('keydown', (event) => {
  console.log(`You pressed "${event.key}".`);
});

// We can ommit the Function Definition's Curly Braces if it's only 1 line
textBox.addEventListener('keydown', (event) => console.log(`You pressed "${event.key}".`));
const originals = [1, 2, 3];

const doubled = originals.map((item) => item * 2); // omit return

console.log(doubled); // [2, 4, 6]

/* it is equivalent to this */

function doubleItem(item) {
  return item * 2;
}

const doubled = originals.map(doubleItem);

References