Function Call

Overview

  • A name followed by a left parenthesis ( in an expression is recognized as a function name.
  • Function calls are expressions whose value is the function's return value.
  • The function's declared return type must match the actual return value.
  • Functions can be declared or described in the global scope (outside other functions) but not inside other functions.
  • Parameter Passing

  • Arguments are passed by value. Each argument expression is evaluated, and its value is passed to the function.
  • The order of expression calculation and value loading is not guaranteed.
  • The system checks the number and type of arguments during execution.
  • This method of function addressing is called a value call.
  • Default Parameters

  • Functions can have default parameter values.
  • When calling a function with default parameters, the list of passed parameters can be limited.
  • Parameters must be provided sequentially; you cannot skip parameters, even those with default values, before the first default parameter.
  • **Example:**

    void somefunc(double init, double sec=0.0001, int level=10);

    somefunc(3.14); // Correct: Uses default values for sec and level somefunc(3.14, 0.0002); // Correct: Uses default value for level somefunc(3.14, 0.0002, 10); // Correct: All parameters provided // somefunc(); // Wrong: 'init' must be provided // somefunc(3.14, , 10); // Wrong: Cannot skip 'sec'

    Ambiguity and Scope

  • Using multiple functions with the same name from different contexts can cause ambiguity.
  • To resolve ambiguity, explicitly specify the function scope using the scope resolution operation.
  • Related Concepts

  • [Passing Parameters](/basis/function/parameterpass)
  • [Function Overloading](/basis/function/functionoverload)
  • [Operation Overloading](/basis/function/operationoverload)
  • [Description of External Functions](/basis/function/extfunctions)
  • [Exporting Functions](/basis/function/export)
  • [Event Handling Functions](/basis/function/events)
  • [Scope Resolution Operation](/basis/operations/other#context_allow)
  • [Overload](/basis/oop/overload)
  • [Virtual Functions](/basis/oop/virtual)
  • [Polymorphism](/basis/oop/polymorphism)
  • Platform Support

  • MQL4 Language