This chapter has introduced static functions: those used in procedural programming as opposed to Instance Methods used to implement object-oriented programming.
References in square brackets link to fully discussions of summary items below.
The general syntax for defining a static function is
(formal parameter list
The formal parameter list can be empty or contain one or more comma separated formal parameter entries. [Function Parameters] Each formal parameter entry has the form
If the function is going to be called from outside its class, the heading needs
to start with
public before the
static. [Library Classes]
If returnTypeOrVoid in the heading is not
void, there must be a
return statement in the function body. A return statement has the form
where the expression should be of the same type as in returnTypeOrVoid. Execution of the function terminates immediately when a return statement is reached. [Returned Function Values]
Execution of a program starts at a function with a heading including
static void Main
There are various conventions for putting documentation just above the headings
of function definitions. The official format, specified by C# and recognized by
Xamarin Studio, involves putting the function interface description on
///. [Function Documentation]
A function call takes the form
( actual parameter list
A function call makes the function definition be executed.
The actual parameter list is a comma separated list of the same length as the formal parameter list. Each entry is an expression. The entries in an actual parameter list do not include type declarations.
Effectively, the function execution starts by assigning to each formal parameter variable the corresponding value from evaluating the actual parameter expression. In particular, that means the actual parameter values must be allowed in an assignment statement for a variable of the formal parameter’s type! [Multiple Function Parameters]
If the function has return type
void, it can only be used syntactically
as an entire statement (with a semicolon added). After the function
call completes, execution continues with the next statement.
If there is a non-void return type, then the function call is syntactically an expression in the statement where is appears. The execution of such a function must reach a return statement. The value of the function-call expression is the value of the expression in this return statement. [Returned Function Values]
A function with a return value can also legally be used as a whole statement. In this case the return value is lost. Though legal, this is often an error! [Not using Return Values]
A variable declared inside a function definition is called a local variable. This declaration may be in either the formal parameter list or in the body of the function. [Local Scope]
A local variable comes into existence after the function is called, and ceases to exist after that function call terminates. A local variable is invisible to the rest of the program. Its scope is just within that function. Its lifetime is just through a single function call. Its value may be transferred outside of the function scope by standard means, principally:
staticthat appears inside a class and outside of any function definition in the class. Static variables are visible within each function of the class, and may be used by the functions. [Static Variables]