What is the one thing that can make your programs more elegant and structured? It sure indeed is a function in a programming language.
Welcome to this article on one of the most important aspects of any programming language and that is “Functions” and more specifically we will be learning about functions using a programming language called “python”.
In this article, we are going to learn everything that you need to know about python functions.
We will be answering many questions for python function such as:-
- What is a function in Python?
- How do you call a function in Python?
- Some Examples of Functions in Python?
- Difference between User-defined functions and Inbuilt (default) function in Python
- What are lambda functions in Python?
- What are def functions in Python?
- And many more such questions will be answered in this article.
With each question, we will be taking some coding examples in python, so that the concepts get really deep into your mind.
I hope you are as excited as I am. So, without further ado, let’s get right into the article.
Python Functions: The Definitive Guide 👌
So, let us first start this article by asking a question.
What do we mean by a general function? Not a python function but a general function.
A general programming function is a piece of code that captures a certain task. Usually, it is used because this particular task is required at many places in the program.
For example, let us assume that a program requires the use of printing again and again. Since this task is a repetitive task, a function with a name “print” can be made which is called whenever there is a need to print something. It turns out that almost every programming language has one function for printing.
Because programmers in the past felt that this function is used almost innumerable times, so they defined this default print function for printing things onto the screen.
Just like this, there can function for all sorts of other tasks like there can be a function for taking the square root of a number or taking the cube of a number etc.
Okay. This was the definition of a general function. Then, what is a python function?
Similar to a general function, a function that is used in a python programming language will be known as python function. Let us now take some examples of some python functions.
Python “print” function
If you want to use the print function, then first you need to open the IDLE python shell. Once this is opened, type the above commands one by one to see the results. Here, I have used the print function with an integer 4, string “Hello, Everyone, float 12.00” and a variable called “name” which stores a string called “Rahul”.
There are two important concepts that we need to understand to effectively understand the concept of functions in python.
- Function Definition: Definition of a function is a step where the function is declared and the code is written for the corresponding function.
- Function Call: Calling a function means to use the function. What we have done above with print is to call the print function.
Then, where is the function definition of the print function. It turns out that this function has already been defined for you to use (call). There is no need for you to define this function again. They are available for you to use (call) without defining them.
There is a special name for these types of functions and that is DEFAULT (INBUILT) FUNCTIONS. These are those functions that are already there in python (they are defined by someone else) and we can call these functions for our use.
There is one more type of function and that is known as “USER DEFINED FUNCTIONS”. The functions defined by and called by us (programmers) are known as User-defined functions. We will talk more about them in a minute but let us talk about more default functions that are there in python.
Python “max” and “min” function
Here, we defined a list of marks and then used an inbuilt function for calculating the maximum and minimum of the list values.
Now, let us talk about one important aspect of functions called “arguments”.
Arguments are those values or variables which are passed to the function name while calling the function.
In the call of print, we passed the value or variable that we wanted to print on the screen. So, those values or variables will be called as arguments. Similarly, while calling the max and min functions, we passed a list.
Hence, in this case, that list will be an argument to the function. Till now, we have used only those functions for which number of arguments were one. But this is not necessary, there can be a function with more than one argument.
Note here that a function is called by writing the name and then pair of parentheses inside which we need to pass required arguments. The following screenshot will make it more clear.
Okay, so far we have discussed python functions, how can we call them and inbuilt (default) python functions. Now, we will spend some time on User Defined Function in python.
User-Defined Python Functions
Those functions which are defined by the user (programmers) of the programming language will be known as User Defined Functions.
The question arises why do the programmers need to declare their own functions when there are so many default functions available to them.
This is because many times those functions may not fulfill the need of the programmer for the current project. This is why they might be interested in declaring (defining) their own custom functions which they can use (call) according to their needs.
Okay, now that we understand what and why of User Defined Functions, let’s talk about how to create those types of functions in python.
There are actually two different ways of defining the User Defined Functions in Python and they are:
- Using the lambda keyword in python.
- Using the def keyword in python.
Both have their own advantages and disadvantages at different times. The lambda functions are also known as one-line-functions or anonymous functions as they can be embedded somewhere as a one-line or as a short concise piece of code.
But on the other hand, if your function is doing a lot of different things, then defining them using the lambda keyword is probably not a good idea.
First of all, to understand how to declare functions using each of these keywords, I would like to take an example and then implement that using both keywords.
Let us say, we want to create a function called greeting which takes one argument called name and then returns a greeting message. (Here, do not worry about what return means, it will be clear in a moment when we define the function).
First, let us look at the way to define the function using the def keyword. To declare the function, I have created a script by hitting CTRL + N and then saving the file as functions.py.
Let us understand how the function has been created.
- First, we start by writing the “def” keyword which tells the python that we are going to define our User Defined Function.
- Then, we write down the name of the function that we want to call our function and it can be anything you like. Here, I have decided that I will call the function as
- Then, start the opening parentheses followed by the argument list separated by comma. Here, I had only one argument, so we will just stick to the one argument and then close the paremtheses.
- Finally, we put “:” (colon) and hit enter and write down all the code that will be part of the function. In our function, we have two lines followed by the colon.
- First, we make a new variable called message which just adds welcome in the front of the name and then in the second line we return this message.
A function may or may not return a value. If it returns a value, then it’s good but it doesn’t then by default every function in python returns None.
Now, that our first User Defined Function is ready, it’s time to use (call) this function. Let’s do that.
First, run the script by hitting the key F5. Then, you will be in the shell where you can use (call) this newly created function. The following shows the result after calling this function many times….
As you can see from the above screenshot that we can just call (use) our User Defined Function just the way we could use any default (inbuilt function).
We can only pass one argument to the greeting function as we have explicitly specified this when defining the function that it can take only one argument called “name”. Let us pass more than one argument and try to see what error we get…
Here we notice that we passed two arguments “Rahul” and 12 and the function threw an error saying that greeting() takes one argument but 2 were given.
So, with this I hope you now understand how to define a User Defined Function using the keyword def.
User-Defined Python Function using lambda keyword
Let us first quickly see how to define the function using the lambda keyword and then we will break that down to understand how that works.
Let us understand how does that work:
- First, we start by writing down the name of the function which is greeting in our case.
- Then, we assign that to a special sequence of characters and combinations. First, we write the keyword lambda which tells the python that we are defining a lambda function and the space.
- After that, we specify the name of the argument which in our case is “name”.
- Then, we specify the colon, and anything after the colon in the lambda function will be returned. Hence, there is no need to explicitly specify the return keyword here. It is understood.
Let’s again run the script and test our greeting function a bunch of times.
So, the output is similar to what we got from def keyword.
Okay, with this we have also covered how to define the User Defined Function in Python and we have reached the end of this article. But before doing that, i would like to talk about what are all the things that we covered in this article.
- What are functions?
- What are python functions?
- How do we use (call) python functions?
- What are arguments to python functions?
- Difference between User Defined Functions and Inbuilt Default functions.
- How to define user-defined functions using the def and lambda keyword?
- Some examples of functions in python and some examples using the def and lambda keywords.
I recommend all of you reading this article to explore more about python functions and I have some tasks for you to try out:
TASKS FOR YOU
Task. 1 Explore pow, sum, len, range, dict etc inbuilt python functions.
Task. 2 Define a Function that will take the name and message arguments and it will return the name with the message using the def keyword.
Task. 3 Define a function that will display the first 10 multiples of a number which will be the argument of the function. Call this function table.
Now it’s time to explore our video tutorial.
So, I hope you will complete these tasks and further I hope that you have been able to follow through with whatever I discussed in this particular article. Bye for now.
Have a nice day.
Thank you. 🙂