Parameters of functions


for example, forex

Formal function parameters

As it was mentioned in the article «How to create your own functions» a function can be defined in the following way:

   type    name ( formal parameters )
{
actions
}

Formal parameters of a function — are values which are passed to the function. Parameters are contained within the parentheses that follow the function name when the function is defined. If there is more than one parameter to be specified then we separate them with commas. For each parameter its type is specified (any of the standard types: arrays, int, bool, datetime, double, color, string).

No more than 64 parameters can be passed to the function.

The scope of formal parameters of a function is the function itself, i.e. they are accessible only from this function.

Parameters can be assigned default values in a function declaration. At that you should bear in mind that once a parameter receives a default value, all remaining parameters to its right must also be assigned default values:

   int GetSomethingUseful ( int a, int b = 0, bool f = true, double r = 5.9)
{
...
}

In this case all or some of the default parameters can be omitted and they will be substituted automatically by the default values :

  • Call of GetSomethingUseful(3, 5, false) will be equivalent to the call of GetSomethingUseful(3, 5, false, 5.9).
  • Call of GetSomethingUseful(3, 5) will be equivalent to the call of GetSomethingUseful(3, 5, true, 5.9).
  • Call of GetSomethingUseful(3) will be equivalent to the call of GetSomethingUseful(3, 0, true, 5.9).

At that you should remember that if we omit a default parameter, all the subsequent parameters mustn’t be indicated as well.

If we import a function from another module then such parameters of such functions can’t have default values.

Everything I spoke about above dealt with passing parameters to a function by value. To make everything clear let’s consider the following example:

  void  MyFirstFunction ()
{
int a = 10;
MySecondFunction ( a );
}

void MySecondFunction ( int b )
{
b = b + 1;
Print ( b );
}

In this example we call MySecondFunction, passing as a parameter the value of variable «a» (in our case — 10). Thus at calling MySecondFunction variable «b» — function formal parameter — will be assigned value 10, which will be increased by one by the next line and will be output to log. But the value of variable a in MyFirstFunction won’t change and will remain equal to 10.

There is another way of passing parameters to a function — by reference (using the ampersand — & after the type definition ). Let’s consider a new example :

  void  MyFirstFunction ()
{
int a = 10;
MySecondFunction ( a );
}

void MySecondFunction ( int& b )
{
b = b + 1;
Print ( b );
}

In this example we call MySecondFunction, passing the reference to the variable «a» as a parameter. Thus in the MySecondFunction function always when the variable «b» is addressed in fact the variable «a» will be addressed. I.e. the value of the variable «a» will be increased by one and will be output to log. On completion of the MySecondFunction function the variable «a» will be equal to 11.

Both variables of standard types (but only within one module) and arrays ( both within one module and in other modules) can be passed as parameters by reference.


Next article: "Operators. Arithmetical operations"

+7 (495) 710-76-76
© 1998—2008 «Alpari»

close

Your Personal Area

For alpari.classic enter your account number (a letter and 4 figures) and the code word for the Personal Area.

For alpari.micro account: enter your login (6 figures) and the password for MT.

Open an account!Forgotten your password?