当前位置:文档之家› 第七章 函数(1)

第七章 函数(1)


6 using namespace std ;
7 8 main()
9 {
10 for ( int n = 1 ; n < 11 ; n ++ ) 11 cout << sqrt ( n ) << endl ;
12 }
1 // Program Example P7B 2 // Demonstration of a programmer-defined function. 3 #include <iostream> 4 #include <string> 5 using namespace std ; 6 7 void stars( void ) ; //function prototype(函数原型) 8 9 main() 10 { 11 string text = "some text " ; 12 13 stars() ; //函数调用 14 cout << endl ;
9 main() 10 { 11 string text = "some text " ; 12 13 stars( 11 ) ; // Call the function to print 11*s - top of the box . 14 cout << endl ; 15 stars( 1 ) ; // Left side of the box - 1 * only. 16 cout << text ; //Display text in the middle of the box. 17 stars( 1 ) ; // Right side of box - 1 * only. 18 cout << endl ; 19 stars( 11 ) ; // Bottom of the box - 11 * s. 20 cout << endl ; 21 } 22 23 void stars( int num ) 24 { 25 for ( int counter = 0 ; counter < num ; counter ++ ) 26 cout << '* '; 27 }
C++允许程序员自己编写函数加入到手头已存在的标准库中。
1 // Program Example P7A 2 // Program to demonstrate the built-in and sqrt()
3 #include <iostream>
4 #include <string> 5 #include <cmath>
Chapter Seven Functions 第7章 函数 (一) 7.1 Introduction (引言)
A function is a block of statements called by name to carry out a specific task. 函数是用函数名来调用执行的具有特定 功能的语句块. C++ has a variety of built-in, pre-written, functions in the standard library. 在C++的标准库中有很多固有的、预先定义的库函数。 • C++ allows a programmer to write functions to add to those already at hand in the standard library.
• Calls to the function stars() in lines 13,15,17 and 19 now have a number between the parentheses (and). This number is called an argument and is received by the parameter num declared as an integer in line 23. • 现在第13、15、17和19行调用函数stars()时,圆括号内 有一个数值。这个数值称为实际参数(简称实参),它从第 23行声明为整型的形式参数(简称形参)中接收数据。 • The parameters of a function are known only within the function. Therefore, variables with the same name can be used in main() or in any other function without a conflict occurring. • 函数的形参仅在函数内部有效,因此,在函数main()或 其他任何函数中使用同名变量不会发生冲突。
类型的声明。将函数类型声明为void,表示函数没有返
回值返回给调用程序。而某些函数确实需要返回一个 数值,例如函数sqrt()就返回一个数值,即一个数的 平方根。
7.2 Function arguments (函数实参)
1 // Program Example P7C 2 // Demonstration of function arguments. 3 #include <iostream> 4 #include <string> 5 using namespace std ; 6 7 void stars( int ) ; // Function prototype. 8
15 cout << ' * ' ; // Left side of the box. 16 cout << text ; //Text in middle of the box. 17 cout << ' * ' << endl ; // Right side of the box. 18 stars() ; // Bottom of the box. 19 cout << endl ; 20 } 21 22 void stars( void ) 23 { 24 for ( int counter = 0 ; counter < 11 ; counter ++ ) 25 cout << '* '; 26 }
26 disp_chars( 1, '+ ' ); // Display a +. 27 cout << endl ; 28 // Bottom of the box. 29 disp_chars( 35, '+ ' ); // Display 35 spaces 30 disp_chars( 11, '+ ' ); // and eleven + s. 31 cout << endl ; 32 } 33 34 void disp_chars( int num, char ch ) //函数首部 35 { 36 for ( int counter = 0 ; counter < num ; counter ++ ) 37 cout << ch ; 38 }
1 // Program Example P7D 2 // Demonstration of a function with two parameters. 3 #include <iostream> 4 #include <string> 5 using namespace std ; 6 7 void disp_chars( int num , char ch ) ; // Function prototype
For example, to give default values to the parameters of disp_chars() in program P7D,the prototype on line 7 is modified to void disp_chars( int num = 1 , char ch = ' ') ; The prototype now assigns a default value of 1 to the first parameter and a space to the second parameter. For example, the statement disp_chars( 35 ) ; // The second argument is omitted. is equivalent to disp_chars( 35, ' ' ); Similarly, the statement disp_chars( ); // Both arguments are omitted. is equivalent to disp_chars( 1, ' ' ); (注:函数定义方式不变,只是函数原型和调用语句形式有改变)
The output from this program is: *********** *some text* ***********
函 数 定 义
like variables, functions must be declared before they are used.和变量一样,函数在使用之前也必须进行声明。 The void in the parentheses on line 7 informs the compiler that the function stars will not receive any data from the calling program. Some function do receive data when called. For example, the sqrt() function returns a numeric Value, i.e. the square root of a number. 第7行是将stars声明为函数。前面的void是对函数stars()
相关主题