Lipi

Return Statement

The firta statement is used to return a value from a function.

Syntax

firta expression;

Basic Return

A simple function that returns the sum of two numbers:

kaam purna add(purna a, purna b) {
  firta a + b;
}

purna result;
result = add(5, 3);  :) result = 8
bhana(result);

Conditional Return

A function that returns different values based on a condition:

kaam purna max(purna a, purna b) {
  yadi(a > b) {
    firta a;
  }anyatha {
    firta b;
  }
}

purna result;
result = max(10, 5);  :) result = 10
bhana("The maximum is: ");
bhana(result);

Early Return

A function that returns early in certain cases:

kaam purna factorial(purna n) {
  yadi(n < 0) {
    bhana("Error: Factorial is not defined for negative numbers");
    firta -1;  :) Early return for error case
  }
  
  yadi(n <= 1) {
    firta 1;
  }
  
  firta n * factorial(n - 1);
}

Important Notes

  • The firta statement immediately exits the function and returns the specified value.
  • The type of the returned value must match the return type specified in the function definition.
  • A function with a return type of khali should not use the firta statement with a value.
  • If a function has a return type other than khali, it must have at least one firta statement.

Next Steps

Now that you understand return statements in Lipi, you can learn more about: