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