my assignment is to figure out the output but im bot sure i got it right here is the Q


A variable x globally defined in your program is assigned the integer value 3. A variable x defined in a function named f_name is assigned the integer value 5. Answer the following after looking at the code that follows.

1 #include <iostream>
2 using namespace std;
3 int f_name(int y);
4
5 int x = 3;
6
7 int main()
8 {
9 cout << x;
10 cout << f_name(x);
11 return 0;
12 }
13
14 int f_name(int y)
15 {
16 int x = 5;
17 return (x + y);
18 }

What is the output of line 9? _________ line 10? _______________

would the answers be line 9: 4,,, and line 10: 6?

Recommended Answers

All 15 Replies

There's a fantastic way to check the output of any source code ;)

Please explain your rationale for your answers, they are not correct.

I believe he is suggesting compiling the program, checking the output and then now that you know the answers figuring out how they came to be so. If i'm wrong jonsca please correct me (don't want to put words in your mouth).:)

[Opinion]
Yes, as it stands it sounds like the OP wants a quick answer. If you have a means to check, check it. Then try to figure out why the answer is right or wrong. Then post.
[/Opinion]

so if my conclusion is wrong how would i come to the correct answer? what and where do i plug things in to discover it? im really confused in what your saying, how do i put the output in to see if its right or wrong?

what and where do i plug things in to discover it?

generally speaking, the computer and the socket in the wall.

generally speaking, the computer and the socket in the wall.

ha ha... very cleaver ;P

ha ha... very cleaver ;P

Would that mean extremely like a large knife?

P.S I can keep this up all day.

Ok, OP. Now, what was your rationale for your answer?

line 9 = 3
line 10 = 8

commented: Don't post your stuff on a bunch of message boards (e.g., SO) and waste the time of many people -1

Yes, that's correct. Can you explain to yourself (and future generations) why?

commented: :) +1

@jonsca
Are you the most patient man on the planet?

Nah, I'm kicking over tables and smashing bottles over this thread. ;)

@jonsca Thnx for being patient :)
@frogboy77 u werent very helpfull soo boo u lol

this is what i got

At line 9, just printing the value of global variable x. At line 10, just passing the value of x to f_name(iny y). Which means value of y at this function scope is 3. Adding this to local variable x gives 8 which the function is returning.

I'm feeling pretty bad about the "boo u", but you starting to try to do your own work is rewarding.
You should probably down my rep. It would have been much easier if i had gave you the answer.

Adding this to local variable x

Right, because the variable at the local scope with the same name as the global one essentially "blocks" the global.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.