例5-8求任意两个整数的最大数#include<iostream>usingnamespacestd;int max(int x, int y);l/函数原型声明,可换为intmax(int,int)int main()max(int, int );cout<<"Entertwo integers :";int a, b; cin >> a >> b;cout <<"The maxium number is " << max(a,b) << endl;return O;int max(int x,int y) Il 函数定义return x>y?x:y;10
10 例5-8 求任意两个整数的最大数 #include <iostream> using namespace std; int max(int x, int y); //函数原型声明,可换为int max(int,int) int main( ) { max(int , int ); cout << "Enter two integers:"; int a, b; cin >> a >> b; cout << "The maxium number is " << max(a,b) << endl; return 0; } int max(int x, int y) // 函数定义 { return x>y?x:y; }
5.3函数的调用函数调用1就是使程序转去执行某个函数体。>C++中,除了main函数外,其他任何函数都是通过被主函数直接或间接调用进行的。2、调用函数名()无参函数的调用格式:有参函数的调用格式:函数名(实参表11
11 5.3 函数的调用 1、函数调用 ➢ 就是使程序转去执行某个函数体。 ➢ C++中,除了main函数外,其他任何函数都是通过 被主函数直接或间接调用进行的。 2、调用 ➢ 无参函数的调用格式: 函数名( ) ➢ 有参函数的调用格式: 函数名(实参表)
5.3函数的调用3、实参实参用来将实际参数的值传递给形参可以是常量、具有值的变量或表达式。位置、注:形参和实参要求个数、类型一一对应。12
12 5.3 函数的调用 3、实参 ➢ 实参用来将实际参数的值传递给形参。 ➢ 可以是常量、具有值的变量或表达式。 注:形参和实参要求个数、位置、类型一一对应
例5.2阶乘函数的调用I/Example5-2:测试阶乘计算函数的主程序#include<iostream>usingnamespacestdint main()intfac(intn);//函数声明,可换为intfac(int)int n;cout <<"Please input a number n to calculten! :"cin >>n;cout << n <<"! =" << fac(n)<< endl;return O;13
13 例5.2 阶乘函数的调用 // Example 5-2:测试阶乘计算函数的主程序 #include <iostream> using namespace std; int main() { int fac(int n) ; //函数声明,可换为int fac(int) ; int n; cout << "Please input a number n to calculte n! :"; cin >> n; cout << n << "! = " << fac(n) << endl; return 0; }