290 likes | 448 Views
Dosen : Ir. Hasanuddin Sirait , MT. Pemrograman Terstruktur C++. . :: Person 10 :: Stefani. P. Tangkuman Yan Makarunggala Supardi Jamali. STMIK / AMIK PARNA RAYA MANADO. 6.10 Variabel Lokal dan Global.
E N D
Dosen :Ir. HasanuddinSirait, MT PemrogramanTerstruktur C++ :: Person 10 :: Stefani. P. Tangkuman Yan Makarunggala SupardiJamali STMIK / AMIK PARNA RAYA MANADO Created By Stefanikha69
6.10 VariabelLokaldan Global • Suatu variabel lokal dideklarasikan di dalam fungsi ‘a’ , dantidakdapatdiakses di luarfungsi (a) tersebut. • Suatuvariabel global dideklarasikan(di) luar / selainsemuafungsi (a) dandapatdiaksesdalamlingkupnya . Created By Stefanikha69
Program 6-16 // Program inimenampilkanpendeklarasianvariabel ‘a’ dalamsuatufungsi // Yang tersembunyidarifungsi lain. #include <iostream.h> void func(void); // FungsiPrototipe void main(void) { intnum = 1; cout<<"In main, num is " <<num<<endl; func(); cout<<"Back in main, num is still "<<num<<endl; } Created By Stefanikha69
Lanjutan //********************************************************* // Mendefinisikanfungsifunc. * / / Inimemilikivariabellokal, num, yang nilaiawal, 20, * / / Akan ditampilkan.* //********************************************************* void func(void) { intnum = 20; cout << "In func, num is " << num << endl; } Created By Stefanikha69
Keluaran Program Di utama / awal, numbernilai 1 Di func, numbernilai 20 Kembali ke utama / awal, num masih bernilai 1 Created By Stefanikha69
Figur 6-8 Variabelpadanumhanyaterlihatpadafungsiutama (main). FUNGSI UTAMA NUM = 1 Variabelpadanumhanyaterlihatpadafungsifunc. FUNGSI FUNC NUM = 20 Created By Stefanikha69
Program 6-17 // Program inimenampilkanvariabel global // kepadasemuafungsi yang dekatsetelah program // mendeklarasikanvariabel. #include <iostream.h> void func(void); // FungsiPrototipe intnum = 2; // Variabel Global void main(void) { cout << "In main, num is " << num << endl; func(); cout << "Back in main, num is " << num << endl; } Created By Stefanikha69
Lanjutan //***************************************************** // MendefinisikanFungsifunc. * // funcmenggantikannilaidarisetiapvariabel global num* //***************************************************** void func(void) { cout << "In func, num is " << num << endl; num = 50; cout << "But, it is now changed to " << num << endl; } Created By Stefanikha69
Keluaran Program Di utama / awal, numbernilai 2 Di func, numbernilai 2 Tapi, sekarangberubahnilaimenjadi 50 Kembalikeutama / awal, numbernilai 50 Created By Stefanikha69
Program 6-18 / / Program inimenunjukkanbahwavariabel global terlihat / / Untuksemuafungsi yang munculdalam program setelah / / Deklarasivariabel. #include <iostream.h> void func(void); // Function prototype void main(void) { cout << "In main, num is not visible!\n"; func(); cout << "Back in main, num still isn't visible!\n"; } Created By Stefanikha69
Lanjutan int num = 2; // Global variable // ***************************************************** // Definisifungsifungsi * // Perubahanfungsinilaivariabel num global. * // ***************************************************** void func(void) { cout << "In func, num is " << num << endl; num = 50; cout << "But, it is now changed to " << num << endl; } Created By Stefanikha69
Keluaran Program Dalamutama, num tidakterlihat! Dalamfunc, num adalah 2 Tapi, sekarangberubahmenjadi 50 Kembalidiutama, num masihtidakterlihat! Created By Stefanikha69
Variabel global diinisialisasi 0 oleh default • KecualiAndasecaraeksplisitmenginisialisasinumerikvariabel global, merekasecaraotomatisdiinisialisasike nol. • variabelkarakter global diinisialisasi NULL, ataukode ASCII 0. Created By Stefanikha69
Program 6-19 // Program ini memiliki variabel global yang tidak diinisialisasi. #include <iostream.h> intglobalNum; // Global variable. Automatically set to zero. void main(void) { cout << "globalNum is " << globalNum << endl; } Created By Stefanikha69
Keluaran Program globalNumadalah 0 Created By Stefanikha69
VariabelLokaldan Global denganNama yang sama • Jikafungsimemilikivariabellokaldengannama yang samasebagaivariabel global, hanyavariabellokaldapatdilihatolehfungsi. Created By Stefanikha69
Program 6-20 / / Program inimenunjukkanbahwaketikavariabellokalmemiliki / / Nama yang samasebagaivariabel global, fungsihanyamelihat / / Variabellokal. #include <iostream.h> // FungsiPrototipe void texas(); void arkansas(); int cows = 10; void main(void) { cout << "There are " << cows << " cows in main.\n"; texas(); arkansas(); cout << "Back in main, there are " << cows << " cows.\n"; } Created By Stefanikha69
Lanjutan / / ****************************************** / / Definisifungsitexas. * / / Sapi-sapivariabellokaldiaturke 100. * / / ****************************************** void texas(void) { int cows = 100; cout << "There are " << cows << " cows in texas.\n"; } Created By Stefanikha69
Lanjutan / / ****************************************** / / Definisifungsiarkansas. * / / Sapi-sapivariabellokaldiaturke 50. * / / ****************************************** void arkansas(void) { int cows = 50; cout << "There are " << cows << " cows in arkansas.\n"; } Created By Stefanikha69
Keluaran Program Ada 10 ekorsapiutama. Ada 100 sapiditexas. Ada 50 ekorsapidiarkansas. Kembalipadautama, ada 10 sapi. Created By Stefanikha69
Program 6-21 / / Program inimemilikivariabellokaldan global. Dalamfungsi / / RingUpSale, adavariabelbernamapajakdaerah. ada / / Jugavariabel global dengannama yang sama. #include <iostream.h> void ringUpSale(void); // Function prototype // Global Variables const float taxRate = 0.06; float tax, sale, total; void main(void) { char again; Created By Stefanikha69
Lanjutan cout.precision(2); cout.setf(ios::fixed | ios::showpoint); do { ringUpSale(); cout << "Is there another item to be purchased? "; cin >> again; } while (again == 'y' || again == 'Y'); tax = sale * taxRate; total = sale + tax; cout << "The tax for this sale is " << tax << endl; cout << "The total is " << total << endl; } Created By Stefanikha69
/ / ****************************************************************** / / DefinisifungsiringUpSale. * / / Fungsiinimemintauntukkuantitasdanhargasatuandari item. * / / Kemudianmenghitungdanmenampilkanpajakpenjualandan subtotal * / / Untukbarang-barang. * / / ****************************************************************** void ringUpSale(void) { int qty; float unitPrice, tax, thisSale, subTotal; cout << "Quantity: "; cin >> qty; cout << "Unit price: "; cin >> unitPrice; thisSale = qty * unitPrice; // Dapatkanharga total unit Lanjutan Created By Stefanikha69
Lanjutan sale += thisSale; // Perbaruipenjualanvariabel global tax = thisSale * taxRate; //Dapatkan pajak penjualan untuk barang-barang subTotal = thisSale + tax; // Dapatkan subtotal untuk item ini cout << "Price for these items: " << thisSale << endl; cout << "tax for these items: " << tax << endl; cout<< "subTotal for these items: " << subTotal << endl; } Created By Stefanikha69
Keluaran Program Jumlah: 2 [Enter] HargaSatuan: 20.00 [Enter] Hargauntuk item: 40.00 pajakuntukbarang-barang: 2.40 subtotal untuk item: 42.40 Apakahada item lain yang akandibeli? y [Enter] Jumlah: 3 [Enter] HargaSatuan: 12.00 [Enter] Hargauntuk item: 36.00 pajakuntukbarang-barang: 2.16 subtotal untuk item: 38.16 Apakahada item lain yang akandibeli? n [Enter] Pajakuntukdijualiniadalah 4,56 Totalnyaadalah 80,56 Created By Stefanikha69
Hati-hatiDenganVariabel global • Hal inimenggodauntukmembuatsemuavariabel global. Tapijanganlakukanitu! • Menggunakanvariabel global dapatmenyebabkanmasalah. - Inilebihsulituntuk debug program yang menggunakan global yang variabel Created By Stefanikha69
PRESENTED BY PERSON 10 (Stefani, Yan, Supardi) Download This File : Stefanikha69.wordpress.com Thank’s for your attention Created By Stefanikha69