COURSES
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
Law
Job Linked
Bootcamps
Study Abroad
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 AnalyticsMS 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 Administration | STEMMS 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 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 MediaMSc Sustainable Tourism and Event ManagementMSc in Circular Economy and Sustainable InnovationMSc in Impact Finance and Fintech ManagementMS Computer ScienceMS in Applied StatisticsMaster 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 ManagementMBA in Strategic Data Driven ManagementMSc Digital MarketingMBA Business and MarketingMaster of Business AdministrationMSc in Sustainable Global Supply Chain ManagementMSc 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 AdministrationMaster of Business AdministrationMSc in International FinanceMSc in International Management and Global LeadershipMaster of Business AdministrationBachelor of BusinessMaster of Business Administration 60 ECTSMaster of Business Administration 90 ECTSMaster of Business Administration 90 ECTSBachelor of Business AnalyticsBachelor of Information TechnologyMaster of Business AdministrationMBA Business AnalyticsMSc in Marketing Analytics and Data IntelligenceMS Biotechnology Management and EntrepreneurshipMSc in Luxury and Fashion ManagementMaster of Business Administration (90 ECTS)Bachelor of Business Administration (180 ECTS)B.Sc. Computer Science (180 ECTS) MSc in International Corporate Finance MSc in Sustainable Luxury and Creative IndustriesMSc Digital Marketing
For College Students

What is Java Passing Parameters? Why it is Important

$$/$$

You learnt how to define functions in the last lecture. The functions you saw had a limited usage because they were not interacting with the code. They were performing an action based on predefined values. What if you want functions to be more flexible? For instance, what if you want them to perform actions based on the values input by the user. For this, the functions would need to interact with the code. Let’s see how this is done.

 

You can download the code used in the video from below :

$$/$$

Video Transcript

 

The functions you just saw were independent of the rest of the code inside the main body and such functions have very limited usage. You need functions to interact with the code. The function should be able to take values from some variables and perform some operations on them. Then it will return something or display some result. Ideally, this is how it will help you even more. For example, you saw how hello function earlier did nothing but print hello world. It would have been great if say it took a name you specified and printed Hello Name. This is actually possible in Java. Let's look at an example. In my main function I am calling for Print greeting function and in this function I am passing Danny as a parameter. Let's see how this code acts. As you can see, Hello Danny has been printed when we passed Danny in the Print greeting function, if you can see the print greeting function, we simply print out hello and concatenate it with the string name that has been passed into this function.

 

In Java terminology, you pass the parameter into a function in the example below here the parameter was a string that contained a name. The function read the name and gave the output remember to specify what type of parameter a function is going to get in the function itself. The function also creates a variable for it so it can use it in the code. Try passing an integer inside the code and it won't run. So let's try passing nine and as you can see, IntelliJ itself has suggested that there is something wrong with the code. Let's expand this example. Let's say if we want the user to provide the name that he or she would actually like to print so at first we'll ask the user to give us an input name. We store that name in, let's say, a string called Name. Next, we pass this value to our Print Greeting function and the Print greeting function is similar to what we've seen before we just print hello and concatenate it with the name parameter that is being sent. Let's try running this example.

 

I'm going to write but it did not print Hello Afra. Instead, it printed hello. Name? What possibly could have gone wrong? If you see in Print Greeting, we are not really passing the variable but instead we are passing a string called Name in double quotes. So let me just fix this and make it Name. So let's try running this code again.

 

Now I'm going to write as we can see now it just simply printed Hello Afra.

 

Video Recap

 

  • Functions in Java should be able to take values from variables and perform operations on them

  • Functions should return something or display a result

  • It is important to specify the type of parameter a function will receive

  • Passing an incorrect type of parameter will result in an error

  • It is possible to prompt the user for input and pass it to a function as a parameter

  • Ensure that the correct variable is passed to a function to avoid errors

  • Properly defined functions in Java can be used to manipulate data and output results.

You just created a function that took a name input from the user and printed a greeting using the name.

In the example you saw, you defined a single parameter. You can define even more parameters as required. Now, let’s look at a function that takes two numbers as input and returns the larger value of the two.

 

Note: You can download the code used in the video from below :

$$/$$

You must clearly define what parameters the function can take. If you define two int parameters, you can pass only two int values into it. This means that you can only pass the values that are of the same data type as the parameters defined in the function.

 

You should also note that the values passed into the function are called arguments. Thus, instead of saying you are passing values/variables into the function, you can say you are passing arguments into the function.

$$/$$

So now you know how to create functions that can interact with the code. In the next few lectures, you will see some more examples where creating functions reduces your effort.