MBAData Science & AnalyticsDoctorate Software & Tech AI | ML MarketingManagement
Professional Certificate Programme in HR Management and AnalyticsPost Graduate Certificate in Product ManagementExecutive Post Graduate Program in Healthcare ManagementExecutive PG Programme in Human Resource ManagementMBA in International Finance (integrated with ACCA, UK)Global Master Certificate in Integrated Supply Chain ManagementAdvanced General Management ProgramManagement EssentialsLeadership and Management in New Age BusinessProduct Management Online Certificate ProgramStrategic Human Resources Leadership Cornell Certificate ProgramHuman Resources Management Certificate Program for Indian ExecutivesGlobal Professional Certificate in Effective Leadership and ManagementCSM® Certification TrainingCSPO® Certification TrainingLeading SAFe® 5.1 Training (SAFe® Agilist Certification)SAFe® 5.1 POPM CertificationSAFe® 5.1 Scrum Master Certification (SSM)Implementing SAFe® 5.1 with SPC CertificationSAFe® 5 Release Train Engineer (RTE) CertificationPMP® Certification TrainingPRINCE2® Foundation and Practitioner Certification
Job Linked
Study Abroad
Master of Business Administration (90 ECTS)Master of Business Administration (60 ECTS)Master in Computer Science (120 ECTS)Master in International Management (120 ECTS)Bachelor of Business Administration (180 ECTS)B.Sc. Computer Science (180 ECTS)MS in Data AnalyticsMS in Project ManagementMS in Information TechnologyMasters Degree in Data Analytics and VisualizationMasters Degree in Artificial IntelligenceMBS in Entrepreneurship and MarketingMSc in Data AnalyticsMS in Data AnalyticsMaster of Science in AccountancyMS in Computer ScienceMaster of Science in Business AnalyticsMaster of Business Administration MS in Data ScienceMS in Information TechnologyMaster of Business AdministrationMS in Applied Data ScienceMaster of Business AdministrationMS in Data AnalyticsM.Sc. Data Science (60 ECTS)Master of Business AdministrationMS in Information Technology and Administrative Management MS in Computer Science Master of Business Administration MBA General Management-90 ECTSMSc International Business ManagementMS Data Science MBA Business Technologies MBA Leading Business Transformation Master of Business Administration MSc Business Intelligence and Data ScienceMS Data Analytics MS in Management Information SystemsMSc International Business and ManagementMS Engineering ManagementMS in Machine Learning EngineeringMS in Engineering ManagementMSc Data EngineeringMSc Artificial Intelligence EngineeringMPS in InformaticsMPS in Applied Machine IntelligenceMS in Project ManagementMPS in AnalyticsMS in Project ManagementMS in Organizational LeadershipMPS in Analytics - NEU CanadaMBA with specializationMPS in Informatics - NEU Canada Master in Business AdministrationMS in Digital Marketing and MediaMS in Project ManagementMaster in Logistics and Supply Chain ManagementMSc Sustainable Tourism and Event ManagementMSc in Circular Economy and Sustainable InnovationMSc in Impact Finance and Fintech ManagementMS Computer ScienceMS in Applied StatisticsMS in Computer Information SystemsMBA in Technology, Innovation and EntrepreneurshipMSc Data Science with Work PlacementMSc Global Business Management with Work Placement MBA with Work PlacementMS in Robotics and Autonomous SystemsMS in Civil EngineeringMS in Internet of ThingsMSc International Logistics and Supply Chain ManagementMBA- Business InformaticsMSc International ManagementMS Computer Science with AIML ConcentrationMBA in Strategic Data Driven ManagementMaster of Business AdministrationMSc Digital MarketingMBA Business and MarketingMaster of Business AdministrationMSc Digital MarketingMSc in Sustainable Luxury and Creative IndustriesMSc in Sustainable Global Supply Chain ManagementMSc in International Corporate FinanceMSc Digital Business Analytics MSc in International HospitalityMSc Luxury and Innovation ManagementMaster of Business Administration-International Business ManagementMS in Computer EngineeringMS in Industrial and Systems EngineeringMSc International Business ManagementMaster in ManagementMSc MarketingMSc Business ManagementMSc Global Supply Chain ManagementMS in Information Systems and Technology with Business Intelligence and Analytics ConcentrationMSc Corporate FinanceMSc Data Analytics for BusinessMaster of Business AdministrationMaster of Business Administration 60 ECTSMaster of Business Administration 90 ECTSMaster of Business Administration 60 ECTSMaster of Business Administration 90 ECTSBachelors in International Management
For College Students

What is Functions Practice in Java?


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:


Video Transcript


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.


Video Recap



  • 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.