课程: Learning Java 17
What are functions?
- [Instructor] So far, we've created code that runs sequentially, one line after the other. We've also written code that manipulates a program's control flow with "if" statements and loops. The way we've written code so far has more to do with the way it's executed rather than the way it's designed. In these next two chapters, we'll be focusing more on program design and how we can create programs that are readable and easily understood by other software engineers. One way to do this is with functions. In software development, a function is just a series of finite steps that accomplish some task. Let say, we have the task of creating a peanut butter and jelly sandwich. The first step would be to gather the ingredients. The next step, spread peanut butter on one slice and jelly on the other slice. Next, you would put the two slices together to create a sandwich. And finally, you would place them on the plate. Each of these steps is a defined step that we take in order to complete the make PB and J task. We call the combination of these steps, in association with the task, a function. So how do you use a function in code? We use its label or the name we give the task. In this case, the task name is makePBandJSandwich. This means every time we want to make a PB and J, we just use the name of the function, instead of listing out every single step. We would write the name of the function with open and closed parentheses. This pseudo code makes three peanut butter and jelly sandwiches because it executes the function three times. Without functions, we would have to copy and paste these steps three times, which is unnecessarily wordy. We want our code to be designed as we live and work in everyday life. When I tell someone to make a peanut butter and jelly sandwich, I don't need to tell them all the steps. I just use the name of the task. But at some point long ago, that person learned how to make a peanut butter and jelly sandwich and they associated the steps with accomplishing that task. In programming, we have to write the code that defines the task, as well as all the steps needed to complete the task or make the peanut butter and jelly sandwich. Once it's defined, we can use the task name to execute the function or perform the task as many times as we want. Now, our functions don't have to be a bunch of steps. Let's say we wanted to create a function that prints out it's teatime whenever it's time for tea. The name or label for the function would be announceTeatime. And for the steps, it would wait for the appropriate time and then print out, "It's teatime!" To use the function, we would just need to use the name, announceTeatime. So why use functions? Functions help developers organize their code in a more meaningful way. Organizing a series of steps under a label helps us keep track of certain tasks we might want our program to accomplish. It also helps us define a single task once so we can use it all throughout the program. For example, once we know how to make a P B and J, we can just use the task name to make one. Now, what if we want to change how we make a peanut butter and jelly sandwich or we want to change how we announce teatime? Instead of having to change a bunch of separate lines in the code, we can go to the function steps and modify the step we want to change. For example, if we wanted to change our announcement to "It's teatime! It's time for some snacks", we would just change the one print statement in the announceTeatime step instead of changing a bunch of lines in separate areas of the code. Functions make it easier to change what our code is doing.
随堂练习,边学边练
下载课堂讲义。学练结合,紧跟进度,轻松巩固知识。