The scope of a variable greatly determines the existence of a particular variable in a function, because there are variables that are only known in a function and are not known in other functions. However, there are also variables that can be accessed by all functions.
1. Automatic Variable
It is a variable that is defined inside a function and acts as a local variable for that function, meaning that the variable is only known inside the function where the variable is defined. The properties of automatic variables are:
- Variables will be created when the variable is called and variables will be lost when the function ends.
- Variables are only known within the function that defines them.
- Initialization by the programmer will be done every time the function is called, for example:
output:
Automatic Variable
Program:
#include <iostream.h>
#include <conio.h>
/**
*bundet.com
*Variabel Otomatis
*/
void coba(); // prototipe fungsi atau pendeklarasian fungsi
void main()
{
cout<<"copyright 2014 www.bundet.com"<<endl<<endl;
int x = 22; //salah satu contoh variabel lokal pada fungsi main()
double y =2.22;
clrscr();
cout<<"copyright 2014 www.bundet.com"<<endl<<endl;
cout<<"Pada fungsi main() : x = " << x << " y = " << y << endl;
coba(); // memanggil fungsi alpha yang telah didefinisikan bagian bawah fungsi main()
cout<<"Pada fungsi main() : x = " << x << " y = " << y << endl;
getch();
}
void coba() //definisi fungsi alpha
{
int x = 20; //variabel lokal pada fungsi coba()
double y = 3.14;
cout<<"Pada fungsi coba() : x = " << x << " y = " << y << endl;
}
2. External Variables
External variables are variables that are defined outside of any function. These variables are also known as global variables, because these variables are known by all functions, for example;
output:
External Variables
Program:
#include <iostream.h>
#include <conio.h>
/**
*bundet.com
*Variabel External
*/
int nilai = 50; // Variabel eksternal
void tambah(); // prototipe fungsi
void main()
{
clrscr();
cout <<"www.bundet.com -> "<<nilai << endl;
tambah();
cout <<"www.bundet.com -> "<<nilai << endl;
tambah();
cout <<"www.bundet.com -> "<<nilai << endl;
getch();
}
void tambah() // Definisi fungsi
{
nilai ++ ; // variabel eksternal dinaikkan
}
Or it could be like this,
Program:
#include <iostream.h>
#include <conio.h>
/**
*bundet.com
*Variabel External dengan Keyword Extern
*/
int nilai = 50; // Variabel eksternal
void tambah(); // prototipe fungsi
void main()
{
clrscr();
cout <<"www.bundet.com -> "<<nilai << endl;
tambah();
cout <<"www.bundet.com -> "<<nilai << endl;
tambah();
cout <<"www.bundet.com -> "<<nilai << endl;
getch();
}
void tambah() // Definisi fungsi
{
extern nilai; //kata kunci extern ini menunjukan bahwa variabel dapat di akses secara global di dalam program
nilai ++ ; // variabel eksternal dinaikkan
}
3. Static Variables
Both external and automatic variables can be static variables. A static variable has the following properties:
If a local variable stands as a static variable, then:
- Variables can only be accessed within the function that defines them.
- Variables are not lost when function execution ends
- Initialization by the programmer will be done only once during the program execution.
If an external variable is made a static variable, then this variable can be accessed by all files defined in the same file as the external variable.
Example:
Static Variables
Program:
#include <iostream.h>
#include <conio.h>
/**
*bundet.com
*Variabel Statis
*/
void saya_ingat(); //Prototipe fungsi atau pendeklarasian fungsi
void main()
{
int mana = 50;
clrscr();
saya_ingat();
saya_ingat();
saya_ingat();
cout<<"main() : mana = "<<mana<<endl;
getch();
}
//Pada fungsi di bawah ini, mana didefinisikan sebagai variabel statis
void saya_ingat()
{
static int mana = 77; //variabel statis
mana++; //naikkan sebesar 1
cout<<"gatewan_ingat() : mana = " <<mana<<endl;
}
Hope this is useful & happy learning!