Computer Science, asked by vr14452, 8 months ago


What is the difference between local and global variables? Explain giving example.​

Answers

Answered by devroy26780
21

Answer:

ur ans is

Explanation:

Global variables are declared outside any function, and they can be accessed (used) on any function in the program. Local variables are declared inside a function, and can be used only inside that function. It is possible to have local variables with the same name in different functions

i think it will help u ok

follow me and mark me as brainlist

Answered by aayu37
1

Answer:

Global variables are declared outside any function, and they can be accessed (used) on any function in the program. Local variables are declared inside a function, and can be used only inside that function. It is possible to have local variables with the same name in different functions.

For example: for(int i=0;i<=5;i++){……} In above example int i=0 is a local variable declaration. Its scope is only limited to the for loop.

Example:

int a =4;

int b=5;

public int add(){

return a+b;

}

Here, 'a' and 'b' are global variables.

Similar questions