Scope question

If I had a variable int traps[] defined in main(), passed by argument to another function, lets say play(traps) and then a separate call to another function layTraps() within the play() function - would I have to pass the traps variable via argument to layTraps() from play() or since the function layTraps() is within the function play() who already has access to traps[] variable, then by inheritance, layTraps() would have access to it??
Last edited on
would I have to pass the traps variable via argument to layTraps()

Yes you need to pass the variable to the layTraps() function. Functions don't "inherit".
Calling functions has absolutely nothing to do with inheritance, so any function you call will need variables it needs passed to it. It doesn't seem you are using classes at all, so layTraps() gains the variables of play() no more than play() gains the variables of main(); that is, not at all.
Seems like I should be using classes... thanks!
Function layTraps() is not within function play(). It is called within play() but defined in some namespace or class. You may not define a function within another function in C/C++.
Ahhhhh I see, its not a function within a function, just a call to a function within another function and that's why the variables scope is not in range. Got it
rcast, where's this list of programs you're going to be making? I'm bored! lol

Never mind, just realised you're using the one from this site!
Last edited on
Topic archived. No new replies allowed.