In the following MCQ I found the "a", "b", and "d" all correct. What do you say? Do I have it correct? Please let me know. Thanks.
Inside a function body, we can access
a: variable declared in arguments of this function
b: variable declared in body of this function
c: variable declared inside main( )
d: variable declared before this function and outside any function
8 replies to this topic
#1
Posted 03 June 2011 - 03:24 PM
I'm an outright beginner, learning C++. Using Win XP Pro and Code::Blocks. Be nice to me, please.:)
|
|
|
#2
Posted 03 June 2011 - 07:06 PM
#3
Posted 04 June 2011 - 03:31 AM
Thank you for the agreement, WingedPanther.:)
I'm an outright beginner, learning C++. Using Win XP Pro and Code::Blocks. Be nice to me, please.:)
#4
Posted 04 June 2011 - 11:13 AM
For the sake of mcq since it explains the context, the answer is right. However, i wanted to emphasize the order of variable creation and usage.
Consider following code which is a syntax error for first access of variable a.
C++ allows declaring variables any where. So a variable is only accessible after the statement of it's creation and not before.
That was the reason ANSI c only allows creation of all variables before any executable statement i.e. if you put an int a in the middle of a function, it will be a syntax error.
Consider following code which is a syntax error for first access of variable a.
void fun()
{
cout << "inside fun" << endl;
cout << a << endl;
int a=10;
cout << "Second " << a << endl;
}
C++ allows declaring variables any where. So a variable is only accessible after the statement of it's creation and not before.
That was the reason ANSI c only allows creation of all variables before any executable statement i.e. if you put an int a in the middle of a function, it will be a syntax error.
#5
Posted 04 June 2011 - 11:47 AM
If you say that 'd' is also correct -- then why discriminating 'c'?
In 'd' -- variables declared outside any function are called global variables which are often accessbile anywhere in the program. While accessing the variable declared before this function means somewhere else in the program. Which means there is similarity between 'c' and 'd' -- we can access variables declared in main() in any function by passing their addresses.
Hope im correct and that helped.
In 'd' -- variables declared outside any function are called global variables which are often accessbile anywhere in the program. While accessing the variable declared before this function means somewhere else in the program. Which means there is similarity between 'c' and 'd' -- we can access variables declared in main() in any function by passing their addresses.
Hope im correct and that helped.
#6
Posted 04 June 2011 - 11:53 AM
Your answer is in your own statement.
C refers to variables declared in main which is NOT outside of any function and main's variables are not directly accessible inside a function unless you pass their addresses.
D is referring only to global variables which cant be in main or any other function
C refers to variables declared in main which is NOT outside of any function and main's variables are not directly accessible inside a function unless you pass their addresses.
D is referring only to global variables which cant be in main or any other function
#7
Posted 04 June 2011 - 12:50 PM
jackson6612 said:
Inside a function body, we can access
It's upto you. a,b,c or d.
#8
Posted 04 June 2011 - 02:57 PM
In function foo(), you cannot access variables that are local to main() unless a reference is passed to foo().
#9
Posted 05 June 2011 - 06:14 AM
c answer is somewhat tricky the way I see it. main after all is just a function and we can access them inside main (= inside function) like the question states.
A conclusion is where you got tired of thinking.
#define class struct // All is public.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









