What are local variables and gloabal variable | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What are local variables and gloabal variable

21st Mar 2018, 2:11 PM
Perrin Ayabara
1 Answer
+ 1
Local variables are declared in a particular scope. This means they can only be accessed within the particular function, or if statement, or while loop, etc. Here 'sum' is an example of a local variable. int add (int a, int b) { int sum = a + b; return sum; } Global variables are declared outside all functions and statements, and can be accessed anywhere. Here 'x' is a global variable: #include <iostream> using namespace std; int x = 15; int main() { return 0; } Hope this helps :)
21st Mar 2018, 2:26 PM
Just A Rather Ridiculously Long Username