{ "cells": [ { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "# Fonksiyonları Dönmek ve Argüman Olarak Göndermek\n", "\n", "Bu konuda fonksiyonları return ile başka bir fonksiyondan dönmeyi ve diğer fonksiyonlara parametre olarak göndermeyi öğreneceğiz." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Fonksiyonları return ile Dönmek\n", "\n", "Bir fonksiyon aynı zamanda bir obje olduğu için, bu fonksiyonu başka bir fonksiyondan **return** ile döndürebiliriz. " ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def fonksiyon(işlem_adı):\n", " \n", " def toplama(*args):\n", " toplam = 0\n", " for i in args:\n", " toplam += i\n", " return toplam\n", " \n", " def çarpma(*args):\n", " çarpım = 1\n", " for i in args:\n", " çarpım *= i\n", " return çarpım\n", " \n", " if işlem_adı == \"toplama\":\n", " return toplama\n", " else:\n", " return çarpma\n", " \n", " " ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": true }, "outputs": [], "source": [ "deneme = fonksiyon(\"toplama\")" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ ".toplama>" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "deneme # toplama fonksiyonuna eşit oldu." ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "28" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "deneme(1,2,3,4,5,6,7)" ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "collapsed": true }, "outputs": [], "source": [ "deneme2 = fonksiyon(\"çarpma\")" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ ".çarpma>" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "deneme2 # çarpma fonksiyonuna eşit oldu." ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "362880" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "deneme2(1,2,3,4,5,6,7,8,9)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "İşte bu şekilde fonksiyonlarımızı da tıpkı diğer değişkenler gibi başka bir fonksiyon içinde tanımlayıp , çağırdığımız yere dönebiliriz." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Fonksiyonları Argüman Olarak Göndermek" ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def toplama(a,b):\n", " return a + b\n", "def çıkarma(a,b):\n", " return a-b\n", "def çarpma(a,b):\n", " return a * b\n", "def bölme(a,b):\n", " return a / b\n" ] }, { "cell_type": "code", "execution_count": 28, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def anafonksiyon(func1,func2,func3,func4,işlem_adı): # Tanımladığımız 4 fonksiyonu da argüman olarak göndereceğiz.\n", " if (işlem_adı == \"toplama\"):\n", " print(func1(3,4))\n", " elif (işlem_adı == \"çıkarma\"):\n", " print(func2(10,3))\n", " elif (işlem_adı == \"çarpma\"):\n", " print(func3(10,3))\n", " elif (işlem_adı == \"bölme\"):\n", " print(func4(10,4))\n", " else:\n", " print(\"Geçersiz işlem adı...\")" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "7\n" ] } ], "source": [ "anafonksiyon(toplama,çıkarma,çarpma,bölme,\"toplama\")" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2.5\n" ] } ], "source": [ "anafonksiyon(toplama,çıkarma,çarpma,bölme,\"bölme\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Buradaki gibi her fonksiyonumuz aslında birer obje olduğu için diğer objeler gibi başka fonksiyonlara argüman olarak gönderilebilir. Bunları da öğrendiğimize göre artık decoratorlar için hazırı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 }