User Tools

Site Tools


programming-languages:mql:func-overload

Function Overloading

Function overloading is a process of creating several functions with the same name, but different parameters.

In overloaded variants of a function, the number of arguments or their type must be different. A specific function variant is selected based on the correspondence of the list of arguments when calling the function, to the list of parameters in the function declaration.

  1. void OnStart() {
  2. string a[] = {"one", "1st", "two", "2nd"};
  3. string b[][2] = {{"one", "1st"}, {"two", "2nd"}};
  4.  
  5. Print(array_string_search(a, "two")); // 2
  6. Print(array_string_search(b, "two")); // 1
  7.  
  8. }
  9.  
  10. //+------------------------------------------------------------------+
  11. //| Search for a value in an array of strings. |
  12. //| @param &arr: array of strings |
  13. //| @param str: the value to look for |
  14. //| @return idx: the position of the value in the array |
  15. //+------------------------------------------------------------------+
  16. int array_string_search(string &arr[], string str) {
  17. int idx = -1;
  18.  
  19. for (int i = 0; i < ArraySize(arr); i++) {
  20. if (str == arr[i]) {
  21. idx = i;
  22. break;
  23. }
  24. }
  25.  
  26. return(idx);
  27. }
  28. int array_string_search(string &arr[][2], string str) {
  29. int idx = -1;
  30.  
  31. for (int i = 0; i < ArrayRange(arr, 0); i++) {
  32. if (str == arr[i][0]) {
  33. idx = i;
  34. break;
  35. }
  36. }
  37.  
  38. return(idx);
  39. }
programming-languages/mql/func-overload.txt · Last modified: 2024/01/17 10:31 by tormec