Simple Function Parameter question...

brian via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Oct 4 06:05:00 PDT 2016


Howdy folks

This might be a really stupid question, but ya know, if you don't 
ask ...

So, anytime I am calling a function, I have to include everything 
that the function needs all the time. My simplistic example is:

>#!/usr/bin/rdmd
>import std.stdio;
>
>void test(string firstinput, string secondinput)
>{
>    if(secondinput=="world")
>        printoutput(firstinput, secondinput);
>}
>
>void printoutput(string thisIsJustGreeting, string secondinput)
>{
>    writeln(thisIsJustGreeting, " ", secondinput);
>}
>
>void main()
>{
>    string greeting = "hello";  // I really don't want to bring 
> this through every function
>    string thisthing = "world";
>    test(greeting, thisthing);
>}

For this, I don't really want to keep bringing "greeting" around 
with me. Now, I know if I call `printoutput` from somewhere where 
that variable hasn't been declared it'll go nuts, but at the 
moment my code is ugly because I have to keep carrying variables 
around everywhere ...

But when I have a whole heap of things which are quasi-global I 
don't want to keep having to include the same things over and 
over again, especially functions within functions. For a tedious 
example:

Maybe my program design just needs rethinking (I'm not from a CS 
background, so I struggle with the easy stuff sometimes), but a 
simple/better way of doing this would really help. :)

TIA


More information about the Digitalmars-d-learn mailing list