{ "cells": [ { "cell_type": "markdown", "id": "7ef9d0af", "metadata": {}, "source": [ "# 概要\n", "関数の作成方法を学ぶ。" ] }, { "cell_type": "markdown", "id": "a729764f", "metadata": {}, "source": [ "# 関数の作成方法\n", "シンプルに引数を取らない関数を作ってみる。\n", "\n", "## 1行の処理を行う例\n", "処理を1行だけ行う例を作ってみる。処理内容はインデント部分。" ] }, { "cell_type": "code", "execution_count": 2, "id": "2e91a72a", "metadata": {}, "outputs": [], "source": [ "# define a simple function\n", "def HelloWorld():\n", " print('Hello, world!')" ] }, { "cell_type": "markdown", "id": "a2ea9764", "metadata": {}, "source": [ "関数が定義されていることは、丸括弧なしで呼び出してみればわかる。この場合、関数が実行されるのではなくオブジェクトが表示される。" ] }, { "cell_type": "code", "execution_count": 8, "id": "75c14aa3", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "<function __main__.HelloWorld()>" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# see the function\n", "HelloWorld" ] }, { "cell_type": "markdown", "id": "6586d02b", "metadata": {}, "source": [ "関数を呼び出して使うには、関数名と丸括弧を書けば良い。" ] }, { "cell_type": "code", "execution_count": 4, "id": "dad0b1ad", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello, world!\n" ] } ], "source": [ "# call the function\n", "HelloWorld()" ] }, { "cell_type": "markdown", "id": "406b437d", "metadata": {}, "source": [ "## 複数行の処理を行う例\n", "処理を複数行に渡って行う例を作ってみる。本当は引数にint型を取って、それが奇数(odd)か偶数(even)かを判別して出力する関数にしたいが、今回は引数を取らずに作ってみる。" ] }, { "cell_type": "code", "execution_count": 5, "id": "83ee5b6b", "metadata": {}, "outputs": [], "source": [ "# define a function\n", "def OddEven():\n", " # define an int instead of giving it as an argument\n", " n = 15\n", " \n", " # show the value of n\n", " print('n = ', n)\n", " \n", " # see if n is even or odd\n", " if(n % 2 == 0):\n", " print(' n is even')\n", " else:\n", " print(' n is odd')" ] }, { "cell_type": "code", "execution_count": 6, "id": "9c47f185", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "n = 15\n", " n is odd\n" ] } ], "source": [ "# call the function\n", "OddEven()" ] }, { "cell_type": "markdown", "id": "100dc029", "metadata": {}, "source": [ "# 練習問題\n", "練習問題をやってみましょう。以下の処理を行うコードを書いてみましょう。\n", "1. 「Hello, python!」と出力する関数HelloPythonを作成してください。\n", "1. HelloPythonを呼び出してください" ] }, { "cell_type": "markdown", "id": "cd02f1da", "metadata": {}, "source": [ "## 回答例" ] }, { "cell_type": "code", "execution_count": 7, "id": "168b8c96", "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello, python!\n" ] } ], "source": [ "# 1: define a function\n", "def HelloPython():\n", " print('Hello, python!')\n", "\n", "# 2: call the function\n", "HelloPython()" ] }, { "cell_type": "markdown", "id": "bbbf54ca", "metadata": {}, "source": [ "以上" ] }, { "cell_type": "code", "execution_count": null, "id": "0cb80d60", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.9.7" } }, "nbformat": 4, "nbformat_minor": 5 }