1、Class07Lecture 8前15分钟 回首页This is Computer Programming I from the University of Washington, Im Martin Dickey. And today well continue our discussion of functions in lecture A. Although Ive titled the lecture “function parameters”. Parameters are only one of many important topics well talk about today
2、. In addition to parameter, well be talking about return values, return types, return statement, local variables, function prototypes and header files. There is times of new terminologies in this lecture as well. Till the end of the lecture, Ill do a standard example with a natural program traced in
3、 detail. And following that, I think there will be time for a couple of bonus topics that I think you may find useful when you sit down to write your programs. Lets go back to the example we use extensively last time. A function which prints a binal of 2 lines its quite a simple function. One of the
4、 advantages of functions, one of our primary motivations, is to make program easier to change in case the designer or the client find that they need to do that. Lets consider one of these changes and see how it might affect us and how we might handle it. Suppose the client now decides 2 rows are not
5、 enough, there should be 5 rows of asterisks when the program starts and stops. But all the other times, there should be only two. How could we solve this problem? Well, using what we know now, you could sit down and write a second function, maybe you call it printBinalr2 that function could print 5
6、 rows of asterisks. And then back in the main program you would call it at the beginning of the program. Call it at the end and all the other spot you will still have calls to the original functions. You could do that, but computer science tendency are new toils(2:12) for trying to generalize to fin
7、d the abstraction and covers more than one case. Could we generalize here? Could we somehow take a single function, printBinalLines and make it do both of the operation that we need? Make it print 5 lines sometimes and make it 2 lines other times. In that sense, thats what parameters are all about.
8、And at a great deal of lecture well be devoted to showing how we can do that. Some of the consequences and some of the programming details involved. Lets state the problem and slide it in a different way. Our original specification was to print 2 rows of asterisks. Wed like to modify that so that it
9、 could either print 2 rows or 5 rows. A more general way of stating this would be: lets print n rows. Well, n is some quantity that we dont know ahead of time, it might be 2, it might be 5, maybe it could be something else, OK. At the moment the function is called, that value would be known so the f
10、unction would know what to do. So n isnt number, its information of the function needs to know and thats a crucial(至关重要的) concept. We need a way of getting information into the function. Lets suppose there is a way to do that, I want to show you the code for it right now, but lets suppose we could d
11、o that. How would that change our main program? Here is how it looks when I called the new version of printBianlLines. When I want to print 5 lines, I would put that requested value in parentheses following the function name. Now pause a minute and try to remember what was there originally. What did
12、 I use to have instead of a parenthesis 5? If you remember or go back and look. It used to be a parenthesis with nothing inside, just empty parenthesis, OK. The parenthesis is there to hold the information thats being sent to the function and we have a name for that kind of information, its called a
13、 parameter. This is terminology well ma(4:39) use extensively in the rest of the course. The parameter, information that is to be sent to the function. How would we write this function? Im not going to give you full code now. Its not really relevant(有关的) what the function body is doing and well see
14、other examples later on in this lecture. The crucial thing is the beginning of the function, how the function knows whether information to look for. And this is done in a way that it is whether parallel(这里是函数声明的意思) to the function call. We still have the function name printBianlLines. But now in par
15、enthesis we have something that looks like a declaration. It says “int n”. Think back to what was there before. It was not an empty parenthesis before, it was a magic keyword “void”(魔:程序中经常出现的) . Ive briefly said that the void meant were not sending information and there are no parameters. Now there
16、 is a parameter. We want the function to know a value. So what we do is we declare, what we call a formal parameter, well give it a name, well give it a type. It is exactly the same format I use when I declare variable in the program and it does declare n. It declares whose locations and whose names and whose type is int. The special are magic things about this n is when the function starts to execute, it will contain an n , the value sent to the fun
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1