Thread overview
Explain function syntax
1 day ago
Brother Bill
1 day ago
monkyyy
1 day ago
H. S. Teoh
1 day ago
Paul Backus
18 hours ago
kdevel
13 hours ago
Luna
12 hours ago
Dejan Lekic
11 hours ago
Ali Çehreli
23 minutes ago
Steven Schveighoffer
1 day ago

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
}

1 day ago

On Wednesday, 17 September 2025 at 22:16:50 UTC, Brother Bill wrote:

>
import std.stdio;

void main()
{

}

alias Calculator = int function(int);

Calculator makeCalculator()
{
	int increment = 10;
	return value => increment + value; // ← compilation ERROR
}

function is almost always wrong you need to be very pure to work at all and if by chance you ever have a function pointer you can convert it to a delegate

>

What does the second (int) refer to?

it takes an int, just delete the names of the arguments from the header syntax

1 day ago
On Wed, Sep 17, 2025 at 10:16:50PM +0000, Brother Bill via Digitalmars-d-learn 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?
[...]

The type of the first (and in this case, only) parameter.


T

-- 
Being in debt attracts a lot of interest from banks.
1 day ago

On Wednesday, 17 September 2025 at 22:16:50 UTC, Brother Bill wrote:

>

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
}

Nested functions that access local variables from their enclosing function need to take an additional, hidden "context" parameter in order to actually find those variables at runtime. Because of this, they have a different calling convention than "normal" functions. As a consequence, if you take a pointer to one of these functions, you do not get a normal "function pointer" type (which is represented by the function keyword), but a special "delegate" type, which contains both a pointer to the function itself, and a pointer to the context needed to call it successfully.

18 hours ago

On Wednesday, 17 September 2025 at 22:16:50 UTC, Brother Bill wrote:

>

return value => increment + value; // ← compilation ERROR

void main()
{
	auto calc = makeCalculator;
	writeln (calc (1));
}

alias Calculator = int function(int);

Calculator makeCalculator()
{
	enum increment = 10; // must not be a stack var
	return value => increment + value;
}

import std.stdio;

Compiles and prints 11.

13 hours ago

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."

12 hours ago

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:

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;
}
11 hours ago
As stated by multiple people, most nested functions will be 'delegates'. However, a nested function is a 'function' if it does not touch local scope:

void main() {
    int twice(int i) {
        return i * 2;
    }

    // Not a delegate:
    static assert(is (typeof(twice) == function));
}

Ali

23 minutes ago

On Thursday, 18 September 2025 at 18:10:13 UTC, Ali Çehreli wrote:

>

As stated by multiple people, most nested functions will be 'delegates'. However, a nested function is a 'function' if it does not touch local scope:

void main() {
    int twice(int i) {
        return i * 2;
    }

    // Not a delegate:
    static assert(is (typeof(twice) == function));
}

You are mistaking the is expression for a function test with the function pointer type.

In fact, the compiler treats this as a function with a context pointer, because it's not static.

Only function literals can be infer as a function pointer.

Some demo code:

void main() {
    int x;
    int couldBeStatic(int i) {
        return i * 2;
    }
    int cantBeStatic(int i) {
        return i * x;
    }
    static assert(is(typeof(couldBeStatic) == function));
    static assert(is(typeof(cantBeStatic) == function));

    // function pointer is not a function
    static assert(!is(typeof(&couldBeStatic) == function));
    static assert(!is(typeof(&cantBeStatic) == function));

    // this one does not need a context pointer
    static int isStatic(int i) {
        return i * 2;
    }
    static assert(!__traits(compiles, {int function(int) fp = &couldBeStatic;}));
    static assert(!__traits(compiles, {int function(int) fp = &cantBeStatic;}));
    static assert( __traits(compiles, {int function(int) fp = &isStatic;}));

    static assert( __traits(compiles, {int delegate(int) dg = &couldBeStatic;}));
    static assert( __traits(compiles, {int delegate(int) dg = &cantBeStatic;}));
    static assert(!__traits(compiles, {int delegate(int) dg = &isStatic;}));

    // lambdas are more malleable
    static assert(__traits(compiles, {int function(int) fp = (int i) => i * 2;}));
    static assert(__traits(compiles, {int delegate(int) dg = (int i) => i * 2;}));
}

-Steve