Fonksiyonlar Fonksiyonlar Program modler hale getirir Yerel deikenler
- Slides: 27
Fonksiyonlar • Fonksiyonlar – Programı modüler hale getirir – Yerel değişkenler, fonksiyonların içerisinde tanımlanır. • Sadece tanımlandığı fonksiyon tarafından bilinir – Parametreler • Fonksiyonlar arasındaki bilgi alışverişi • Yerel değişkenler • Fonksiyonların yararları – Bölme ve Zaptetme • Yönetilebilir program geliştirme – Yazılımı yeniden kullanabilme • Varolan fonksiyonları yeni programlar için blok olarak inşa etme • Soyutlama – içsel detayları gizleme (kütüphane fonksiyonları) – Kodları tekrar yazmayı engelleme © Copyright 1992– 2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
Fonksiyon Tanımları • Fonksiyon Tanımlama biçimleri return-değer-tipi function-name( parametere-listesi ) { bildirimler ve ifadeler } – Fonksiyon adı: herhangi geçerli tanımlayıcı – Veri tipi döndürme: sonucun veri tipi (varsayılan int) • void – fonksiyonlar geriye değer döndürmez – Parametre-listesi: parametreleri belirtilmiş, virgülle ayrılmış listedir • A type must be listed explicitly for each parameter unless, the parameter is of type int • Her parametre için parametre tipleri ayrı belirtilmelidir (eğer tipleri int değilse) © Copyright 1992– 2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
Fonksiyon Tanımlama • Fonksiyon Tanımlama biçimleri (devam) return-değer-tipi function-name( parametere-listesi ) { bildirimler ve ifadeler } – Bildirimler ve ifadeler: fonksiyonun gövdesi • Değişkenler blokların içinde tanımlanabilir (kısıtlanabilir) • Fonksiyonlar diğer fonksiyonların içinde tanımlanamaz. – Geri dönüş kontrolu • Eğer boş değer geri dönüyorsa (hiç bir değer dönmüyorsa) – return; – veya, sağ paranteze ulaşıncaya kadar • Eğer bir değer geri dönüyorsa – return ifade(geri dönecek değer); © Copyright 1992– 2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
fig 05_03. c (Part 1 of 2) © Copyright 1992– 2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Outline
Outline fig 05_03. c (Part 2 of 2) Program çıktısı 1 4 9 16 25 36 49 64 81 100 © Copyright 1992– 2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
Outline fig 05_04. c (Part 1 of 2) © Copyright 1992– 2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
Outline fig 05_04. c (Part 2 of 2) Enter three Maximum is: integers: 22 85 17 85 integers: 85 22 17 85 integers: 22 17 85 85 © Copyright 1992– 2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Program çıktısı
5. 6 Fonksiyon Prototipleri • Fonksiyon prototipi – – – Fonksiyon adı Parametreler – fonksiyonun içinde tuttuğu şeyler Return type – fonksiyonun veri dönüşü tipi (varsayılan int) Fonksiyonları onaylamakta kullanılır Eğer fonksiyon prototipi programın içerisinde yer almazsa, prototipe ihtiyaç duyulur – Prototip ile fonksiyonlar int maksimum( int x, int y, int z ); • 3 ints içerir • int değeri döndürür • Geçiş kuralları ve dönüşümler – Kendinden küçük tiplere dönüşümler hata oluşturabilir © Copyright 1992– 2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
5. 6 Fonksiyon Prototipleri © Copyright 1992– 2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
5. 7 Öncü (Header) Dosyalar • Öncü Dosyalar – Kütüphane fonksiyonları için fonksiyon prototipleri içerir – <stdlib. h> , <math. h> , vs. . . – Yükleme. . . #include <filename> #include <math. h> • Öncü dosya düzenleme – Fonksiyonlarla dosya oluşturma – filename. h olarak kayıt etme. – #include "filename. h“ ile başka dosyalara yükleme – Fonksiyonları tekrar kullanma. © Copyright 1992– 2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
5. 7 Öncü Dosyalar © Copyright 1992– 2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
5. 8 Fonksiyon çağırma: Değere Göre ve Referansa Göre • Değere göre – Argümanın kopyası fonksiyona gönderir. – Kopyayı değiştirmek, orijinal değişkenin değerini etkilemez – Fonksiyonun argüman’ı değiştirmeye ihtiyaç duymadığı zaman kullanılmalıdır. • Önemli sorunların oluşmasını engeller • Referansa göre – Orijinal argümanı gönderir. – Argümanı değiştirmek, orijinal değişkenin değerini etkiler – Sadece güvenilir fonksiyonlar ile kullanılmalı • Şimdi, değere göre çağırmaya bakalım © Copyright 1992– 2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
5. 9 Rasgele Sayılar Üretme • rand fonksiyonu – yükle <stdlib. h> – 0 ve RAND_MAX (en az 32767) arasında sayı “random” geri döndürme. i = rand(); – Pseudorandom • “random” sayıların önceden ayarlanmış dizisi • Her fonksiyon çağrılmasında aynı dizi tekrar eder. • Derecelendirme (Scaling) – 1 ile n arasında random sayı alma 1 + ( rand() % n ) • rand() % n 0 ile n– 1 arasında bir sayı döndürür • 1 ile n arasında random sayıya 1 ekleme yapar. 1 + ( rand() % 6) – 1 and 6 arasında sayı © Copyright 1992– 2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
5. 9 Rasgele Sayılar Üretme • srand fonksiyonu (rassallaştırma) – <stdlib. h> – Farklı bir dizide rasgele sayılar oluşturulmasını sağlar – Unsigned(işaretsiz) tipte bir tamsayıyı argüman olarak kullanır srand( seed ); – srand( time( NULL ) ); /*yükle <time. h> */ • time( NULL ) – Program çalıştırıldığı esnadaki saniye cinsinde oluşturur – “(Randomizes) rasgele seçmeler" çekirdek © Copyright 1992– 2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
Outline fig 05_07. c © Copyright 1992– 2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
6 5 6 6 6 1 6 2 5 1 2 3 5 5 4 4 6 3 2 1 © Copyright 1992– 2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Outline Program çıktısı
Outline fig 05_08. c (Part 1 of 3) © Copyright 1992– 2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
Outline fig 05_08. c (Part 2 of 3) © Copyright 1992– 2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
Outline fig 05_08. c (Part 3 of 3) Face 1 2 3 4 5 6 Frequency 1003 1017 983 994 1004 999 © Copyright 1992– 2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Program Çıktısı
Outline fig 05_09. c (Part 1 of 2) © Copyright 1992– 2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
Outline fig 05_09. c (Part 2 of 2) Enter seed: 67 6 1 Enter seed: 867 2 1 Enter seed: 67 6 1 Program Çıktısı 1 6 4 1 6 6 2 4 4 1 6 3 1 6 6 2 1 6 4 1 6 6 2 4 © Copyright 1992– 2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
5. 10 Örnek: Şans Oyunu • Barbut(craps) similasyonu • Kurallar – İki zar atılır • İlk atışta 7 veya 11 (top. ), oyuncu kazanır. • İlk atışta 2, 3, veya 12 (top. ), oyuncu kaybeder. • İlk atışta 4, 5, 6, 8, 9, 10 – oyuncunun puanı – Kazanmak için kendi puanını 7 gelmeden önce tekrar atması gerekir. © Copyright 1992– 2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
Outline fig 05_10. c (Part 1 of 4) © Copyright 1992– 2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
Outline fig 05_10. c (Part 2 of 4) © Copyright 1992– 2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
Outline fig 05_10. c (Part 3 of 4) © Copyright 1992– 2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
Outline fig 05_10. c (Part 4 of 4) © Copyright 1992– 2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
Player rolled 5 + 6 = 11 Player wins Player rolled Point is 5 Player rolled Player wins 4 + 1 = 5 6 + 2 = 8 2 + 1 = 3 3 + 2 = 5 Player rolled 1 + 1 = 2 Player loses Player rolled 1 + 4 = 5 Point is 5 Player rolled 3 + 4 = 7 Player loses © Copyright 1992– 2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Outline Program Çıktısı
- Tehlike kaynağına yönelik koruma uygulamaları
- Kaynakta yerel aspirasyon sistemi
- Yerel yönetim nedir
- Siyaset nedir
- Fonksiyonlar not
- Fonksiyonlar tekrar
- Fonksiyonlar sunum
- Eşit fonksiyonlar
- Arduino fonksiyon tanımlama
- Arduino fonksiyonlar
- Bilgisayar bilimi fonksiyonlar
- Bir fonksiyonun karesi
- John proctor
- Rev john hale
- Answers
- Cynthia hale birthday
- Lucy lambert hale
- Hales
- Infoready uark
- John proctor the crucible quotes
- Who is rebecca nurse in the crucible
- Why does hale visit the proctor's’ home
- Sıvı maddenin soğutularak katı hale geçmesi
- Sıvı maddenin soğutularak katı hale geçmesi
- Alfred hale fingerprints
- Hale woodruff mutiny on the amistad
- Hale retail group
- Why did mrs. putnam enlist tituba’s help?