How to Define a Function in Python?
To tell the computer you would like to create a Python function, just write def at the beginning of the line. Def is neither a command nor a function. It is a keyword. To indicate this, Jupyter will automatically change its font color to green. Then, you can type the name of the function you will use. (To learn how to navigate through the Jupyter dashboard, check out our tutorial The Jupyter Dashboard – A Walkthrough.)
How to Create a Function with a Parameter?Our next task will be to create a Python function with a parameter. Let it be “plus ten” with a parameter ”a”, that gives us the sum of “a” and 10 as a result… Always begin with the “def” keyword. Then, type the name of the function, “plus ten”, and in parentheses, designate the parameter “a”. The last thing to write on this line would be the colon sign.
People often confuse print and return, and the type of situations when we can apply them. To understand the concept better, try to imagine the following.There is an argument x, which serves as an input in a function, like the one we have here. The function, in this case, is x plus 10. Given that x is an input, we can think of it as a value we already know, so the combination of x and the function will give us the output value y. Well, in programming, return regards the value of y; it just says to the machine “after the operations executed by the function f, return to me the value of “y”. “Return” plays a connection between the second and the third step of the process. In other words, a function can take an input of one or more variables and return a single output composed of one or more values.
This is why “return” can be used only once in a function.Therefore, we can say the concept of a function applies to programming almost perfectly.
Is There Another Way to Define a Function?There is another way in which you could organize the definition of your function. Start by defining “plus ten” with an argument of “a” and a colon. On the next line, instead of directly returning the value of “a” plus 10, another variable can be created inside the function to carry that value. I will use the name “result” here. I will assign it with the desired value of “a” plus 10.
Let’s check what we just did.If I execute the code in the cell, I will obtain nothing. Why? Because to this moment, I have only declared the variable “result” in the body of our function.
“Print” takes a statement or, better, an object, and provides its printed representation in the output cell.It just makes a certain statement visible to the programmer. A good reason to do that would be when you have a huge amount of code, and you want to see the intermediary steps of your program printed out, so you can follow the control flow. Otherwise, print does not affect the calculation of the output. Differently, return does not visualize the output. It specifies what a certain function is supposed to give back. It’s important you understand what each of the two keywords does. This will help you a great deal when working with Python functions.
The following could be helpful.Let that same function also print out the statement “outcome”. If we put down only “return outcome”, and then “return result”, what will we get when we call the function? Just the first object to return – the statement “outcome”.
How to Use a Function in another Function?It isn’t a secret we can have a function within the function (which is way more useful than a picture within a picture). For instance, let’s define a function called ‘wage’ that calculates your daily wage. Say you use working hours as a parameter, and you are paid 25 dollars per hour.
Notice, I don’t technically need the print command here.I could print out the wage afterwards, but I don’t really need to. So, I’ll proceed this way, just returning the value I need.
How to Combine Conditional Statements and Functions?We already know how to work with if statements, and how to work with Python functions. In this post, we’ll learn how to combine the two. This is a fundamental concept in programming, so please pay attention! You’ll encounter it quite regularly when coding. Johnny’s mom told him that, by the end of the week, if he has saved at least 100 dollars, she would give him an extra 10 dollars. If he does not manage to save at least 100 dollars, though, she would prefer not to give him the extra cash. Clear. Now, let’s define a function called “add 10”, which takes as a parameter the unknown “m” that represents the money Johnny saved by the end of the week.
What should we ask the computer to do?If “m” is greater than or equal to 100, then add 10 to the saved amount. If it is not, return a statement that lets us know Johnny should save more. That is, if “m” is greater than or equal to 100, let “m” assume the value of “m” plus 10.