Explain function syntax
Luna
luna at foxgirls.gay
Thu Sep 18 16:19:00 UTC 2025
On Wednesday, 17 September 2025 at 22:16:50 UTC, Brother Bill
wrote:
> In the following from Programming in D, page 483, we use
> function keyword.
>
> I expect that the first int in
> ```
> int function(int);
> ```
>
> represents the return value.
>
> What does the second (int) refer to?
>
> Then there is another example with 'delegate' instead of
> 'function.
>
> ```
> import std.stdio;
>
> void main()
> {
>
> }
>
> alias Calculator = int function(int);
>
> Calculator makeCalculator()
> {
> int increment = 10;
> return value => increment + value; // ← compilation ERROR
> }
>
> ```
To give a proper reply, `function` is the a function pointer
type, in D function pointers are strongly typed using this
keyword. `delegate` is a function pointer but with extra data
along with it, to eg. a class function would be a delegate due to
the hidden `this` pointer that tags along.
in this case `int function(int)` means "A function which takes an
integer argument, and returns an integer."
More information about the Digitalmars-d-learn
mailing list