C# Programming Overview continued – Functions

Functions are at the core of the C# programming language, they define jobs to do in code, setting out a group of instructions to perform an action or resolve some calculation based a set of input.  Functions of course do a lot more as you will see here.


1. Definition

The definition of a function includes the return type, the function name, the parameters’ list, and the function body.

The function body is enclosed between an opening and a closing brace. Example:

The parameters a and b receive the values of the arguments when the function is called. Example:


2. Scope

The code found in a function is private to the function and cannot be accessed by any statement from another function. Function code is accessed only through function call.

A C# program starts from the function Main. All functions have a file scope. Parameters and variables declared inside the function have function scope. They are created when the function is entered and destroyed when the function ends. Static variables declared inside the function have a function scope, but they retain their values between function calls.


3. Calling a Function

A function is called by using its name followed by an opening and closing parenthesis including the arguments, if any. If the function has no arguments, then the argument list is left empty. When a function is called, the function parameters receive the value of the arguments by respecting their order. Parameter1 receives the value of argument1, and ParameterN receives the value of argumentN.

When a function is called, the sequential execution flow is interrupted, and the program counter jumps to the first statement of the function called. Once the last statement is executed, or a return instruction is executed, the sequential execution flow is interrupted and the program counter is updated so it points to the next instruction after the function call. The return instruction or statement also returns a value since the function has a returning type. In other words, the function could be used as value specified by its type.


4. “return” Statement Example

A return statement stops the operation of the function and sends back the current return value of the function at that time. 

It is important to note that if a return is specified inside a loop contained in a function (this can also cause errors), then the function will stop, return the value and not perform any of the remaining code in the function.  It is better (as shown below) for the return to be called at the conclusion of an opperation. 

E.G. if x then return x else return y.

returns can also only be a single type, class or struct.

Example:


5. Call by Value Function Arguments

Arguments can be passed in two ways: Call by value and call by reference. The call by value method copies the value of the argument into the function parameter. Therefore, changes made to the parameter have no effect on the argument. In other words, the function code cannot modify the arguments used to call the function.

Example:


6. Call by Reference Function Arguments

To pass a parameter by reference, use the ref or out keyword. A variable of a reference type does not contain its data directly; it contains a reference to its data. When you pass a reference-type parameter by value, it is possible to change the data pointed to by the reference, such as the value of a class member. However, you cannot change the value of the reference itself; that is, you cannot use the same reference to allocate memory for a new class and have it persist outside the block. To do that, pass the parameter using the ref (or out) keyword.

Example:


%d bloggers like this: