兰州理工大学研究生课程VC++课件5
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
Why Do You Need Байду номын сангаасunctions?
• One major advantage that a function offers is that it can be executed as many times as necessary from different points in a program. • Without the ability to package a block of code into a function, programs would end up being much larger, because you would typically need to replicate the same code at various points in them. • You also use functions to break up a program into easily manageable chunks for development and testing; a program of significant size and complexity that consists of several small blocks of code is much easier to understand and test than if it were written as one large chunk.
5.Introducing Structure In Your Programs
201405
WHAT YOU WILL LEARN IN THIS CHAPTER:
• • • • • • How to declare and write your own C++ functions How function arguments are defined and used How to pass arrays to and from a function What pass-by-value means How to pass pointers to functions How to use references as function arguments, and what pass-by-reference means • How the const modifier affects function arguments • How to return values from a function • How to use recursion
double power(double x, int n) // Function header
• • • •
It consists of three parts: ➤ The type of the return value (double, in this case) ➤ The name of the function (power, in this case) ➤ The parameters of the function enclosed between parentheses (x and n, in this case, of types double and int, respectively)
• You could write a function to raise a value to a given power; that is, to compute the result of multiplying the value x by itself n times, which is xn: • • • • // Function to calculate x to the power n, with n greater than or // equal to 0 double power(double x, int n) // Function header { // Function body starts here...
• A function is a self-contained block of code with a specific purpose. • A function has a name that both identifies it and is used to call it for execution in a program. • The name of a function is global if it is not defined within a namespace, otherwise the name is qualified by the namespace name. • The name of a function is not necessarily unique, as you’ll see in the next chapter; however, functions that perform different actions should generally have different names.
• NOTE • No semicolon is required at the end of the function header or after the closing brace for the function body.
The General Form of a Function Header
UNDERSTANDING FUNCTIONS
• Up to now, you haven’t really been able to structure your program code in a modular fashion, because you only know how to construct a program as a single function, main(); but you have been using library functions of various kinds as well as functions belonging to objects. • A good understanding of how to implement functions is essential to object-oriented programming.
• The general form of a function header can be written as follows:
return_type function_name(parameter_list)
• The return_type can be any legal type.
• If the function does not return a value, the return type is specified by the keyword void. • The keyword void is also used to indicate the absence of parameters, so a function that has no parameters and doesn’t return a value would have the following function header.
double result(1.0); // Result stored here for(int i = 1; i <= n; i++) result *= x; return result;
• } // ...and ends here
The Function Header
• Let’s first examine the function header in this example. The following is the first line of the function:
• The name of a function is governed by the same rules as those for a variable. • A function name is, therefore, a sequence of letters and digits, the first of which is a letter, where an underscore (_) counts as a letter. • The name of a function should generally reflect what it does, so, for example, you might call a function that counts beans count_beans().
• You pass information to a function by means of arguments specified when you invoke it. • These arguments must correspond with parameters that appear in the definition of the function. • The arguments that you specify replace the parameters used in the definition of the function when the function executes. • The code in the function then executes as though it were written using your argument values.
Structure of a Function
• As you have seen when writing the function main(), a function consists of a function header that identifies the function, followed by the body of the function between braces that makes up the executable code for the function. • Let’s look at an example.
• Functions enable you to segment the program so that you can write the code piecemeal.
• You can test each piece independently before bringing it together with the other pieces. • This approach also allows the development work to be divided among members of a programming team, with each team member taking responsibility for a tightly specified piece of the program that has a well-de! ned, functional interface to the rest of the code.
• The return value is returned to the calling function when the function is executed, so when the function is called, it results in a value of type double in the expression in which it appears. • The parameter names and any variables defined in the body of the function are local to the function.