Jump to content

MCQ: variable declared in arguments of this function

- - - - -

  • Please log in to reply
8 replies to this topic

#1
jackson6612

jackson6612

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 304 posts
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
I'm an outright beginner, learning C++. Using Win XP Pro and Code::Blocks. Be nice to me, please.:)

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
I agree.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
jackson6612

jackson6612

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 304 posts
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
fayyazlodhi

fayyazlodhi

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 403 posts
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.


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
AKMafia001

AKMafia001

    Programmer

  • Members
  • PipPipPipPip
  • 119 posts
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.

#6
fayyazlodhi

fayyazlodhi

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 403 posts
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

#7
AKMafia001

AKMafia001

    Programmer

  • Members
  • PipPipPipPip
  • 119 posts

jackson6612 said:

Inside a function body, we can access

It's upto you. a,b,c or d.

#8
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
In function foo(), you cannot access variables that are local to main() unless a reference is passed to foo().
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#9
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
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