tailieunhanh - Ivor Horton’s BeginningVisual C++ 2008 phần 3

chức năng, nhưng điều này không phải như vậy. Bạn có thể sử dụng tên tham số còn ý nghĩa hơn trong các mẫu thử nghiệm chức năng để hỗ trợ sự hiểu biết về tầm quan trọng của các tham số và sau đó sử dụng tên tham số ngắn hơn trong định nghĩa chức năng nơi những tên dài hơn sẽ làm cho các mã | Chapter 5 Introducing Structure into Your Programs function but this doesn t have to be so. You can use longer more expressive parameter names in the function prototype to aid understanding of the significance of the parameters and then use shorter parameter names in the function definition where the longer names would make the code in the body of the function less readable. If you like you can even omit the names altogether in the prototype and just write double power double int This provides enough information for the compiler to do its job however it s better practice to use some meaningful name in a prototype because it aids readability and in some cases makes all the difference between clear code and confusing code. If you have a function with two parameters of the same type suppose our index was also of type double in the function power for example the use of suitable names indicates which parameter appears first and which second. Try It Out Using a Function You can see how all this goes together in an example exercising the power function. Declaring defining and using a function include iostream using std cout using std endl double power double x int n Function prototype int main void int index 3 Raise to this power double x Different x from that in function power double y y power 3 Passing constants as arguments cout endl cubed y cout endl cubed power index Outputting return value x power x power Using a function as an argument cout endl with auto conversion of 2nd parameter x x cout endl return 0 Function to compute positive integral powers of a double value First argument is value second argument is power index double power double x int n 244 Chapter 5 Introducing Structure into Your Programs double result for int i 1 i n i Function body starts here. Result stored here result x return result .and ends here This program shows some of the ways in which you can use the function power specifying the arguments to