summaryrefslogtreecommitdiff
path: root/tutorials/module_1/notebook_1/functions.ipynb
blob: fe3317d5225c2b6e85e05c7311c84102374a0b28 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
{
 "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
}