You are on page 1of 2

#include<iostream> using namespace std; int y;//global variable //default to 0 void main() {//outer scope //scope - {} { //local variable

- is declared //inside the scope //local variable doesn't have default value int x=4; cout<<x; //variable is defined in the //inner scope }//when you exit scope everything //that was declared inside the scope //is gone //cout<<x;//NOT RIGHT //you can't see variable from the other scope cout<<y;//is OK, y is global } #include<iostream> using namespace std;

int add_number(int a)//whatever you //have inside () is called parameter //or argument of the function { a=a+8; return a; } void main() { int x=5; //if you want to use variable from outside //in the function - you have to pass it //to the function add_number(x);//function accepts int variable cout<<x<<endl;//compiler creates copy of x //after function execution it goes back //to original value x=add_number(x); cout<<x<<endl; }

You might also like