What is a Python function?
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.
Creating a Python function
In Python, a function is created using the keyword “def” followed by the name of the function and a pair of parentheses. 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. This makes your code more readable.
In this article, we are going to name the Python function myfunction because it is just an example. After the name of the function and the parentheses, we should do a full colon. It is important to note that after we hit enter everything that happens in this function has to be indented.
Inside the body of the function, we are going to print out “this is my function”. If we save this program and run it we will not get any output because we have not yet called the function. To call a function in python type the name of the function followed by parentheses which just has this one line of code in. It prints out ”this is in my function” and there we go:
We can also use a return statement to return things out of our Python function. To demonstrate that we are going to take away the print statement from the body of the function. Instead, we are going to assign the string “this is my function” to the variable “name”.
Now if we run this nothing will happen because we have removed the print statement. 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”.
In order to actually get some output when we run this function, we should put a print statement around where we are calling myfunction(). So it’s important to make sure that if you’re always returning something from your function.
In this example, we are going to create a function by the name “add10” and then pass it one parameter x in between the parentheses. Now, this basically means that when we call for this function we have to put in this parameter or argument in here that basically says we need a value for x.
Now inside the body function place, y = x+10 and then return y on the next line. This function takes the value that we pass to it and then adds a 10 to it. Finally, print out the function add10() and then pass an argument inside the parentheses, in this case, we are going to pass 5.
So when we run this it is going to execute the function, take the 5 as x and then apply it and come out of the other end we get 15. As shown in the code below.
In this example, we are going to write a function that splits a sentence into words and returns how many words were in that sentence. We are going to define a function by the name splitandcount() and then we’re going to pass a parameter by the name “sentence”.
Now in the body of the Python function we are going to declare a variable s and assign it a method split() as shown here s= sentence.split().
We are now going to return the length of s. Now when we split a sentence or a string it splits it into words, individual words And then the length is basically the length of the list, s.
We can now go ahead and print the splitandcount(“hello welcome to codeberry”) and now when we run this we should get four out of there.
In our Programming Tutorials series, you’ll find useful materials which will help you improve your programming skills and speed up the learning process.
- How to create perfect HTML tables?
- HTML color codes
- CSS background images
- Best Programming Books You Must Read in 2021
- +10 Best Data Science Courses, Data Scientist Salaries, and Everything About DS
- Python for loop
- Creating a table using HTML and CSS
- Best way of using Java Arrays and ArrayLists
- Best way of using Python Sets
- Best ways of using a Python Dictionary
- Best way of using Python Classes
- Best way of using Python Range
- Best way of using Python if-else
- Best way of using Python RegEx
- Best way of using Python Lists
- Best way of using Python Enumerate
- Best way of using Python Functions
Would you like to learn how to code, online? Come and try our first 25 lessons for free at the CodeBerry Programming School.