{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Function 方法操作 - 模組化商務程式的整合者\n", "\n", "#### 作者:黃鈺淇(臺灣行銷研究特邀作者)\n", "#### 完整文章介紹連結:https://medium.com/@andiology/function-方法操作-模組化商務程式的整合者-c00d8210f9f8\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 函式架構" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ " def Function_Name(Parameter):\n", " \"\"\"\n", " Introduction\n", " \"\"\"\n", " Main Code\n", " return" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Example:" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "# 定義函式\n", "def sum_three(a,b,c):\n", " '''\n", " This function will sum all the numbers in the parameters.\n", " '''\n", " output = a + b + c\n", " print('sum =',output)\n", " return output" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "sum = 18\n" ] }, { "data": { "text/plain": [ "18" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 呼叫函式\n", "sum_three(3,6,9)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 預設參數值" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "def sum_three(a=1,b=2,c=3):\n", " '''\n", " This function will sum all the numbers in the parameters.\n", " '''\n", " output = a + b + c\n", " print('sum =',output)\n", " return output" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "sum = 12\n" ] }, { "data": { "text/plain": [ "12" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sum_three(3,6)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 變數範圍" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "x = 10" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "# Function內使用全域變數\n", "def example():\n", " global x\n", " print(x + 5)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "15\n" ] } ], "source": [ "example()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 回傳值" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "sum = 18\n" ] } ], "source": [ "x = sum_three(3,6,9)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "18" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 補充:檢視函式說明" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on function sum_three in module __main__:\n", "\n", "sum_three(a=1, b=2, c=3)\n", " This function will sum all the numbers in the parameters.\n", "\n" ] } ], "source": [ "help(sum_three)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 實作練習解答\n", "#### Fahrenheit = (Celsius * 9/5) + 32" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [], "source": [ "def Celsius_to_Fahrenheit(celsius = 0):\n", " fahrenheit = (celsius * 9/5) + 32\n", " print(celsius, 'Celsius is equivalent to', fahrenheit, 'Fahrenheit')\n", " return fahrenheit" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "40 Celsius is equivalent to 104.0 Fahrenheit\n" ] } ], "source": [ "f = Celsius_to_Fahrenheit(40)" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "104.0" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 進階:模組程式" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [], "source": [ "# 加法\n", "def plus(x,y):\n", " p = x + y\n", " return p" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [], "source": [ "# 減法\n", "def minus(x,y):\n", " m = x - y\n", " return m" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [], "source": [ "# 乘法\n", "def multiply(x,y):\n", " mul = x * y\n", " return mul" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [], "source": [ "# 除法\n", "def divide(x,y):\n", " div = x / y\n", " return div" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "9" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "plus(6,3)" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "minus(6,3)" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "18" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "multiply(6,3)" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2.0" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "divide(6,3)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "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.8.3" } }, "nbformat": 4, "nbformat_minor": 2 }