Forward referencing functions in D

wilcro wmcr at mail.com
Fri Oct 16 20:47:56 UTC 2020


On Friday, 16 October 2020 at 19:55:53 UTC, wilcro wrote:
> The web page "Programming in D for C Programmers" 
> (https://dlang.org/articles/ctod.html#forwardfunc) states that 
> forward declarations are neither required nor permitted, and 
> that the following construct is allowable:
>
> void myfunc()
> {
>     forwardfunc();
> }
>
> void forwardfunc()
> {
>     ... //do stuff
> }
>
>
> However, the following code will cause a compiler error:
>
> import std.stdio: writeln;
>
> void main()
> {
>
>     void myfunc() {
>
>         forwardfunc(); // onlineapp.d(8): Error: undefined 
> identifier forwardfunc
>     }
>
>     void forwardfunc() {
>
>         writeln("foo");
>     }
>
>     myfunc();
>
> }
>
>
> Evidently, I am misunderstanding something very elemental here; 
> thanks for any enlightenment regarding this.



Thanks to all for your responses; as a related followup question, 
would there be any reason to avoid placing the majority of code 
for a program outside of the main function?


More information about the Digitalmars-d-learn mailing list