summaryrefslogtreecommitdiff
path: root/tutorials/module_1/functions.ipynb
diff options
context:
space:
mode:
Diffstat (limited to 'tutorials/module_1/functions.ipynb')
-rw-r--r--tutorials/module_1/functions.ipynb216
1 files changed, 216 insertions, 0 deletions
diff --git a/tutorials/module_1/functions.ipynb b/tutorials/module_1/functions.ipynb
new file mode 100644
index 0000000..6662537
--- /dev/null
+++ b/tutorials/module_1/functions.ipynb
@@ -0,0 +1,216 @@
+{
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "id": "8e2f3fed-9592-4c3a-a0a3-1d9c8d58244f",
+ "metadata": {},
+ "source": [
+ "# Functions\n",
+ "\n",
+ "Like a traditional mathematical functions, python functions can take an\n",
+ "input, process it, and give an output. In python, the input variables\n",
+ "are referred to as *arguments*. Functions are blocks of code that is run\n",
+ "every time it’s called. This allows us to re-use code.\n",
+ "\n",
+ "Functions are defined by using the <code> def </code> keyword. Reminder:\n",
+ "it is important to keep track of indentations as it signifies the end of\n",
+ "the function when the indentation returns back to the same level.\n",
+ "\n",
+ "## Defining Functions\n",
+ "\n",
+ "### Simple function\n",
+ "\n",
+ "A simple function with no input variable can be useful if you need to\n",
+ "re-use code multiple times without having to re-write it."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 1,
+ "id": "b673514c-db14-4da7-8f6a-65402b27c3c6",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "def function_name():\n",
+ " print(\"This is from a function\")"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "b7cca02d-2d53-4acd-ada5-cb8221008b52",
+ "metadata": {},
+ "source": [
+ "### Defining a function with one input\n",
+ "We can pass variables through to the function to be processed as\n",
+ "follows:"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 2,
+ "id": "89f84f81-461a-42e3-9e7a-a6be2ac8c4d7",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "def function(x):\n",
+ " print(x + \" is best\")"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "0d0d30cb-83d1-438f-85b0-c5e3e49eff29",
+ "metadata": {},
+ "source": [
+ "Note input variables can be of any data type (integer, float, string,\n",
+ "etc.). \n",
+ "\n",
+ "### Returning values from a function If we want to calculate a\n",
+ "value and pass it back to the script for further use, we can use the\n",
+ "`return` keyword. Let’s define a linear function that takes two inputs,\n",
+ "`x` and `b`, computes the corresponding `y` value, and returns it so it\n",
+ "can be used elsewhere in the code."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 3,
+ "id": "5f2ff033-050c-4939-bab4-f55ec0de688b",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "def function(x, b):\n",
+ " y = 3*x+b\n",
+ " return y"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "a2ad94cf-6e16-4186-874c-972d6932858a",
+ "metadata": {},
+ "source": [
+ "For multiple output variables we can add \n",
+ "\n",
+ "## Calling functions Now that\n",
+ "we’ve covered defining functions we want to call the function in order\n",
+ "to execute the block inside the function. To do this, we simply re-call\n",
+ "the function name as follows."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 4,
+ "id": "4bff7ef2-630f-43d3-b54a-cc8214aa7034",
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "5"
+ ]
+ },
+ "execution_count": 4,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "function(2,-1)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "caeeca6c-3442-4785-9d54-e39801ee3cf6",
+ "metadata": {},
+ "source": [
+ "Note that when running this code, nothing happens. This is because we\n",
+ "haven’t told the computer what to do with the output. Hence, if we wish\n",
+ "to store the output then we need to use the assign operator `=`."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 5,
+ "id": "b47437eb-050d-481e-a72b-201a7703b193",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "5\n"
+ ]
+ }
+ ],
+ "source": [
+ "output = function(2,-1)\n",
+ "\n",
+ "print(output)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "8cd86a50-5dc6-42df-b651-a147b445cb57",
+ "metadata": {},
+ "source": [
+ "In case you want to return multiple output variable from a single\n",
+ "function we will have…\n",
+ "\n",
+ "## Summary"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 6,
+ "id": "d22da5ff-85ee-4658-a959-1a2b5df6eb9a",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "def function_name(argument1, argument2, argument3):\n",
+ " output1 = argument1 * argument2 - argument3\n",
+ " output2 = argument2 + argument3\n",
+ " return output1, output2\n",
+ "\n",
+ "[solution1, solution2] = function_name(1,2,3)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "bd984f25-729f-437c-8df4-0feb75aa7ed8",
+ "metadata": {},
+ "source": [
+ "- `def` - defines a function. All the code that is indented underneath\n",
+ " is considered inside the function block.\n",
+ "- `function_name` - this is used to call the function block.\n",
+ "- `argument1` (optional) - input variable. This is data that can be\n",
+ " pass to the function. It is possible to have multiple variables\n",
+ " separated by a comma. As well as can be omitted if the function\n",
+ " should just give you an output such as.\n",
+ "- `return` (optional) - if you wish to return something to your\n",
+ " script, the return keyword is used. The keyword can be followed by\n",
+ " an output variable or a constant. For multiple output variables,\n",
+ " separate them by a comma."
+ ]
+ }
+ ],
+ "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.13.2"
+ }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 5
+}