Now inside the body function place, y = x+10 and then return y on the next line. Now, this basically means that when we call for this function we have to put in th is parameter or argument in here that basically says we need a value for x. I n this example, we are going to create a function by the name “add10” and then pass it one parameter x in between the parentheses. So it’s important to make sure that if you’re always returning something from your function. In order to actually get some output when we run this function, we should put a print statement around where we are calling myfunction(). Now to return something out of our Python function, we simply type the word “return” and then whatever it is, we are returning, in this case, we are returning ”name”. Now if we run this nothing will happen because we have removed the print statement. Instead, we are going to assign the string “ this is my function” to the variable “name”. To demonstrate that we are going to take away the print statement from the body of the function. We can also use a return statement to return things out of our Python function. It prints out ”this is in my function” and there we go: To call a function in python type the name of the function followed by parentheses which just has this one line of code in. If we save this program and run it we will not get any output because we have not yet called the function. Inside the body of the function, we are going to print out “this is my function”. It is important to note that after we hit enter everything that happens in this function has to be indented. After the name of the function and the parentheses, we should do a full colon. In this article, we are going to name the Python function myfunction because it is just an example. When coming up with a name for a function we should always use something that makes sense or and kind of fits in with what your code is doing. In Python, a function is created using the keyword “ def” followed by the name of the function and a pair of parentheses. A Python function is essentially a piece of code that we assign a name and we can use over and over again in the rest of our program.
0 Comments
Leave a Reply. |