Jump to content

Student trying to grasp function scope issue


Recommended Posts

:hello: Hi

I've recently picked up my programming studies after having laid them aside for a several years, so I'm still a beginner. I recently purchased and have enjoyed working through a book/CD combo by Dietel + Dietel on C++ and I also have copy of a lite version of VisualC++ v6.

I have also been going through some old course materials and plan to get back to creating simple console programs for Windows 3.1 on my old PC using my old Borland TurboC++ 4.5 compiler for fun, and as a sort of a basic concept "lab class", which brings me to this:

:} Lately, I've been struggling with an issue with regard to functions... I came upon an old test question in my old course materials that frankly, has me stumped. Maybe it's because it was really a trick question. I've read and re-read my old text book and other more recent C++ books, I've searched this web forum and others, I've looked for C++ tutorials that would clarify my thinking on functions, but I've not been able to understand what the output, if any, of the code example would be. I've recreated the code below (it only vaguely resembles the original).

#include <iostream.h>
void beezer(int b, int z) {
b = 3;
z = z + b;
cout << "beezer: b " << b << endl;
cout << "beezer: z " << z << endl;
}
int main( ) {
int b = 9;
int z = 18;
beezer(b, z);

cout << "main: b + " << b << endl;
cout << "main: z + " << z << endl;
}

Me, to avoid confusion, I wouldn't have used the same identifiers in both the "beezer" function and the main. And I don't think I quite understand how z can be initialized with z + b when z hasn't been initialized. Unless you can use the values in main...OK, I MIGHT understand, but which "b" do you use?? Anyway, maybe the point was that C++ could, in fact, run the code, and if I understood functions and calls properly, I'd know what the output would be anyway...even with the code written as it is. Bottom line: what would the output of this code be, if any?

I have guessed such things as : 3, 3 AND 9, 18 ...but I don't know.

Would you consider offering a helping hand when you find a minute? I feel as though I understand the basics of functions when clearly written, but when it comes to this particular code, I can't figure out how in the world it would ever work! I feel that knowing the answer to this might give me a deeper understanding of the basics of functions and calls and I certainly don't want to stumble ahead without having a clear understanding and good foundation of the LAWS of the function and the call. Can you help? :whistle:

Link to comment
Share on other sites


Why don't you just compile it and run?

beezer: b 3
beezer: z 21
main: b + 9
main: z + 18

These are called "local variables", they were designed to allow "recycling" of identifier names within functions, but I also find that it makes it more difficult to comprehend the program as a whole.

In beezer() z is the name of one of the parameters, so it becomes initialised with whatever it was called with. In main(), b and z are as they are initialised prior to the call to beezer().

I personally wouldn't write code like this either, even though the language allows it.

Link to comment
Share on other sites

LLXX:

:D First of all, thank you very, very much!

I sincerely appreciate your very prompt and clear explanation!

You asked, "Why not just compile and run it?"

A fair question. There are some practical reasons* at the moment, but beside those, I really did want to try to comprehend the concept without compiling.

*Practical Reasons:

I'm so new to VisualC++ that I have not yet learned to use it properly. I tried writing and running some simple code on VC++, but couldn't get it to compile as yet. Back to the instruction manual. VC++ is more complex than my old Borland TurboC++ upon which I could enter all sorts of code experiments for proof quite easily. And it so happens that I haven't set up the old machine yet with the compiler with which I was more familiar. I've been busy in the text books mostly and reviewing old notes and exercises on paper. Those issues are soon to be rectified...

I just wanted to get this theoretical function concept down in the mean time.

It was bugging me. Thanks for exterminating my bug. B)

Link to comment
Share on other sites

phkninja:

I should stay clear of cute (and somewhat mis-applied) idioms.

There was no actual bug... the problem was "bugging" me and since that was resolved, I felt that, in a way... oh, never mind. I apologize for the obtuse malapropism.

Further, I apologize for a typographical error (otherwise known as a "typo") in my code...actually, what I should have written was not

cout << "main: b + " << b << endl;
cout << "main: z + " << z << endl;

but rather:

cout << "main: b = " << b << endl;
cout << "main: z = " << z << endl;

It didn't seem to get in the way of the understanding I was seeking on function scope, so I didn't say anything about it after I noticed it...until now.

As to your different outcome... I'll ponder that a bit. As soon as I get my compilers back up and running, I'll give it my own test.

Thank you very much for your genuine and helpful interest. I can see I'm in good company. It spurs me on to do my best.

Link to comment
Share on other sites

LLXX has explained it very well.

May i just add, the current program has an output of

beezer: b 3

beezer: z 21

main: b + 3

main: z + 18

Is the "bug" supposed to be in it or what???

Actually, in main() b should have a value of 9... you do see b=9 ?

The b in beezer() is local to it, doing anything with that does not affect the one in main().

Link to comment
Share on other sites

See even i make mistakes. LLXX is right

should be (with your adjusted code)

beezer: b 3

beezer: z 21

main: b = 9

main: z = 18

The question i was asking was it supposed to do that

Passing b in beezer(b,z) has no use, b is overwritten by b=3 in the next line of code.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...