课程: Learning Java 17
Calling functions in Java
- [Instructor] Now that we've defined our announceTeaTime function, we can proceed to the next step, using functions in Java. To use a function, we just call it by name. We often refer to this as calling the function. We're recalling the steps we defined previously and we're executing them. Let's call this function in Java. We defined our function outside of the main function, but we're going to use our function inside the main function. We'll use it by name, announceTeatime. This will call the announceTeatime function, execute its steps, and then continue to execute whatever's left in the main function sequentially. Let's run this in debug mode so we can see what's happening. We'll add a break point to where we call the function and then run it. The program's execution has just started and we're about to call the announceTeatime function. Notice we have no variables in our variables window, which means no variables are in scope. We have a parameter P, which is args, and we'll talk about that later on. Let's hit Step Into. This will step into the implementation of announceTeaTime. We're in steps to an announceTeaTime to the user. Calling a given function by name means executing the steps associated with that function name. The first step to be executed is the print statement. If we hit Step Over, we'll execute this line of code, and in the console, we see Waiting for tea time. Let's hit Step Over again. We execute the next line. Let's enter a random word, treat, and we'll hit Enter. We'll hit Step Over. We'll hit Step Over again. And we'll finally print It's tea time. Now you'll notice we create a local variable called input. This will disappear once we exit the announceTeaTime function. The input scope is only in this function. Let's hit Step Over and it disappears. No more input variable. Let's hit Continue. It works as expected. Let's add some more code to this main func, rephrase. So it works as expected. Let's add some more code to the main function to make sure we understand the control flow. Now let's run the program again, but we'll move our break point. We'll move it to the first line of the announceTeaTime implementation. Let's run it. In the console, we see the print statement, Welcome to your new job. The next step is to announceTeaTime. Let's hit Continue. We'll type in a random word, treaty. So the program announces teatime and then goes back to the main function. We continue to perform these steps. We write code, we review code, we learn stuff and we print that all to the console. Then we call announceTeaTime again so we return to announceTeaTime steps. Let's hit Continue. We'll type in trick. Then we go through the rest of the steps. We announce it's teatime and return to the main function. Then we get promoted and that's what's printed to the console. Awesome, we just called our first function. Now you might be thinking this teatime function looks pretty similar to the main function and that's because they're both functions. Whenever we hit the Play button and execute a program, the main function is automatically called. It's automatically used. Every time we've been writing code, we've been defining the main function that's automatically called at execution.
随堂练习,边学边练
下载课堂讲义。学练结合,紧跟进度,轻松巩固知识。