Danh mục

Kỹ thuật lập trình_ Module 6

Số trang: 33      Loại file: pdf      Dung lượng: 923.68 KB      Lượt xem: 12      Lượt tải: 0    
Thu Hiền

Xem trước 4 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

Tham khảo tài liệu kỹ thuật lập trình_ module 6, công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả
Nội dung trích xuất từ tài liệu:
Kỹ thuật lập trình_ Module 6 Module6 A Closer Look at FunctionsTable of ContentsCRITICAL SKILL 6.1: Know the two approaches to argument passing ........................................................... 2CRITICAL SKILL 6.2: How C++ Passes Arguments .......................................................................................... 2CRITICAL SKILL 6.3: Using a Pointer to Create a Call-by-Reference .............................................................. 3CRITICAL SKILL 6.4: Reference Parameters ................................................................................................... 4CRITICAL SKILL 6.5: Returning References .................................................................................................... 9CRITICAL SKILL 6.6: Independent References ............................................................................................. 12CRITICAL SKILL 6.7: Function Overloading .................................................................................................. 13CRITICAL SKILL 6.8:Default Function Arguments ........................................................................................ 26CRITICAL SKILL 6.9: Function Overloading and Ambiguity .......................................................................... 29This module continues our examination of the function. It discusses three of C++’s most importantfunction-related topics: references, function overloading, and default arguments. These features vastlyexpand the capabilities of a function. A reference is an implicit pointer. Function overloading is thequality that allows one function to be implemented two or more different ways, each performing aseparate task. Function overloading is one way that C++ supports polymorphism. Using a defaultargument, it is possible to specify a value for a parameter that will be automatically used when nocorresponding argument is specified. We will begin with an explanation of the two ways that argumentscan be passed to functions, and the implications of both methods. An understanding of argumentpassing is needed in order to understand the reference. 1 C++ A Beginner’s Guide by Herbert SchildtCRITICAL SKILL 6.1: Know the two approaches to argumentpassingIn general, there are two ways that a computer language can pass an argument to a subroutine. The firstis call-by-value. This method copies the value of an argument into the parameter of the subroutine.Therefore, changes made to the parameter of the subroutine have no effect on the argument used tocall it.Call-by-reference is the second way a subroutine can be passed arguments. In this method, the addressof an argument (not its value) is copied into the parameter. Inside the subroutine, this address is used toaccess the actual argument specified in the call. This means that changes made to the parameter willaffect the argument used to call the subroutine.CRITICAL SKILL 6.2: How C++ Passes ArgumentsBy default, C++ uses call-by-value for passing arguments. This means that the code inside a functioncannot alter the arguments used to call the function. In this book, all of the programs up to this pointhave used the call-by-value method. For example, consider the reciprocal( ) function in this program:takes place inside reciprocal( ), the only thing modified is the local variable x. The variable t used as anargument will still have the value 10 and is unaffected by the operations inside the function. 2 C++ A Beginner’s Guide by Herbert SchildtCRITICAL SKILL 6.3: Using a Pointer to Create aCall-by-ReferenceEven though C++’s default parameter-passing convention is call-by-value, it is possible to manuallycreate a call-by-reference by passing the address of an argument (that is, a pointer) to a function. It isthen possible to change the value of the argument outside of the function. You saw an example of this inthe preceding module when the passing of pointers was discussed. As you know, pointers are passed tofunctions just like any other values. Of course, it is necessary to declare the parameters as pointer types.To see how passing a pointer allows you to manually create a call-by-reference, consider a functioncalled swap( ) that exchanges the values of the two variables pointed to by its arguments. Here is oneway to implement it:The swap( ) function declares two pointer parameters, x and y. It uses these parameters to exchange thevalues of the variables pointed to by the arguments passed to the function. Remember, *x and *y referto the variables pointed to by x and y. Thus, the statement*x = *y;puts the value of the object pointed to by y into the object pointed to by x. Consequentl ...

Tài liệu được xem nhiều: