Functions

A function is a block of organized code that is used to perform a single task.

You can define a function with comma separated arguments (parameters) or without an argument.

ተግባር functionName(args) {
    // function body
}

functionName is the name of your function. args are parameters (arguments) that the function expects. function body is the body of the function.

a function by default returns ባዶ (nil) value, but you can return a value using መልስ.

ተግባር add(num1, num2) {
    መልስ num1 + num2;
}

this function adds the two numbers that are passed as an argument and returns their result.

Functions are useless if we can't call them, so we can call a function like

ተግባር add(num1, num2) {
    መልስ num1 + num2;
}
 
መለያ result = add(1, 2); // 1 + 2 = 3
አውጣ result; // will print 3

here i am calling the add function with 1 and 2 as an argument and storing the return value in result variable. the function expects 2 arguments if instead i had passed 1 or 0 arguments an error will occur.

ተግባር add(num1, num2) {
    መልስ num1 + num2;
}
 
መለያ result = add(1); // error