-
Functions
A function in programming is a task you the program to perform. It tells the computer what to do and how to do it when it runs a certain program. It's kind of like telling a robot what to do. For example, if you want the robot to make something for you, you would give it instructions or write down a recipe on how to make that thing. That's what functions are like: instructions for the computer on how to make something happen.
-
-
Defined Functions
Javascript
Example of function in javascript.
function addition(a, b) {
	return a + b;
}

console.log(addition(1, 2)); // 3
int addition(int a, int b) {
 return a + b;
}

void main() {
 print(addition(1, 2)); // 3
}

import std.stdio : write;

int addition(int a, int b) {
	return a + b;
}

void main()
{
	write(addition(1, 2)); // 3
}

<?php
function addition($a, $b) {
	return $a + $b;
}

print(addition(1, 2)); // 3

def addition(a, b):
	return a + b

print(addition(1, 2)) # 3

package main

import "fmt"

func addition(a int, b int) int {
	return a + b
}

func main() {
	fmt.Println(addition(1, 2)) // 3
}

-
-
-
Anonymous Functions
Javascript
Example of anonymous function in javascript.
(function (a, b) {
	console.log(a + b); // 3
})(1, 2);
void main() {
 (int a, int b) {
	print(a + b); // 3
 }(1, 2);
}

import std.stdio : write;

void main()
{
	(int a, int b) {
		write(a + b); // 3
	}(1, 2);
}

<?php
call_user_func(function ($a, $b) {
	print($a + $b); // 3
}, 1, 2);
print((lambda a, b: a + b)(1, 2)) # 3

package main

import "fmt"

func main() {
	func(a int, b int) {
		fmt.Println(a + b) // 3
	}(1, 2)
}

-
-
-
Variable Functions
Javascript
Example of variable function in javascript.
var addition = function (a, b) {
	return a + b;
};

console.log(addition(1, 2)); // 3
void main() {
 var addition = (int a, int b) {
	return a + b;
 };

 print(addition(1, 2)); // 3
}
import std.stdio : write;

void main()
{
	auto addition = (int a, int b) { 
		return a + b; 
	};

	write(addition(1, 2)); // 3
}

<?php
$addition = function ($a, $b) {
	return $a + $b;
};

print($addition(1, 2)); // 3
addition = lambda a, b : a + b

print(addition(1,2)) # 3
package main

import "fmt"

func main() {
	var addition = func(a int, b int) int {
		return a + b
	}

	fmt.Println(addition(1, 2)) // 3
}

-
-