{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Functions\n", "If you have tasks you need to do many times over, don't code these tasks many times, but instea, use function
\n", "so that each time you need to execute the task, you can simply call the function. This brings simplicity, readability
\n", "and more importantly, easy software maintenance.\n", "\n", "**Created and edited by:** John C.S. Lui on August 8th, 2020.\n", "\n", "** Important note:** *If you want to use and modify this notebook file, please acknowledge the author.*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## General syntax of defining a function" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\\# Let's define a function
\n", "**def** function_name (argument_1, argument_2,...):
\n", "         write down the code of your function here
\n", "         write down the code of your function here
\n", " \n", "\n", "\\# Later on in your program, you can use the *function_name* to call the function
\n", "....
\n", "function_name(value_1, value_2, ...)
\n", "....
\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Examples" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Example\n", "def print_reverse_name(first_name, last_name):\n", " print(\"The person's name is %s %s\" %(last_name, first_name))\n", " \n", "f_name1 = \"John C.S.\"\n", "l_name1 = \"Lui\"\n", "print_reverse_name(f_name1, l_name1)\n", "\n", "f_name2 = \"Frank\"\n", "l_name2 = \"Kelly\"\n", "print_reverse_name(l_name2, f_name2) # It is the \"ordering\" that is important !!!!" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Let say you need to send emails to the following students with the following messages\n", "print(\"Peter, you are doing great in CSCI2040.\")\n", "print(\"And please, give me a good coure evaluation because my job is on the line.\\n\")\n", "\n", "print(\"Mary, you are doing great in CSCI2040.\")\n", "print(\"And please, give me a good coure evaluation because my job is on the line.\\n\")\n", "\n", "print(\"Susan, you are doing great in CSCI2040.\")\n", "print(\"And please, give me a good coure evaluation because my job is on the line.\\n\")\n", "\n", "print(\"Stanley, you are doing great in CSCI2040.\")\n", "print(\"And please, give me a good coure evaluation because my job is on the line.\\n\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# The above code is UGLY, you can simplify it using function\n", "def message_func (name):\n", " print(\"%s, you are doing great in CSCI2040.\" %name)\n", " print(\"And please, give me a good coure evaluation because my job is on the line.\\n\")\n", " \n", "message_func('Peter')\n", "message_func('Mary')\n", "message_func('Susan')\n", "message_func('Stanley')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Better yet, we can use list and function\n", "def message_func (name):\n", " print(\"%s, you are doing great in CSCI2040.\" %name)\n", " print(\"And please, give me a good coure evaluation because my job is on the line.\\n\")\n", "\n", "name_list = ['Peter', 'Mary', 'Susan', 'Stanley']\n", "\n", "for name in name_list:\n", " message_func(name)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# We can also use the list methods we learnt to do more fancy things\n", "def message_func (name):\n", " print(\"%s, you are doing great in CSCI2040.\" %name)\n", " print(\"And please, give me a good coure evaluation because my job is on the line.\\n\")\n", "\n", "name_list = ['Peter', 'Mary', 'Susan', 'Stanley']\n", "\n", "# sort the list\n", "name_list.sort()\n", "\n", "print(\"*****Display items in sorted order:\")\n", "for name in name_list:\n", " message_func(name)\n", "\n", "# sort the list in reverse order\n", "name_list.sort(reverse=True)\n", "print(\"\\n*****Display items in reverse sorted order:\")\n", "for name in name_list:\n", " message_func(name)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Why we want to use functions?\n", "- You only write a set of code ONCE.\n", "- Good software engineering practice. You can focus on a function to make sure it is correct.\n", "- One can modify the function behavior, instead of modifying spreaded codes around your program" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Let say you want to print a message, and give a list of students.\n", "def message_students (students, message):\n", " print(message)\n", " for student in students:\n", " print('\\t *', student)\n", "\n", "name_list = ['Peter', 'Mary', 'Susan', 'Stanley']\n", "message = \"The following students got 'A' in CSCI2040\"\n", "\n", "name_list.sort()\n", "message_students(name_list, message)\n", "\n", "print(\"\\n\")\n", "name_list.sort(reverse=True)\n", "message = \"The folloiwng students also hate John C.S. Lui\"\n", "message_students(name_list, message)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Values returned by a function\n", "Each function can return some values to the caller, and it can be very useful." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "def message_students (students, message):\n", " print(message)\n", " for student in students:\n", " print('\\t *', student)\n", " return len(students) \n", "\n", "name_list = ['Peter', 'Mary', 'Susan', 'Stanley']\n", "message = \"The following students got 'A' in CSCI2040\"\n", "\n", "name_list.sort()\n", "list_size = message_students(name_list, message)\n", "print(\"\\nThere are %d students in the list\" %list_size)\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Use **globals**\n", "\n", "At times, we want to access some \"global variables\". We can use the **global** statement" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# def a function\n", "def scopetest (a):\n", " global b\n", " b = 4\n", " a = 12\n", " print('inside function scopetest, a =', a, \";\", \"b =\", b)\n", " \n", "a = 1\n", "b = 2\n", "print ('a=', a, ';', 'b=', b)\n", "scopetest(a)\n", "print ('now a=', a, \";\", \"b=\", b)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Let's examine our understanding on function" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Question: What is the answer for the following code?\n", "\n", "def outerFun(a, b):\n", " def innerFun(c, d):\n", " return c + d\n", " return innerFun(a, b)\n", "\n", "res = outerFun(5, 10)\n", "print(res)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Question: What is the answer for the following code?\n", "\n", "def add(a, b):\n", " return a+5, b+5\n", "\n", "result = add(3, 2)\n", "print(result)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Question: What is the answer for the following code?\n", "\n", "def fun1(num):\n", " return num + 25\n", "\n", "fun1(5)\n", "print(num)\n", "# what is the problem?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Question: What is the answer for the following code?\n", "\n", "def fun1(name, age=20):\n", " print(name, age)\n", "\n", "fun1('Emma', 25)\n", "age1=0\n", "fun1('John', age1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Question: What is the answer fro the following code?\n", "\n", "def outerFun(a, b):\n", " def innerFun(c, d):\n", " return c + d\n", " return innerFun(a, b)\n", " return a\n", "\n", "result = outerFun(5, 10)\n", "print(result)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercise\n", "\n", "Write a function which takes:
\n", "* A list of 10 names\n", "* Another list of 10 numbers which represents the age of the corresponding people in the first list\n", "* Print out the name, the age, and a message that
\n", " if a person is above 50, the message is \"over the hill\",
\n", " else a message is \"has a bright future\"\n", "* You may need to look up the syntax for \"if... else\"\n", "\n", "Initialize the two lists, and then call the function." ] }, { "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 }