{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Math with Python - Functions\n", "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Reusing code\n", "\n", "Suppose we want to try out several values for a given expression (eg. 5*x + 11 - x/2).\n", "\n", "We could copy the first statement multiple times and replace with each value we want to evaluate. \n", "What if we want to change the expression?\n", "We should apply the change to all copies respectively.\n", "\n", "#### Can we do better?\n", "We could wrap the expression in a function.\n", "\n", "#### What is a function?\n", "You can think of a function as a process which receives some input and produces some output. Inputs to a function are called ***arguments*** and the output product is what the function ***return***s.\n", "\n", "In fact you allready used the built-in function `print()`." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\n", "## How do we define a function?\n", "\n", "In Python a function is defined with the `def` keyword that marks the start of the function header.\n", "\n", "We then define a unique function name in the same way as variables,\n", "enclose the input parameters `(parameter1, parameter2, ...)` in parenteheses \n", "and put a colon `:` to mark the end of the function header.\n", "\n", "All statements that define the function body should be equally indented (4 spaces are commonly used, the editor should handle this automatically)\n", "\n", "At the end, the return keyword (optional) gives the function output (if any).\n", "\n", "Let's see how we can define a function that multiplies a given variable by 3:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "def triple(x): # x is the input number to be tripled\n", " product = 3 * x \n", " return product # this is what the function outputs" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now let's use this function to produce some results" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "15\n" ] } ], "source": [ "tripled = triple(5)\n", "print(tripled)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can also pass a variable as parameter to a function" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "15\n" ] } ], "source": [ "test = 5\n", "tripled = triple(test)\n", "print(tripled)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now let's try several values by wrapping an expression into a function" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "def my_function(x):\n", " return 5*x + 11 - x/2" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "29.0\n", "38.0\n", "47.0\n", "51.5\n", "56.0\n" ] } ], "source": [ "result = my_function(4)\n", "print(result)\n", "result = my_function(6)\n", "print(result)\n", "result = my_function(8)\n", "print(result)\n", "result = my_function(9)\n", "print(result)\n", "result = my_function(10)\n", "print(result)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### What if the expression contains more than one variables?\n", "\n", "No problem, we can put as many as we need.\n", "\n", "Let's take for instance (x*3 + y/2 - 9):" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "def my_function2(x, y):\n", " return x*3 + y/2 -9" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "7.0\n", "-5.5\n", "123.0\n", "-306.0\n" ] } ], "source": [ "print(my_function2(4, 8)) # functions can also be nested\n", "print(my_function2(1, 1))\n", "print(my_function2(40, 24))\n", "print(my_function2(-100, 6))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Try it out!\n", "Use the cell bellow to write your own function.\n", "\n", "Call this function multiple times with deifferent parameters and print the output.\n", "\n", "Once you are ready, click on `Kernel` tab > `Restart & Run All`" ] }, { "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": 4 }