Oct 7, 2013

vJASS for mules #4: Functions

Functions are code blocks that can be reutilized later. It takes some input, process any action above it and returns an output. Example of function:

function myFunction takes integer a, integer b returns integer
    return a + b
endfunction

As variables, functions have a name. To create a function, you need to type "function" keyword preceded of its name, "takes" keyword, parameter list, "returns" keyword" return type and finally the "endfunction" keyword that determines the end of function block.

function functionName takes argumentsList returns returnType
    // code block
endfunction

Parameter List
It's the input received by the function. Each value received is called "argument". Each parameter is in reality a variable that's waiting to have a value assigned to it and arguments are those values. That's why we have to inform a type and a name, so we can utilize this variable inside the function block. Important note: The parameters' variables only exist inside the function scope. We call variables like that as "local variables".

Return type
Each function has to return a value (even nothing is a value on programming language). Normally this value represents the processing over the input. Obviously, the returned value has to be of the same type specified on function creation.

Calling functions
We call functions through the keyword "call". Example:

call myFunction(1, 2) // It'll yield 3

Preceded of call, we write the function name and between paranthesis the values that will be assigned to the parameters of function. Those values are called arguments. If the function hasn't any parameter, you don't write any parameter, but the parenthesis is always mandatory. Important note: The argument is assigned to the parameter in order, i.e., the first argument will go to the first parameter and so on...

Function Stack
It's important to know at least a little of how the functions are read on compiler. When your compiler finds a function, he stops what he was doing and go to inside the function block. The continuation of previous code will only happen when the function block ends. A "return" call will finish the function block immediately, independent if exist or not more code below. 


Answer mentally:
  • What is a function?
  • What is an argument list?
  • What is an return type?
  • How do we call functions?


<<Previous>>                                                                                  <<Next>>

No comments :

Post a Comment