Index

Conventional (Named) Functions


Note that methods (on a class) are a type of conventional functions (as described here).

Example 1
This is a conventional function format. The function is named -- squareNumber.
/** Returns square of input param x */
function squareNumber(x) {
	return x * x;
}

//Try It
println('Square of 3: ' + squareNumber(3));
println('Square of (-5): ' + squareNumber(-5));
Example 2
This is a conventional function format. The function is named -- isOdd.
/** Returns true if param x is odd */
function isOdd(x) {
	return x % 2 == 1;
}

//Try It
println('Is 17 Odd: ' + isOdd(17));
println('Is 18 Odd: ' + isOdd(18));

Abbreviated (Arrow) Functions (Anonymous Functions)


Overview


These functions are called anonymous because they are unnamed.

They are called arrow functions because they use an arrow between the declared input and the function logic (as you'll see in the examples).
It is relatively common for object languages to support arrow syntax (either -> or =>).

Simple (One-Line) Function Examples


When functions are simple enough to be coded on one line, the return value is implied (as shown below).

Example 1 (Triple Number)
Here is a function where the input is a number (primitive) and the output is a number.

This function simply triples the input, and returns the result
//Outputs the triple of input x
let tripleNumFct = (x) => 3 * x

//Try It
let x = 10;
let y = tripleNumFct(x);
println(y);
Example 2 (Square Number)
We assign it to a var myFct and then call myFct.

(x) = function input params
x*x = function operation
// Assigns function to var myFct */
let myFct = (x) => x*x;

//Try It
let result = myFct(3);
println('Square of 3: ' + result);
Example 3 (getFirst - Object Input)
Here is a function where the input is an object (an array) and the output is an element from the array.

This function returns the first element in the input array.
let getFirstFct = (array) => array.get(0);

let array = ['A++', 'A', 'A-'];
let result = getFirstFct(array);
println('Result is ' + result);


Multi-Line Function Examples


When functions are more complex, we can code them on multiple lines. When then need to explicitly use the return keyword (as shown below).

Example 1 (Max)
Simple function to return max of three numbers.
// Returns max of three numbers
let maxFct = (a, b, c) => {
	let max = (a >= b) ? a : b;
	return (max >= c) ? max : c;
}

//Try It
let a = 2;
let b = 11;
let c = 3;
println(maxFct(a, b, c));
Example 2 (Print Array)
Here is a function that simply prints the elements in a list.
//Prints a list/array
let fct = (list) => {
	for (let nextElem of list) {
		println(nextElem);
	}
}

//Try It
let names = ['Asha', 'Chin', 'Kofi', 'Chin'];
fct(names);
Example 3 (Sum Array)
Here is a function that returns the sum of the numbers in a list.
//Sums the numbers in a list/array
let fct = (list) => {
	let sum = 0;
	for (let nextElem of list) {
		sum += nextElem;
	}
	return sum;
}

//Try It
let numbers = [10, 20, 30, 40];
let sum = fct(numbers);
println("Sum of list is: " + sum);
Example 4 (Count Evens)
Here is a function that returns a count of the even numbers in a list.
//Function that returns true if num is even
let isEvenFct = (num) => num % 2 == 0;

//Counts the even numbers in a list/array
let fct = (list) => {
	let count = 0;
	for (let nextElem of list) {
		if (isEvenFct(nextElem))
			count++;
	}
	return count;
}

//Try It
let numbers = [7, 8, 9, 10];
let result = fct(numbers);
println("Even Count: " + result);
Example 5 (Factorial)
Simple function to return max of three numbers.
// Returns factorial of param num
let factorialFct = (num) => {
	let result = 1;
	let x = num;
	for (let x = num; x > 1; x--)
		result *= x;
	return result;
}

//Try It
println('Factorial of 2: ' + factorialFct(2));
println('Factorial of 3: ' + factorialFct(3));
println('Factorial of 4: ' + factorialFct(4));
Example 6 (Find First Index)
Here is a function that searches for the param searchValue in a list and returns the index of the first match.
// Returns index of first match (of searchValue in list)
let findFirstIndex = (list, searchValue) => {
	for (let index = 0; index < list.size(); index++) {
		let nextElem = list.get(index);
		if (nextElem.equals(searchValue))
			return index;
	}
	return -1;
}

//Try It
let names = ['Asha', 'Chin', 'Kofi', 'Chin'];
let nm = 'Chin';
let firstIndex = findFirstIndex(names, nm);
println(firstIndex);
Example 7 (Is Prime?)
Here we code a function that tests if a number is prime
let isPrime = (num) => {
	if (num == 1) return false;
	if (num == 2) return true;
	if (num % 2 == 0) return false;
	//3, 5, 7
	if (num <= 7) return true;
	for (let f = 3; f <= 7; f += 2)
		if (num % f == 0)
			return false;
	let max = Math.floor(Math.sqrt(num));
	//From 5, prime factors are 6k+/-1
	let z = 6;
	max++;	//to include 6k-1let isPrime = (num) => {
	while (z <= max) {
		if ((num % (z-1) == 0) || (num % (z+1) == 0))
			return false;
		z += 6;
	}
	return true;
}

//Try It
println("is Prime (37): " + isPrime(37));
//test, expect 25 between 1-100
let count = 1; //count "2"
for (let n = 3; n < 100; n += 2)
	if (isPrime(n))
		count++;
println("Prime count (1-100): " + count);
//test, expect 168 between 1-1000
count = 1; //count "2"
for (let n = 3; n < 1000; n += 2)
	if (isPrime(n))
		count++;
println("Prime count (1-1000): " + count);
println("\nList primes 1-100");
for (let n = 3; n < 100; n += 2)
	if (isPrime(n))
		println("Prime: " + n);


Special Case - Running Function Immediately


If desired we can run a function immediately (without assigning to var).

Example
An anonymous (unamed) function is highlighted. It is invoked (run) immediately (without assigning function to var like in prev step).

(x) = function input params
x*x = function operation

side note: this function type is sometimes called an immediately invoked function expression (IIFE)
// Runs function immediately */
let result = ((x) => x*x)(4);
println('Square of 4: ' + result);