1 / 27

Person 10

Person 10. Arthur Sumandag. Pemrograman Terstruktur C++. 6.10 Variabel Lokal dan Global. Suatu variabel lokal dideklarasikan di dalam fungsi ‘ a ’ , dan tidak dapat diakses di luar fungsi ( a ) tersebut .

Download Presentation

Person 10

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Person 10 Arthur Sumandag Pemrograman Terstruktur C++

  2. 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 .

  3. 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; }

  4. Lanjutan //********************************************************* // Mendefinisikanfungsifunc. * / / Inimemilikivariabellokal, num, yang nilaiawal, 20, * / / Akan ditampilkan.* //********************************************************* void func(void) { intnum = 20; cout << "In func, num is " << num << endl; }

  5. Keluaran Program Di utama / awal, numbernilai 1 Di func, numbernilai 20 Kembali ke utama / awal, num masih bernilai 1

  6. Figur 6-8 Variabelpadanumhanyaterlihatpadafungsiutama (main). FUNGSI UTAMA NUM = 1 Variabelpadanumhanyaterlihatpadafungsifunc. FUNGSI FUNC NUM = 20

  7. 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; }

  8. 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; }

  9. Keluaran Program Di utama / awal, numbernilai 2 Di func, numbernilai 2 Tapi, sekarangberubahnilaimenjadi 50 Kembalikeutama / awal, numbernilai 50

  10. 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"; }

  11. 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; }

  12. Keluaran Program Dalamutama, num tidakterlihat! Dalamfunc, num adalah 2 Tapi, sekarangberubahmenjadi 50 Kembalidiutama, num masihtidakterlihat!

  13. Variabel global diinisialisasi 0 oleh default • KecualiAndasecaraeksplisitmenginisialisasinumerikvariabel global, merekasecaraotomatisdiinisialisasike nol. • variabelkarakter global diinisialisasi NULL, ataukode ASCII 0.

  14. 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; }

  15. Keluaran Program globalNumadalah 0

  16. VariabelLokaldan Global denganNama yang sama • Jikafungsimemilikivariabellokaldengannama yang samasebagaivariabel global, hanyavariabellokaldapatdilihatolehfungsi.

  17. 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"; }

  18. Lanjutan / / ****************************************** / / Definisifungsitexas. * / / Sapi-sapivariabellokaldiaturke 100. * / / ****************************************** void texas(void) { int cows = 100; cout << "There are " << cows << " cows in texas.\n"; }

  19. Lanjutan / / ****************************************** / / Definisifungsiarkansas. * / / Sapi-sapivariabellokaldiaturke 50. * / / ****************************************** void arkansas(void) { int cows = 50; cout << "There are " << cows << " cows in arkansas.\n"; }

  20. Keluaran Program Ada 10 ekorsapiutama. Ada 100 sapiditexas. Ada 50 ekorsapidiarkansas. Kembalipadautama, ada 10 sapi.

  21. 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;

  22. 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; }

  23. Lanjutan / / ****************************************************************** / / 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

  24. 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; }

  25. 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

  26. Hati-hatiDenganVariabel global • Hal inimenggodauntukmembuatsemuavariabel global. Tapijanganlakukanitu! • Menggunakanvariabel global dapatmenyebabkanmasalah. - Inilebihsulituntuk debug program yang menggunakan global yang variabel

  27. TERIMA KASIH Keyword : smarthur

More Related