5. Functions

Before a function can be called it has to be defined. A function definition consists of following parts:

  • return type
  • function name
  • list of parameters passed to the function
  • function body

The syntax is:

returnType functionName(param1Type param1Name, param2Type param2Name,...){ functionBody }

returnType: The method's return type, i.e. the data type of the value, that the function returns. This can be void, then the function returns no value.

functionName: The function's name. Note that also type names are allowed as function names, but only if the function takes at least one parameter (if it takes no parameter, a type name followed by () is reserved for the constructors like array()). This can be used to write parameterized constructors for your struct types. Example for Point2D:

typedef Point2D struct{ int x; int y; } Point2D Point2d(int x, int y){ //Parameterized constructor for Point2D Point2D result = Point2D(); result.x = x; result.y = y; return result; }

In GSL two functions can have the same name if they have a different signature, i.e. the types (not the names!) of their parameters have to differ. This is called overloading. We can use overloading for example to define functions for different data types, but with the same name (see example below).

list of parameters: The parameters that are passed to this function. Each parameter must first have its data type declared, followed by the name of the parameter. Parameters are seperated by a comma. The function may also take no parameters, then you can only write ().

Function Body: These are just arbitrary statements that define the function's behaviour. The statement "return RETURNVALUE;" may be used to leave the function, returning the value <RETURNVALUE> (which must be of the type <returnType>). If the returnType is void, then you can use "return;" to return without a value. The function MUST return a value if it isn't declared void. If it is declared void and no return is executed until the function body's end is reached, the function returns implicitly.

Examples: Function with no parameters and no return value:

void printLol(){ echo("lol"); return; //May be omitted, since return type is void }

Integer, float addition as a function

int add(int i, int j){ //signature: (int,int) return i + j; //May not be omitted, since the function must return an int } float add(float i, float j){ //Overloaded function, same name, but different signature: (float,float) return i + j; }

Functions are called by their name, followed with parameters handed to the function, surrounded by brackets, seperated by commas:

funcName(param1,param2,...)
Such a call is an expression that evaluates to the value, that the function returns. So it can either be used in a more complex expression or be terminated with a comma and become a statement. No special keyword like "call" is needed then. Functions that return void can only be used as a statement.

Function parameter types have to match exactly the ones in the declaration for type safety purposes. So, no implicit casting is done here, you have to cast all parameters explicitly to the desired type.

Examples:

echo("abc"); //Calling the echo function with parameter "abc"

echo((string)add(5,3)); //Using the add function inside the echo function. Have to cast to string, since no implicit casting is done on function parameters