User Defined Functions

A function may be defined using syntax such as the following:

  complex foo(complex arg_1,complex arg_2, complex arg_3)  {  complex sum;      sum=arg_1+arg_2+arg_3;    return(sum);  }  

Any valid code may appear inside a function. At the beginning of a function you can declare local variables, which are only valid inside the function and are destroyed when the function exits. After the variable declarations you can write program code. Function names are case sensitive (just like variables).


Function Declaration

In ChaosPro, functions must be declared before they can be used, just as in C. By defining a function (i.e. writing down a complete function including the function code) it implicitely gets declared, too. But sometimes you may want to use a function before defining it. In that case you must tell ChaosPro something about the function you plan to use. You do this by declaring it:

  // Let's declare it, so we can use it. It will be defined later  complex foo(complex arg_1,complex arg_2, complex arg_3);     complex useTheFunction(void)  {  complex myVar;      // Ok, let's use the function foo, which has been declared.    // Note that currently ChaosPro's compiler does not know what this function actually does    myVar=foo(z,z,z);    return(myVar);  }    // Ok, now finally we need to define the function. It has already been declared, but the function code  // is missing.  complex foo(complex arg_1,complex arg_2, complex arg_3)  {  complex sum;      sum=arg_1+arg_2+arg_3;    return(sum);  }  

As you can see, declaring a function is simple: You only need to place the first function line containing the return type, the function name and its parameters before the function which wants to use it. Terminate the function declaration with a semicolon “;”.
Now ChaosPro knows that there will be a function which accepts three complex parameters and returns a complex parameter. ChaosPro can create the complete code to call the function, it can decide what type conversions need to be done (if any), and it knows that a complex number will be returned from the function. The only thing which ChaosPro does not know is, where to find the function: No function code has been defined. But this can be adjusted later…
At the end of the compilation process ChaosPro also knows where to find the function “foo”: By a process called “linking” it fills in the proper calling address to any call to that function.

Function Overloading

ChaosPro supports function overloading: This means that you can write several functions having exactly the same name, differing only in the parameters.

This allows you to write better code: Imagine you want to calculate the average of two numbers, so you write the following function:

  real average(real a,real b)  {    return((a+b)/2);  }  

Ok, now you can call it using x=average(a,b); So far so good. Now imagine you want to calculate the average of three numbers:

  real average(real a,real b,real c)  {    return((a+b+c)/3);  }  

No problem? Indeed, no problem in ChaosPro, but a huge problem in many other programming languages: They complain that there’s already a function called “average”, so you many not reuse the function name.
In ChaosPro you can reuse the function name as long as it has another number of parameters or differs in the parameter types. The reason for this is that each function has an “internal name” consisting of the function name followed by the parameter types. And only this internal name needs to be unique.
So each of the following functions can be defined in the same class:

  real average(real a,real b,real c);  real average(real a,complex b,real c);  real average(real a,complex b,complex c);  real average(real a,complex b);  ...  real average(int a,real b);  

When you use a function you must provide a matching set of parameters. No parameter conversions will take place. And ChaosPro will look for and use the function which matches your parameter set, i.e. it will use the function which needs exactly the parameters (number of parameters and types) which you provide.

In contrast to some other programming languages ChaosPro does not support variable numbers of arguments to functions. But a similar effect can be achieved by passing an array to a function.
ChaosPro also does not support default arguments to functions.
On the other hand ChaosPro supports any (fixed) number of parameters to functions, parameters can be call-by-value or call-by-reference (see Function arguments).

Recursive Function Calls

Of course you can recursively call functions. Here’s an example, which shows clearly the mechanism of recursive algorithms:

  int factorial(int i)  {          if (i<=1)          {                  return(1);          }          else          {                  return(i*factorial(i-1));          }  }  

It returns i! (supposing i is a valid positive integer greater than 0).

User Defined Functions