xxxxxxxxxx
A function is a reusable set of statements to perform a task or calculate a value.
Functions can be passed one or more values and can return a value at the end of their execution.
In order to use a function, you must define it somewhere in the scope where you wish to call it.
xxxxxxxxxx
function factorial(n) {
if (n == 0) {
return 1;
} else {
return factorial(n - 1) * n;
}
}
xxxxxxxxxx
function functionName(parameter1, parameter2, parameter3) {
body code goes here
}
xxxxxxxxxx
/function <name:unknown>
/function <name:string>
xxxxxxxxxx
[PHP]
<?php
function helloWorld() {
echo "Hello World";
}
helloWorld();
?>
[Python]
def helloWorld():
print("Hello World")
helloWorld()
[JavaScript / JS Normal Function]
function helloWorld() {
console.log("Hello World!");
}
helloWorld()
[JavaScript / JS Dynamic, Arrow Function]
const helloWorld = () => {
console.log("Hello World!");
}
helloWorld()
xxxxxxxxxx
A function is a set of statements that take inputs, do some specific
computation and produces output.
The idea is to put some commonly or repeatedly done task together and make a
function so that instead of writing the same code again and again for different
inputs, we can call the function.
xxxxxxxxxx
function nama_function(parameter/argument 1, parameter/argument 2, ) {
[Isi dari function berupa tindakan/steatment]
return [expression];
}