-moz-user-select:none; -webkit-user-select:none; -khtml-user-select:none; -ms-user-select:none; user-select:none;

Wednesday, 13 May 2015

Local and global variables

Today I am here with some more concepts related to functions.

Function call

When you want to call a person, you normally call him by his name. In the same way, when we want to call or use a function in a program, we call it by its name.

Actually function call is a statement that activates a function.Whenever we call a function by its name, the control moves to the function that is called,the statements in the function body are executed and then control returns back to the calling function.

Local variable 

A variable declared inside a function is known as local variable. The scope of local variable is just inside a function. It means it cannot be used outside that function.Local variable is also known as automatic variable.Remember that the lifetime of a local variable is limited.

Global variable

A variable declared outside a function is known as global variable. The scope of global variable is inside the function as well as outside the function. It means it can be used everywhere in the program.Remember that global variables are declared before main function.The lifetime of a global variable is not limited.

FAQ's

Q. What is the difference between local variable and global varible in C++?

A. Local variables:

                                   Local variables in main are only visible within main.These variables only exist inside the specific function that creates them. They are unknown to other functions and to the main program. As such, they are normally implemented using a stack.

Global variables:

                    A global variable may be accessed anywhere.It has a scope of the whole program, it is accessable from any function/module.That is, a global variable is available for use throughout your entire program after its declaration.

Q. What is the difference between "call by value" and "call by reference" in C++?

A. The basic difference is that when you pass a parameter by value, the function receives only a copy of the original object, so it can't do anything to affect the original object. With pass by reference, it gets a reference to the original object, so it has access to the original object, not a copy of it -- unless it's a const reference, it can modify the original object.(reference taken from stackoverflow.com)

Also,in pass by reference, we use "&" symbol with the variable while in pass by value,we do not use this symbol.

No comments:

Post a Comment