function is a concept for C programming language, objective-c is entirely relies on C.
To define a function, you need provide four components: return value, function name, parameters and code block.
like this:
1 2 3 | |
the return value is a integer value, function name is getRandownInteger, there are two paramters: minimum and maximum, code block is enclosed by braces.
Function also can use pointer reference as return value or paramaters, like this:
1 2 3 4 5 | |
The declaration and implementation can be sepearted, the declaration tells the compiler that these is a function and the implementation do the real work.
1 2 3 4 5 6 7 8 | |
If a method want call function , the function declaration should be defined before the method.
static word
static functions
By default, all functions have a global scope, if you want limit the function’s scope to the current file, use ‘static’ keyword:
1 2 3 4 5 6 | |
Note that static keyword should be use on both the function declaration and implementation.
static local variable
By defualt, the local variable in a function will be reset each time the function is called. If you use ‘static’ midifier on a local variable in a function , the variable value will be remembered.
Note that the local variable’s scope do not changed, it still only accessible inside the function itself.