Hello and Welcome to Blog #9, Python: Functions. This blog will introduce the concept of functions in python, and how to create and use functions.
Functions don't just come in programming languages. Functions come in math as well. Functions in Math are similar to the ones in coding languages. A function, basically, carries out a specific task as part of a program. In a program, you can have as many functions as you want or need to complete your task.
A function in programming takes in inputs, although it does not necessarily have to, and gives an output. You can use functions to split your code into sets of tasks. Each function in your program will carry out a specific task, and in the end, you can use all the outputs to carry out a bigger task.
Next, how do you define a function. To define a function, you use def(). Inside the parentheses, you put in what are called, parameters. These are like the inputs. You can have as many parameters as you need to, as long as, every time you use, or call, the function, you define all the parameters. Here is how it looks:
def(*parameters, separated by commas*):
*The code that the function will carry out*
Inside the function, the parameters passed to the function are used to carry out the task. You should try to only pass as many parameters as you need to accomplish your goal. Only pass the parameter, if it plays a role in the task that the function will do, you can't finish the task without the parameter, or, using the parameter makes the code a lot simpler.
Next, at the end of the function, you can add, 'return,' or 'return *something*'
This is essentially the output line. As you can see, you can either just 'return', or you you can return something. When you just return, it means the function is done, and it will go back to the line in your program you called the function on. When you return something, that something is basically the output of your program. Meaning, if you called the function to find and change the value of a number, and you passed the existing value of the number to the function as a parameter, then the function will process. It will run the lines of code in it, and at the end, you would return the new value of the number. This means that the new value of the number will be sent back to where the function was called, and the old value is replaced.
Finally, how do you call a function. This is simple. Say you have a function called, testFunction. You would define the function:
def testFunction():
You would define parameters if needed, which we don't in this case, as this is an example. Then you would write the code in the function, indented by four spaces:
def testFunction(*parameters*):
*code*
And finally you might return:
def testFunction(*parameters*):
*code*
return
Now, to call the function, and run the code it contains:
testFunction(*parameters*)
That is how the function is called. Note that we don't put a colon after the line. Also, this is very important, the parameters you pass when you call the function must be the same number of parameters you passed when defining it. Like this:
def testFunction(number):
*code*
return
Here, the variable, number, is in the parameters, meaning that 'numbers' is something used in this function. When calling the function, you must pass in a value for number:
testFunction(1)
This will run the function using number's value as 1. It will use 'number's' value as 1, and run the code.
These are the basics of functions and how they work. Thank you for reading, Python: Functions. Please leave any comments or questions in the comments section!
Comments
Post a Comment