What if you want to perform the same set of instructions over and over again, but using different numbers? A function is a way to group together code that performs an action you want to do over and over again. For example, if we were making a calculator program, we might have a function called exponent to raise a number to an exponent. Any time you want to use exponent, you can just call exponent on the numbers you want, without having to copy and paste all of the code in exponent. Here’s how you define a function:def functionName( ):# Put your function’s code hereThe parentheses next to the function’s name are a place to put in parameters; these are the variables that the function uses in its code. Since our function has nothing in the brackets, it does not take in any parameters. Let’s say we decide to implement our exponent function. Now, we need parameters: we need the number that will be raised to the exponent (the base) and the exponent itself. Here’s what the function would look like:

Remember, ** is the exponentiation operator in Python (2**3 = 23 = 8). This function takes in the base and exp as its arguments, uses them in its code and prints out an answer. Arguments are the actual values of the parameters passed in. For example, this is how you would call the exponent function:

Looking at the first function call, base and exp are the parameters, but 2 and 3 are the arguments. Similarly, in the second one, base and exp are the parameters and 8 and 2 are the arguments. So you can see how functions can be reused on any arguments you want. ​What if you want to use the value produced by your function in another function? You can use return to pass back a value to where the function was called from. For example, if you have a function called calculator, and you want to call the exponent function from it and have it return the answer back to calculator, you would do the following:

What’s happening? The three lines are the same exponent definition, but now we have the new calculator function. It takes in no arguments, calls exponent with the arguments 4 and 2, saves the returned value in the variable answer and then prints answer.