# Functions

Functions are a way to organize pieces of similar or repeating code. Rather than having to constantly type out the same code, we can put the desired code in a function and call it whenever we want to use it.

## Creating a function

### Define your function - name & parameters

To make a function in python, we must use the def keyword. This keyword tells the code editor where the function is defined. def is followed by the name of the function. The name of the function is used to call your function from other parts of the program. In order to generalize the function (so that we can use it at multiple places), we might need to send some information to it. This is achieved using parameters. They are placed in between parentheses () and follows right after the name of the function. If no parameters are needed, they can be ignored. The function declaration ends with a colon. The statements that run each time the function is called follow on the next lines. Tabs are very important here, so make sure your code is indented properly. Every indented line after the function definition line (with the exception of comments) will be executed when you call the function.

Here’s what a function looks like:

def functionName(parameter1, parameter2, ...):
# write some code
# call this function from other parts
# of the code useing functionName


### Write the function body

In Python, indentation defines which code is controlled by the function. In the example below, in order to call the function sectionA, you must specify 2 parameters, startMeasure and endMeasure. When you call sectionA, the 3 fitMedia function lines are run. The 2 fitMedia function calls at the bottom are outside of the sectionA function body.

#Section A
def sectionA(startMeasure, endMeasure):
fitMedia(HOUSE_MAIN_BEAT_002, 4, startMeasure, endMeasure)

fitMedia(HOUSE_DEEP_CRYSTALCHORD_001, 3, 1, 5)
fitMedia(HOUSE_DEEP_CRYSTALCHORD_002, 3, 5, 9)


The code that is indented underneath the sectionA function is the code that is executed each time function sectionA is called.

## Returning a value

Many times you don’t want to just print the results or perform an action, but you want the function to provide you back with the result of performing the action. To do this, you use the return keyword.

def my_function(x):
return 5 * x

print(my_function(3)) # prints 15
print(my_function(5)) # prints 25


## Using the function - Function call

To call a function, input the name of the function and its parameters wherever you want to execute the function. For example, in order to call function sectionA, we would do something like this:

sectionA(1,9)


In this example, sectionA will be called with startMeasure value as 1 and endMeasure value as 9.

### Challenge 1 - Create your own function!

Challenge: Create a function called addNumbers that takes 2 parameters called number1 & number2 and prints the sum on console. Call the function addNumbers with parameters 3 & 5. What is printed on the console?

### Challenge 2

Let’s try to create some simple math functions. Create a function called add that takes in two parameters, number1 and number2, and return the sum of the two numbers.

To check that your function is working correctly, add the following code after your function definition, which calls the function add() and save its value in a variable. For example:

value2 = add(2, 3)
print("This should print 5: " + str(value2))


### Challenge 3

Can you create functions for subtraction, multiplication, and division?

Test the functions out by calling each function, saving its return value, then printing it out to the console.