”;
Functions are useful in programming because they reduce time and effort for writing the code again and again. A well written function code offers high reusability. This chapter explains you how to write and work with functions in LOLCODE.
Definition of a Function
A function is a set of statements that are executed all at once by calling that function. In LOLCODE, a function’s definition starts with the keyword “HOW IZ I” and the closing keyword is “IF U SAY SO”.
The syntax for writing a function in LOLCODE is−
HOW IZ I <function name> [YR <parameter/argument> [AN YR <other _arguments..> …]] <code block to execute / Set of statements to execute> IF U SAY SO
Important Points:
Consider the following important points when you are defining a LOLCODE function −
-
In LOLCODE, the function can accept only a certain fixed number of arguments as an input.
-
The arguments or parameters, are the identifiers that become a variable to the function.
-
Functions in LOLCODE can’t access any other values other than the values passed to them as arguments.
Returning Value from a Function
Return in coding means something that is given back. In programming, a function can return some value to the program when its execution is completed. In LOLCODE, functions return varying values as explained below −
-
FOUND YR <any_expression> returns the value of the expression when function block is executed completely.
-
GTFO returns no value (NOOB), which is similar to return 0 in other programming languages like C and Java.
-
If no other return statement is found, then IF U SAY SO is executed and the value in the IT variable is returned
.
Calling Functions
A function is defined in the body of program and is later called for execution. A function which accepts a given number of arguments is called as shown below −
I IZ <function_name> [YR <expression_One> [AN YR <expression_Two> [AN YR <expression_Three> ...]]] MKAY
While calling a function, the expression is formed by the function name, followed by the number of arguments that the function will accept. These arguments can be simple variables or any expressions. If a function accepts any expression instead of a simple value, then the expressions” values are calculated before the function is called.
Please remember that the number of arguments a function will accept, should be defined in the definition of the function.
Example
HAI HOW DUZ I MAINUMBA I HAS A NUMBA GIMMEH NUMBA FOUND YR NUMBA IF U SAY SO VISIBLE MAINUMBA KTHXBYE
When you run the above code, it will ask for an input, and then when you submit the input, you’ll see the same as the result. For example, if we enter 55, it will print 55.
Example
HAI 1.2 HOW IZ I MULTIPLY YR FIRSTOPERANT AN YR SECONDOPERANT FOUND YR PRODUKT OF FIRSTOPERANT AN SECONDOPERANT IF U SAY SO VISIBLE I IZ MULTIPLY YR 2 AN YR 3 KTHXBYE
The above function that performs multiplication of input operands will print the following output when you run it−
sh- 4.3$ lci main.lo 6
Example
HAI 1.2 I HAS A STRINGARRAY ITZ A BUKKIT STRINGARRAY HAS A VAR17 ITZ "OBJECT1" STRINGARRAY HAS A VAR18 ITZ "OBJECT2" HOW IZ STRINGARRAY ACCESS YR VARIABLE FOUND YR STRINGARRAY''Z SRS VARIABLE IF U SAY SO I HAS A STRING ITZ "VAR17" VISIBLE STRINGARRAY IZ ACCESS YR STRING MKAY KTHXBYE
The output that the above code will produce is −
sh- 4.3$ lci main.lo OBJECT1
”;