In this post, we’ll learn what Python function is and how to properly use it in practical Python code. We’ll be using a step by step approach using multiple practical code examples so you can have a complete idea of Python functions.
After reading this post, I’m confident that you’ll be able to use Python function in your own code with ease.
So let’s not delay anymore and just dive right into understanding it.
What is Python Function?
It is a group of statements that can be called again and again for some specific action. For instance, you want to perform a function of addition. If you don’t use functions, you will have to specify the code everytime you want to perform addition.
In functions, you just specify the logic once and can use the same function again and again by calling it.
It avoid bulkiness of code and make the code more organized and professional.
We’ll understand how to define and use Python function with the help of practical examples. But let’s first understand its syntax.
Syntax Of Python Function
def function_name(parameters): """doc string""" // optional actions to be performed return statement // optional
- It starts with the def keyword.
- Then comes the name of function. Make it unique for every function. funWork, name2, fun_work, name like these are valid function names. Don’t start the function’s name with digit as it would result in an invalid name like 3nameFun, 4FunName.
- Then we have a parameters section inside parenthesis() which can be used to pass values to function. These are optional.
- This line ends with a colon(:).
- Doc string is also optional. It can be used to describe the working of function. Proper indentation is required. Use tripe quotes as shown in the above example to be able to write this string in multiple lines.
- We can specify an action that we want to perform using this function’s body. Use proper indentation and start writing logic for the specific action.
- Return statement is also optional and is used to return the value from function.
Types of Python Functions
Actually, the functions in Python can be divided into 2 types. These are:
- Built-in Functions
- User defined Functions
We’ll explain everything about user defined functions as we want to create our own customized functions in Python. Below sections will properly explain user defined functions in detail.
Implementing Python Function (Multiple Examples)
See below practical code examples to properly understand how Python functions work.
Example 1: Function with no Parameters
def justFun(): """function with no parameters""" print('This is a function')
You can see that this function will print a simple statement. You can specify any action in it.
Example 2: Calling a Function
We’ll be calling the same above function. For that, we just have to use the function’s name. Some examples are listed below:
justFun() // if function has no parameters justFun(2) // if function has one parameter and you want to pass integer to it justFun(2,'green') // if function has 2 parameters and you want to pass an integer and string
So this is how you can call the function with its name and if it has some parameters then you have to pass them as an arguments while calling the function.
Let’s now call a function with no parameters.
def justFun(): """function with no parameters""" print('This is a simple function') justFun() // calling the function
This is a simple function
Example 3: Function with multiple Parameters
def justFun(a,b): """function of addition""" print(a+b) justFun(3,6)
You can see that the type of parameters will adapt the data type of values that are passed as an arguments to this function. If we’d have passed a string then the datatype of parameters would be set to string.
Call the function always after it is defined or else it will give an error.
Example 4: Use Return Statement in Function
As the name suggests, it is used to return something from the function from where it is called. Its syntax is simple, just use the return keyword before the value you want to return. See below examples:
def justFun(a,b): """function or addition""" c=a+b // store the added value in c return c // return the value stored in c returnedValue=justFun(5,6) // returned value will be store here print(returnedValue) // 11
You can see here that we have added the 2 parameters and store the value after addition in a new variable(c). After that, we’ve returned the value of c variable using the return statement.
Outside/after function, we made a call to the function with the required arguments and assign the value that it will return to a newly created variable.
Finally, we’ve printed its value which means the value is successfully returned from the function.
You can return a direct string or some other values as well. Some examples can be seen below:
return a+b // 11 return 'The value is '+str(a+b) // The value is 11
You can also use if statement as well to return a specific value or statement. See below code:
def justFun(a,b): """function or addition""" c=a+b if c>5: return 'Value is greater than 5' else: return 'Value is less than 5' returnedValue=justFun(5,6) print(returnedValue) // Value is greater than 5
This is how you can use an if else statement to return a specific value from Python function.
So this is how you can easily create and use Python function. Hope you like this post.
Feel free to ask questions related to the implementation and use of Python function. I’ll be very happy to answer all your questions.
As a conclusion of this post, hope you now have an in-depth practical understanding of what Python function is and how to properly define and use it. I’ll be happy to have your valuable thoughts. Thank you for reading this post.