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?

Second int is the type of the first (and only) parameter. Functions and delegates may or may not have parameters, as you probably already know.


Another D example that tries to demonstrate the difference between the two:

```d
import std.stdio;

int times2(int arg) {
  return arg * 2;
}

// function that takes an integer and returns an integer
alias FunCalc = int function(int);

// delegate that takes an integer and returns an integer
alias DelCalc = int delegate(int);

void main() {
  int foo = 2;
  int fooPlus(int arg) {
    return foo + arg;
  }

  // Works because times2 is a regular function
  FunCalc fn = &times2;
  writeln(fn(10));  // Output: 20

  // Works because fooPlus is a nested function. Just like
  // lambdas and methods they have context pointer. fooPlus
// uses the "foo" variable from the local context (scope in this case).
  DelCalc dg = &fooPlus;
  writeln(dg(10));  // Output: 12
  foo = 5;
  writeln(dg(10));  // Output: 15

// Following will not be allowed as compiler detects that fooPlus
  // is actually a delegate (nested function) :
  // FunCalc funNotWorking = &fooPlus;
}
```

Reply via email to