summaryrefslogtreecommitdiff
path: root/tutorials/module_1/notebook_1/functions.ipynb
diff options
context:
space:
mode:
Diffstat (limited to 'tutorials/module_1/notebook_1/functions.ipynb')
-rw-r--r--tutorials/module_1/notebook_1/functions.ipynb128
1 files changed, 0 insertions, 128 deletions
diff --git a/tutorials/module_1/notebook_1/functions.ipynb b/tutorials/module_1/notebook_1/functions.ipynb
deleted file mode 100644
index fe3317d..0000000
--- a/tutorials/module_1/notebook_1/functions.ipynb
+++ /dev/null
@@ -1,128 +0,0 @@
-{
- "cells": [
- {
- "cell_type": "markdown",
- "id": "cb3c1386-3242-4781-8e63-bc304b7337d7",
- "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.\n",
- "\n",
- "``` python\n",
- " def function_name():\n",
- " print(\"This is from a function\")\n",
- "```\n",
- "\n",
- "### Defining a function with one input\n",
- "\n",
- "We can pass variables through to the function to be processed as\n",
- "follows:\n",
- "\n",
- "``` python\n",
- " def function(x):\n",
- " print(x + \" is best\")\n",
- "```\n",
- "\n",
- "Note input variables can be of any data type (integer, float, string,\n",
- "etc.).\n",
- "\n",
- "### Returning values from a function\n",
- "\n",
- "If we want to calculate a value and pass it back to the script for\n",
- "further use, we can use the `return` keyword. Let’s define a linear\n",
- "function that takes two inputs, `x` and `b`, computes the corresponding\n",
- "`y` value, and returns it so it can be used elsewhere in the code.\n",
- "\n",
- "``` python\n",
- " def function(x, b):\n",
- " y = 3*x+b\n",
- " return y\n",
- "```\n",
- "\n",
- "For multiple output variables we can add\n",
- "\n",
- "## Calling functions\n",
- "\n",
- "Now that we’ve covered defining functions we want to call the function\n",
- "in order to execute the block inside the function. To do this, we simply\n",
- "re-call the function name as follows.\n",
- "\n",
- "``` python\n",
- "function(2,-1)\n",
- "```\n",
- "\n",
- "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 `=`.\n",
- "\n",
- "``` python\n",
- "output = function(2,-1)\n",
- "\n",
- "print(output)\n",
- "```\n",
- "\n",
- "In case you want to return multiple output variable from a single\n",
- "function we will have…\n",
- "\n",
- "## Summary\n",
- "\n",
- "``` python\n",
- "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)\n",
- "```\n",
- "\n",
- "- `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
-}