Defining a FunctionDefining a function requires some specific actions just like defining a variable list. Let’s start by using an example of a function that will add two numbers that you input together.When telling the computer that what you’re about to define is a function, you first use the word “def”. This word is followed by the name of the function in all lowercase letters and words separated by underscores. Right after the name, you have a set of parenthesis that will have something or nothing in it depending on what you want your function to output. We’ll talk about that in more detail later. Finally at the end of that line you have a colon. This denotes that there is more code on following lines that is part of the function.

In the example above, you can see that I named my function “add_numbers”. Just like variables, make sure to give your functions name that tell you exactly what they do. This will make your code much more readable.In this function, there is something inside of the parenthesis. These are called function inputs. Since a function generally takes an input, does something to it, and then gives you an output, we need to give this specific function something to add. “number1” and “number2” are like variables that can only be used in this function. The inputs of a function are separated by commas.Writing the Body of the FunctionAfter defining the function, everything you code inside of it is just like what you would normally code. Just remember that the variable names you’re using will be the ones in the parenthesis.Since we’re writing a function that will add two numbers, we can define another variable inside the function called “total” that equals to the sum of “number1” and “number2”.

After writing the code that tells the function what you want to do with the input values, you’re almost done! In order for the function to give the final value back to you, we’ll need to use something called a return command.Returning a ValueThe point of a function is for it to do something with an input and give the programmer a final value, so we need a way to get that final value. A return command will take a designated value and give it back to the caller. This means when you call the function, it will automatically give you the final value of the selected inputs. Let’s see the return command in action with our adding numbers example to make it less confusing.

Since the final value you want to get from the function is the “total” variable, we’ll have the function return total. Now you’re done writing your function!​Calling a Function

Calling a function is how programmers refer to making a function actually do something. We can compare creating functions using kitchen utensils. Defining a function is like making a kitchen utensil. Now you have that tool at your disposal, but you’re not actually using it for anything yet. When you call a function, that’s when you’re actually using the utensil to cook. This means you can reuse the function for different inputs since you can use kitchen utensils to make various different meals.Now that the add_numbers function is created, let’s try calling it on the numbers 5 and 3. When calling a function, you just type the function name with the parenthesis containing the inputs. In this case, we’re replacing the inputs with actual numbers instead of the names “number1” and “number2”. This is kind of like defining a variable.

When you run your program like this, you’ll see that you don’t actually get an output on the command line. This is because the computer is running the program, but you didn’t have it show you the result. In order for us to see the result, we’ll put a print() function around the function we called to have the computer print the result of the function to the console.

Now we can see the output of the function!To recap, a general function needs these things: “def” to tell the computer that you’re creating a function, a function name, a set of parenthesis that may or may not contain input variables, the function body that tells it what to do with the inputs, and a return command that returns the final result.This add_numbers function is a very basic example of what functions can do. You can put just about anything inside of functions, which make them extremely crucial to programmers. Once you create more complicated functions, imagine the time you can save by just having to write it all out once and then calling it whenever you need it!