3.12. Static Function Summary

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.

3.12.1. Function definition

  1. The general syntax for defining a static function is

    static returnTypeOrVoid FunctionName ( formal parameter list )
    statements in the function body...
  2. 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

    type parameterName

  3. If the function is going to be called from outside its class, the heading needs to start with public before the static. [Library Classes]

  4. If returnTypeOrVoid in the heading is not void, there must be a return statement in the function body. A return statement has the form

    return expression ;

    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]

  5. Execution of a program starts at a function with a heading including

    static void Main

    Thus far we have only discussed having an empty parameter list in the heading of the definition of Main, and we defer discussion of Parameters to Main until we have introduced One Dimensional Arrays.

  6. 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 consecutive lines starting with ///. [Function Documentation]

3.12.2. Function Calls

  1. A function call takes the form

    FunctionName ( actual parameter list )

    A function call makes the function definition be executed.

  2. 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]

  3. 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.

  4. 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]

  5. 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]

3.12.3. Scope

  1. 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]

  2. 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:

    • If it is the expression in a return statement, its value is sent back to the caller.
    • It can be passed as an actual parameter to a further function called within its scope.

    [Local Scope]

3.12.4. Static Variables

  1. There may be a declaration prefaced by the word static that 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]
  2. A common use of a static variable is to give a name to a constant value used in multiple functions in the class. [Static Variables]