Basic question about passing and handling global vars to a function

 
Hello,

i've the following basic question.

if i define a array in global scope.

double myglobalarray[];
myglobalarray[0]=987;


Is it possible to pass this array to a function, work, fill and change his values at the end at global scope, without knowing/working with the name of the global var inside the function? For Example

void myFunction(doube myGvar){
        
        //Change something
        myGvar[0]='123';
}


I call the function

myFunction(myglobalarray);

After calling the function in global scope Print(" Now it should be 123:"+myglobalarray[0]);

Is it possible, how? Is there may something special at passing the var to the function ?

 

The fact that it is a globally scoped variable means that it does not need to be passed as a parameter. It can be accessed directly.

// Untested, uncompiled example code — simply typed out

double g_adbPrices[];

int GetPoints( int index ) {
   return (int) round( g_adbPrices[ index ] / _Point );
};
 

Do not post code that will not even compile.

  1.         myGvar[0]='123';

    «'123'» is not a string (e.g. «"123"»). It is not a single charter (short). The array type is a double. So neither will work.

  2. ReLor2: After calling the function in global scope \

    You can not call functions on global scope, call them in OnTick.

 
ReLor2:
Is it possible to pass this array to a function, work, fill and change his values at the end at global scope, without knowing/working with the name of the global var inside the function?

Sure. Pass an array to a function by reference. Read the paragraph "References: Modifier & and Keyword this" from the documentation.

I use this very often.

Example:

double globalArr1[5];
double globalArr2[10];

void OnStart()
  {
   ArrayInitialize(globalArr1, 1.23);
   ArrayInitialize(globalArr2, 2.34);
   Print("first:");
   printLastElement(globalArr1);
   Print("second:");
   printLastElement(globalArr2);
  }

void printLastElement(double &arr[])
  {
   int lastIndex = ArraySize(arr) - 1;
   PrintFormat("Last index %i, value %.2f", lastIndex, arr[lastIndex]);
  }


Documentation on MQL5: Language Basics / Data Types / References: Modifier & and Keyword this
Documentation on MQL5: Language Basics / Data Types / References: Modifier & and Keyword this
  • www.mql5.com
References: Modifier & and Keyword this - Data Types - Language Basics - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Vladislav Boyko #:

Sure. Pass an array to a function by reference. Read the paragraph "References: Modifier & and Keyword this" from the documentation.

I use this very often.

Example:


But in my example, it would be correct to use the const access specifier to explicitly indicate that the array cannot be modified by the function.

void printLastElement(const double &arr[])
  {
   int lastIndex = ArraySize(arr) - 1;
   PrintFormat("Last index %i, value %.2f", lastIndex, arr[lastIndex]);
  }


Documentation on MQL5: Language Basics / Variables
Documentation on MQL5: Language Basics / Variables
  • www.mql5.com
Variables - Language Basics - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Thanks a lot!