ThomasMertes / seed7

Source code of Seed7
GNU General Public License v2.0
207 stars 11 forks source link

There is a return statement or not? #15

Closed renatoathaydes closed 11 months ago

renatoathaydes commented 1 year ago

From the Introduction page, section 1.3:

There is no return statement. Instead a result variable can be defined to which the result of a function can be assigned.

This seems to be incorrect.

From the Tutorial, section 2.7 (and many others following it):

const func boolean: flipCoin is return rand(FALSE, TRUE);

So, it seems Seed7 has returns, so not-having return is no longer a feature?!

ThomasMertes commented 1 year ago

There is no return statement. Defining a function with

const func boolean: flipCoin is
  return rand(FALSE, TRUE);

is a shortcut for

const func boolean: flipCoin is func
  result
    var boolean: coinState is FALSE;
  begin
    coinState := rand(FALSE, TRUE);
  end func;

The return above is an alternate possibility to define a function. It is not comparable to the return statements of other programming languages.

Leaving a function from the middle of a loop with a return statement is not supported in Seed7.

renatoathaydes commented 1 year ago

Ok, so return may only appear as the last statement of a function? Or only on a single-line function?

ThomasMertes commented 1 year ago

Ok, so return may only appear as the last statement of a function?

No. Actually return is not a statement. Using 'return' as statement triggers an error.

const func boolean: flipCoin is func
  result
    var boolean: coinState is FALSE;
  begin
    return rand(FALSE, TRUE);     # THIS WILL NOT WORK
  end func;

See 'return' as alternate way to define a function. The 'return' construct can be used if the function result can be expressed with one expression. So you can use it as long as you don't need statements.

In detail: The return construct creates a function (like a lambda function without parameters). So 'return' is a function that creates a function. This way 'return' provides an alternate way to define a function body:

const func boolean: flipCoin is
  return rand(FALSE, TRUE);

In the example above

return rand(FALSE, TRUE);

is used to initialize the function with a function value that is created by 'return'.

Hopefully this detailed explanation not too confusing. :-)

renatoathaydes commented 1 year ago

Yes, I get it... but is return defined in s7 itself? Or that's language syntax?

ThomasMertes commented 1 year ago

but is return defined in s7 itself? Or that's language syntax?

Part of return is defined in libraries and part of it is hard-coded in the s7 interpreter.

The syntax of return is defined in syntax.s7i with:

$ syntax expr: .return.()     is <-  30;

Syntax statements use the Structured Syntax Definition of Seed7. How return can be used is defined for every type in seed7_05.s7i with:

const func func aType: return (ref aType param)         is action "PRC_RETURN";
const func func aType: return (ref func aType param)    is action "PRC_RETURN";

These definitions of return are done for every new type that is defined. What return does is defined with action "PRC_RETURN".

The action PRC_RETURN is implemented in the file prclib.c (part of the s7 interpreter) with the C function header:

objectType prc_return (listType arguments)
swork1 commented 1 year ago

@ThomasMertes Why is the syntax of return defined in syntax.s7i library but the definition of PRC_RETURN is hard-coded in the interpreter? I'm sure you have a reason as to why so I'm curious. I would have thought it would either be all defined in a library or all inside the interpreter.

ThomasMertes commented 1 year ago

The action PRC_RETURN is a primitive action and by design all primitive actions are implemented by interpreter and compiler.. This allows that the compiler can generate efficient machine code for actions like PRC_WHILE (a while loop).

How primitive actions are used is defined in libraries. For operators and statements there is a syntax definition that uses the Seed7 Structured Syntax Definition. Which types are used for a primitive action is defined in a semantic definition. This can be seen in the declaration of the while-statement.

ThomasMertes commented 1 year ago

I added an answer the question Is there a return statement? to the FAQ.

Do you think this is sufficient or does it need an improvement?

swork1 commented 1 year ago

I added an answer the question Is there a return statement? to the FAQ.

Do you think this is sufficient or does it need an improvement?

Looks good. Thanks for the response

ThomasMertes commented 11 months ago

I close this issue because the FAQ now answers the question Is there a return statement?