digitalmars.D.learn - Simple Function Parameter question...
- brian (18/36) Oct 04 2016 Howdy folks
- Adam D. Ruppe (3/3) Oct 04 2016 I'd put your repeated variables together in a struct, then pass
- brian (8/11) Oct 04 2016 That's not a bad idea.
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: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
Oct 04 2016
I'd put your repeated variables together in a struct, then pass it around to the functions. At least then you pass just one param at a time, without losing the info.
Oct 04 2016
On Tuesday, 4 October 2016 at 13:16:35 UTC, Adam D. Ruppe wrote:I'd put your repeated variables together in a struct, then pass it around to the functions. At least then you pass just one param at a time, without losing the info.That's not a bad idea. I have been creating "ids" to point to my structs and passing that around where needed, but in places it's still getting ugly, as they are sometimes more complicated than single vars, but it does the trick. I might just get it to work the ugly way, and try pretty it up later. :P
Oct 04 2016