{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Fonksiyonlarda Parametre Türleri\n", "\n", "Bu derste fonksiyonlara parametre vermenin farklı yollarını öğrenmeye çalışacağız. İsterseniz hemen başlayalım.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Parametrelerin Varsayılan Değerleri\n", "\n", "Biliyorsunuz önceki konularda şöyle bir fonksiyon tanımlamıştık." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def selamla(isim):\n", " print(\"Selam\",isim)\n", " " ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Selam Murat\n" ] } ], "source": [ "selamla(\"Murat\")" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Selam Serhat\n" ] } ], "source": [ "selamla(\"Serhat\")" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "selamla() missing 1 required positional argument: 'isim'", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mselamla\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;31m# Böyle bir kullanım hata verecektir.\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;31mTypeError\u001b[0m: selamla() missing 1 required positional argument: 'isim'" ] } ], "source": [ "selamla() # Böyle bir kullanım hata verecektir." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Ancak biz eğer bir parametrenin değerini varsayılan olarak belirlemek istersek, bunu varsayılan değerler ile yapabiliriz. Varsayılan değerleri anlamak için **selamla** fonksiyonunu varsayılan parametre değeri ile yazalım.\n" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def selamla(isim = \"İsimsiz\"):\n", " print(\"Selam\",isim)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Selam İsimsiz\n" ] } ], "source": [ "selamla() # Hiç bir değer göndermedik. \"isim\" parametresinin değeri varsayılan olarak \"İsimsiz\" olarak belirlendi" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Selam Serhat\n" ] } ], "source": [ "selamla(\"Serhat\") # Değer verirsek varsayılan değerin yerine bizim verdiğimiz değer geçer. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "İşte bu kadar ! Peki birçok parametreye sahip olursak ne olacak ? Bir fonksiyon daha tanımlayalım" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def bilgilerigöster(ad = \"Bilgi Yok\",soyad = \"Bilgi Yok\",numara = \"Bilgi Yok\"):\n", " print(\"Ad:\",ad,\"Soyad:\",soyad,\"Numara:\",numara)" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Ad: Bilgi Yok Soyad: Bilgi Yok Numara: Bilgi Yok\n" ] } ], "source": [ "bilgilerigöster() # Bütün parametreler varsayılan değerle ekrana basılacak." ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Ad: Mustafa Murat Soyad: Coşkun Numara: Bilgi Yok\n" ] } ], "source": [ "bilgilerigöster(\"Mustafa Murat\",\"Coşkun\") # ad ve soyad değerini verdik ancak numara parametresi varsayılan değer oldu. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Ancak böyle bir durumda argümanları gönderirken değerleri **sıralı** vermemiz gerekiyor. Peki sadece **numara** parametresine değer vermek istersek ne yapacağız ? \n" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Ad: Bilgi Yok Soyad: Bilgi Yok Numara: 123456\n" ] } ], "source": [ "bilgilerigöster(numara = \"123456\") # numara parametresini özel olarak belirtiyoruz." ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Ad: Mustafa Murat Soyad: Bilgi Yok Numara: 123456\n" ] } ], "source": [ "bilgilerigöster(ad = \"Mustafa Murat\",numara = \"123456\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Aslında biz varsayılan değerleri kursumuzun en başlarında görmüştük. **print fonksiyonunun sep parametresini hatırlayalım.**" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Mustafa Murat Coşkun\n" ] } ], "source": [ "print(\"Mustafa\",\"Murat\",\"Coşkun\") # sep parametresine değer vermeyince varsayılan olarak boşluk karakteri verildi." ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Mustafa/Murat/Coşkun\n" ] } ], "source": [ "print(\"Mustafa\",\"Murat\",\"Coşkun\",sep = \"/\") # sep parametresine özel olarak değer atadık." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "İsterseniz print fonksiyonun nasıl yazıldığına **help** fonksiyonu sayesinde bakalım." ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on built-in function print in module builtins:\n", "\n", "print(...)\n", " print(value, ..., sep=' ', end='\\n', file=sys.stdout, flush=False)\n", " \n", " Prints the values to a stream, or to sys.stdout by default.\n", " Optional keyword arguments:\n", " file: a file-like object (stream); defaults to the current sys.stdout.\n", " sep: string inserted between values, default a space.\n", " end: string appended after the last value, default a newline.\n", " flush: whether to forcibly flush the stream.\n", "\n" ] } ], "source": [ "help(print) # sep parametresine varsayılan olarak boşluk değeri verildiğini görebiliyoruz." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Esnek Sayıda Değerler" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Biliyorsunuz bir fonksiyon yazıldığında özel olarak kaç tane parametresi olacağını önceden belirtmemiz gerekiyor. Örneğin, bir **toplama** fonksiyonu yazalım. " ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def toplama(a,b,c):\n", " print(a+b+c)\n", " " ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "12\n" ] } ], "source": [ "toplama(3,4,5)" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "toplama() takes 3 positional arguments but 4 were given", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mtoplama\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m4\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m5\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m6\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;31m# 4 tane argüman veremeyiz.\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;31mTypeError\u001b[0m: toplama() takes 3 positional arguments but 4 were given" ] } ], "source": [ "toplama(3,4,5,6) # 4 tane argüman veremeyiz." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Eğer bu fonksiyonu 4 argüman alacak şekilde tanımlamak istersek, tekrardan tanımlamamız gerekiyor." ] }, { "cell_type": "code", "execution_count": 28, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def toplama(a,b,c,d):\n", " print(a+b+c+d)" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "18\n" ] } ], "source": [ "toplama(3,4,5,6)" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "toplama() missing 1 required positional argument: 'd'", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mtoplama\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m4\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m5\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;31m# Ancak bu seferde 3 argüman veremiyoruz.\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;31mTypeError\u001b[0m: toplama() missing 1 required positional argument: 'd'" ] } ], "source": [ "toplama(3,4,5) # Ancak bu sefer de 3 argüman veremiyoruz." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Peki ben bir fonksiyonu esnek sayıda argümanla kullanmak istersem ne yapacağım ? Bunun için de **Yıldızlı Parametre** kullanmam gerekiyor. Kullanımı şu şekildedir;\n" ] }, { "cell_type": "code", "execution_count": 39, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def toplama(*parametreler): # Artık parametreler değişkenini bir demet gibi kullanabilirim.\n", " toplam = 0\n", " print(\"Parametreler:\",parametreler)\n", " for i in parametreler:\n", " toplam += i\n", " return toplam\n", "\n", " " ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Parametreler: (3, 4, 5, 6, 7, 8, 9, 10)\n", "52\n" ] } ], "source": [ "print(toplama(3,4,5,6,7,8,9,10))" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Parametreler: ()\n", "0\n" ] } ], "source": [ "print(toplama())" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Parametreler: (1, 2, 3)\n", "6\n" ] } ], "source": [ "print(toplama(1,2,3))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "print fonksiyonunu tekrar hatırlayacak olursak aslında **print** fonksiyonu bu şekilde tanımlanmış bir fonksiyondur. Çünkü biz print fonksiyonuna istediğimiz sayıda argüman gönderebiliyorduk." ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3 4 5 6\n" ] } ], "source": [ "print(3,4,5,6)" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Elma Armut\n" ] } ], "source": [ "print(\"Elma\",\"Armut\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Bu konuda gördüğümüz gibi Python'ın bu özelliklerini kullanarak daha esnek fonksiyonlar yazabiliriz. Bir sonraki konuda **Global ve Yerel değişkenleri** öğrenmeye çalışacağız." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.1" } }, "nbformat": 4, "nbformat_minor": 2 }