My function uses return to send back a value. How do I fix it?
← Getting Started · Ref: Q1032
EK9 has no return statement. Declare the return value with <- at the top of the function, then assign to it.
BEFORE (wrong):
calculateArea()
-> radius as Float
return 3.14159 * radius * radius
AFTER (fixed):
calculateArea() as pure -> radius as Float <- rtn as Float: 3.14159 * radius * radius
The <- rtn as Float line declares a variable called rtn that IS the return value. You can assign to it with := later in the function if needed:
findLabel()
-> code as Integer
<- rtn as String: "unknown"
switch code case 1 rtn := "active" case 2 rtn := "inactive"
Every code path must initialise the return value. The compiler enforces this — there is no way to forget.
Example
defines module qa.gettingstarted.fixreturn defines function calculateArea() as pure -> radius as Float <- rtn as Float: 3.14159 * radius * radius findLabel() as pure -> code as Integer <- rtn as String: "unknown" switch code case 1 rtn := "active" case 2 rtn := "inactive" default rtn := "unknown" defines program FixReturnDemo() stdout <- Stdout() area <- calculateArea(5.0) stdout.println(`Area: ${area}`) label <- findLabel(1) stdout.println(`Label: ${label}`)
Common mistakes
E01072 — 'return' does not exist in EK9. Declare the return value with <- at the function top, then assign to it.
Incorrect:
return 3.14159 * radius * radius
Correct:
<- rtn as Float: 3.14159 * radius * radius
Other ways to ask this
- Quick fix: replace return statement in EK9
- EK9 has no return keyword. What do I use instead?
- How do I return a value from an EK9 function?
Coming from another language?
EK9 uses declared return values instead of return statements. The compiler ensures all paths assign a value.
Keywords: declare, function, quick, fix, return