FUNCTION_EXISTS

The FUNCTION_EXISTS built-in function is used to check if a function exists on a particular device that can be used by the CALL function.

When the device and function parameters of a CALL are constant and the function does not exist, it is a compile time error. If either parameter is non-constant an error will not be found until they are used at runtime, resulting in a runtime error and test failure. Using FUNCTION_EXISTS first allows the user to handle this case at runtime and do something else instead.

Syntax

FUNCTION_EXISTS( STRING deviceReference, STRING functionName )

Parameters

deviceReference
A test device reference in the form 'board.device'. The board name is optional, but the reference must uniquely identify the test device.
functionName
The name of the test device function to check.

Return value

TRUE if the functionName exists and is callable for the deviceReference. Otherwise it will return FALSE.

Following are the reasons it may return FALSE:

  • The deviceReference test device cannot be found.
  • The deviceReference test device is ambiguous. The device name exists on multiple boards so the reference has to be of the form 'board.device'.
  • The functionName cannot be found on the test device. Either the wrong device is referenced, the function name is incorrect or the scope of the function is not GLOBAL.

Note that this function does not extend to checking input or output parameter types, which would still fail a CALL at runtime if the wrong parameters are used.

GLOBAL TurnOnLEDs()(INT result)
  STRING[] LEDs := {"XJDemo.D1", "XJDemo.D5", "XJDemo.D10", "XJDemo.D15"};
  STRING LedFunctionName := "SetLED";
  
  FOREACH STRING LED IN LEDs
    IF FUNCTION_EXISTS(LED, LedFunctionName) = TRUE THEN
      PRINT("Turning on LED '", LED, "'\n");
      CALL(LED, LedFunctionName)(0);
    END;
  END;
END;
[On XJDemo v4 where only D1 to D9 exist]
Turning on LED 'XJDemo.D1'
Turning on LED 'XJDemo.D5'

See also