In the previous session, you created a program that could find out if a number is a prime number or not. Can you use a function that can use the logic, take a number as a parameter, and return whether the function is a prime number or not? This way, to check if a number is a prime number, you will simply have to pass it through the function instead of writing the entire logic for it. You will now learn how to go about this.
Note: You can download the code used in the video from below:
Hi. In the last section you learned how functions work. In this lecture you will see some demonstrations of them. Earlier, you created a code to calculate prime numbers. Let's use the same code to create a function that returns the string prime if the number is a prime number and not prime if it is not. In my main method, I am first asking the user to enter the number that he or she likes to check if it's prime or not. Then I simply pass that number variable to my prime function. My prime function basically just checks if the number is prime or not. I'm not going to go through the entire logic since we have visited this code in the earlier lectures. Just remember, if prime check is equals to true, we are setting the result as prime. If it is not, we set the result as a string, not a prime. And at the end we just return the result. Now, once the prime function returns the string, we are storing it in the prime number variable. Then I'm just printing out the prime number variable. Let me run this code now.
Let's say I want to give twelve as a number. It said not a prime number. Let me try running this code again, this time with two. Two is a prime number. If you can see over here, I printed directly the prime number output. But what will happen if I just call the function directly inside my print statement? As you can see, it's going to print the same thing twice. So essentially, it's not really required for me to store the value returned by my function in a variable. Rather, I can simply call my prime function directly inside the print statement. You need to note that any change you make in the function is reflected in the final result. This is one advantage of using functions. Now suppose you have to call this function multiple times and you need to make a small correction in this function. You will simply make a correction in the function body which will get reflected in the program whenever it's called. Imagine if you were using the actual code you wrote earlier. You would have to type the entire thing again and again and then make these corrections each time. So say in the result I want to mention is a prime instead of just prime. So I simply made the changes in my prime function. I do not have to make any changes over here. Since we are simply calling my simply calling the prime function.
Let's run this code and you can see eleven is a prime number. These changes were reflected at the place where these functions were called.
The segment explains how to use functions in Python to calculate prime numbers and return a result.
The function checks if the entered number is prime or not, and returns "prime" or "not a prime" accordingly.
The segment shows how to call the function and store the result in a variable, or directly call the function inside a print statement.
The advantage of using functions is that any changes made in the function body will be reflected wherever the function is called.
The segment concludes by demonstrating how changes made in the function are reflected in the final result.
One of the major advantages of using functions is that if something goes wrong, you know where to make corrections that will be reflected in the entire program. Take two scenarios: one where you are checking prime numbers using the entire elaborate logic, let’s say, 10 times in different places, and you realise that you have made an error. Here, you will have to make corrections in each of these 10 places. However, if in the second scenario, you use a function, which essentially reduces the effort required, you can simply make the correction in the function body. It will be reflected in the results.
Till now, you saw examples where you took integers or strings as parameters. But you can even take arrays as parameters. Let’s create a function that takes an array as an argument and returns its maximum value.
Note: You can download the code used in the video from below:
So, you saw some examples where using functions reduced your effort and made your code efficient. In the next lecture, we’ll dive deeper into how functions work.