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.ipynb104
1 files changed, 104 insertions, 0 deletions
diff --git a/tutorials/module_1/notebook_1/functions.ipynb b/tutorials/module_1/notebook_1/functions.ipynb
new file mode 100644
index 0000000..28a9201
--- /dev/null
+++ b/tutorials/module_1/notebook_1/functions.ipynb
@@ -0,0 +1,104 @@
+{
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "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.). \\### 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.\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 \\## 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.\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."
+ ],
+ "id": "c549f9ae-7280-471e-a7e3-92d3340b7514"
+ }
+ ],
+ "nbformat": 4,
+ "nbformat_minor": 5,
+ "metadata": {}
+}