summaryrefslogtreecommitdiff
path: root/tutorials/1_functions.md
diff options
context:
space:
mode:
authorChristian Kolset <christian.kolset@gmail.com>2025-03-11 22:52:28 -0600
committerChristian Kolset <christian.kolset@gmail.com>2025-03-11 22:52:28 -0600
commita05b8ed78049796abe23260ff54c9649e5f72637 (patch)
treec4cc181bc039337210e971ce0c01eb9c253b501b /tutorials/1_functions.md
parent0e3914e32e399f0051c196ab8c5da95f91044406 (diff)
Added text to functions tutorials and added a summary section at the bottom.
Diffstat (limited to 'tutorials/1_functions.md')
-rw-r--r--tutorials/1_functions.md51
1 files changed, 41 insertions, 10 deletions
diff --git a/tutorials/1_functions.md b/tutorials/1_functions.md
index e4c9874..edbd3a2 100644
--- a/tutorials/1_functions.md
+++ b/tutorials/1_functions.md
@@ -1,29 +1,60 @@
# Functions
-Like a traditional mathematical functions, python functions can take an input, process it, and give an output. Functions are blocks of code that is run everytime it's called. This allows us to re-use code.
+Like a traditional mathematical functions, python functions can take an input, process it, and give an output. In python, the input variables are referred to as *arguments*. Functions are blocks of code that is run every time it's called. This allows us to re-use code.
-Functions are defined by using the <code> def </code> keyword. Reminder: it is important to indent all code inside the function to signify the end of the function.
+Functions are defined by using the <code> def </code> keyword. Reminder: it is important to keep track of indentation as it signify the end of the function when the indentation.
-## Creating a function with no input
-This is useful if you need to re-use code without having to copy paste the block.
+## Defining Functions
+### Simple function
+A simple function with no input variable can be useful if you need to re-use code multiple times without having to re-write it.
-```
+```python
def function_name():
print("This is from a function")
```
-## Creating a function with one input
+### Defining a function with one input
+We can pass variables through to the function to be processed as follows:
-```
+```python
def function(x):
print(x + " is best")
```
-## Returning values from a function
-Let's make a
+Note input variables can be of any data type (integer, float, string, etc.).
+### Returning values from a function
+If we want to calculate a value and pass it back to the script for further use, we can use the `return` keyword. Let's define a linear function that takes two inputs, `x` and `b`, computes the corresponding `y` value, and returns it so it can be used elsewhere in the code.
-```
+```python
def function(x, b):
y = 3*x+b
return y
```
+
+For multiple output variables we can add
+## Calling functions
+Now that we've covered defining functions we want to call the function in order to execute the block inside the function. To do this, we simply re-call the function name as follows.
+
+```python
+function(2,-1)
+```
+
+Note that when running this code, nothing happens. This is because we haven't told the computer to print the result.
+
+
+## Summary
+
+```python
+def function_name(argument1, argument2, argument3)
+ output1 = argument1 * argument2 - argument3
+ output2 = argument2 + argument3
+ return output1, output2
+
+[solution1, solution2] = function_name(1,2,3)
+```
+
+
+- `def` - defines a function. All the code that is indented underneath is considered inside the function block.
+- `function_name` - this is used to call the function block.
+- `argument1` (optional) - input variable. This is data that can be pass to the function. It is possible to have multiple variables separated by a comma. As well as can be omitted if the function should just give you an output such as.
+- `return` (optional) - if you wish to return something to your script, the return keyword is used. The keyword can be followed by an output variable or a constant. For multiple output variables, separate them by a comma. \ No newline at end of file