Created: 2023-01-11 16:36
Status: #concept
Subject: Programming
Tags: C Variable Method Argument Parameter Scope Callback Function
Function
Definition
// 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
- 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.
- Function Body is the code found inside the curly braces
{ }
of the function.- it creates a Block Scope.
- 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 Definitions are always Hoisted up, so you can call functions before their declarations.
function myFunction() {
alert('hello');
}
myFunction();
// calls the function once
Default Parameters
You can write default parameters by assigning a value inside the function parameters.
- By default, empty parameters become undefined (JavaScript).
- Default Parameters work when the input is
undefined
.
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 ()
.
- It has no Identifier.
- We can call it after declaring it, but using another set of
()
after. - We can assign it to variables.
- It does not pollute the Global Scope because it deallocates itself when it resolves.
// 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;
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);