2 Function Declaration Declaration principle: If a function is defined before its being used,the declaration may be omitted; If a function is used before its definition,it must be declared in advanced. Declaration method: Return type Name parameters list ) For example: int sum(int n,float m); 函数申明可以不包含参数的名字,而只要包含函数的类型 017年4月26日12时17分 HOME 第4章函数与预处理 BACK NEX
HOME2017年4月26日12时17分 第4章 函数与预处理 7 • Declaration principle: If a function is defined before its being used, the declaration may be omitted; If a function is used before its definition, it must be declared in advanced. • Declaration method: • Return type Name (parameters list ); For example: int sum(int n, float m); //函数申明可以不包含参数的名字,而只要包含函数的类型
3 Function's type Getting parameters and returning value.For example: int bigger(int a,int b) { return(a>b)?a:b; Getting parameters but not return value.For example: void delay(long a) for(int i=1;i<=a;i++); C++要求在定义函数时必须指定函数的类型。 017年4月26日12时17分 HOME 第4章函数与预处理 8 BACK NEXT
HOME2017年4月26日12时17分 第4章 函数与预处理 8 • Getting parameters and returning value. For example: int bigger(int a, int b) { return(a>b)?a:b; } • Getting parameters but not return value. For example: void delay(long a) { for(int i=1;i<=a;i++); } • C++要求在定义函数时必须指定函数的类型
3 Function's type Getting no parameters but returning value.For example: int geti() { int x; cout<<"Please input a interger:\n"; cin>>X; return X; } Getting no parameters and returning no value.For example: void message() cout<<“This is a message.ln” } 017年4月26日12时17分 HOME 第4章函数与预处理 9 BACK NEXT
HOME2017年4月26日12时17分 第4章 函数与预处理 9 • Getting no parameters but returning value. For example: int geti() { int x; cout<<“Please input a interger:\n”; cin>>x; return x; } • Getting no parameters and returning no value. For example: void message() { cout<<“This is a message.\n” }
4 Function's Call Declare a function prototype at the beginning of the program before it being called: 类型标识符被调用函数名(含类型说明的形参表); Call Form 函数名(实参列表) max(a,b) Nesting call Nesting definition is not allowed in any function, but nesting call is permitted. Recursion call Function can call itself directly or indirectly. 0了年4月26日12时17分 第4章函数与预处理 10 BACK NEXT
HOME2017年4月26日12时17分 第4章 函数与预处理 10 • Declare a function prototype at the beginning of the program before it being called: 类型标识符 被调用函数名 (含类型说明的形参表); • Call Form 函数名(实参列表) max(a,b) • Nesting call Nesting definition is not allowed in any function, but nesting call is permitted. • Recursion call Function can call itself directly or indirectly
例4.1在主函数中调用其他函数 #include <iostream> using namespace std; void printstar(void) l定义printstari函数 { C0UtK<"*********"<<edl;/输出30个 } void print_message(void) l定义print_message函数 { cout<<"Welcome to C++!"<<endl; 输出一行文字 int main(void) printstar() ∥调用printstar函数 print_message()月 i调用print_message函数 printstar()月 ∥调用printstar函数 return 0; 2017年4月26日12时17分 HOM正 第4章函数与预处理 11 BACK NEXT
HOME2017年4月26日12时17分 第4章 函数与预处理 11 #include <iostream> using namespace std; void printstar(void) //定义printstar函数 { cout<<"****************************** "<<endl; //输出30个“*” } void print_message(void) //定义print_message函数 { cout<<"Welcome to C++!"<<endl; //输出一行文字 } int main(void) { printstar( ); //调用printstar 函数 print_message( ); //调用print_message函数 printstar( ); //调用printstar 函数 return 0; }