Back to Defining Functions

Functions · Defining Functions · Practice Question

Implicit vs Explicit Arguments

Sign in to track your progress

Question

Rewrite the following function using explicit named parameters instead of implicit x, y, z, then explain when you would choose each style.

f:{(x - y) % z}

Solution

pnlPct:{[entryPx; exitPx; notional]
  (exitPx - entryPx) % notional
 }

pnlPct[100.0; 105.0; 100.0]
/ 0.05

Explanation

The implicit form {(x - y) % z} works but gives the reader no hint about what the three arguments represent. Explicit names (entryPx, exitPx, notional) make the intent immediately clear. (Avoid names that are q keywords — exit, count, last, value and so on — as parameter or variable names.)

When to use implicit x, y, z:

  • The function is very short (one expression) and the arguments are obvious from context
  • Used inline as a lambda passed directly to an iterator
/ Clear enough as implicit
{x * x} each 1 2 3 4 5
(+/) each (1 2 3; 4 5 6)

When to use explicit names:

  • The function body is more than one line
  • The arguments have domain-specific meaning that a name communicates
  • The function is called from multiple places in the codebase
/ Explicit names are worth it here
calculateVwap:{[price; qty]
  notional: sum price * qty;
  notional % sum qty
 }

One firm rule: never use x, y, or z as local variable names inside a function. They are reserved for arguments — reusing them as locals creates confusion about whether they still hold the original argument value at that point in the function.