3.1 函数声明,调用与定义
函数调用
- 函数调用:主调(客户)函数与被调(服务器)函数
函数调用时的参数与返回值
例一:Swap( a, b )
; 无返回值
例二:n = Add( a, b )
; 有返回值 - 函数原型
函数的实现与调用格式说明:作为函数接口 一般出现在头文件中。
格式:函数返回值类型 函数名称( 形式参数列表 );
例一:int Add( int x, int y )
;
例二:void Swap( int x, int y );
例三:void Compute();
- 函数实现
函数定义,使用编程语言给出函数的执行步骤 - 函数返回值
函数完成后带回来的结果
主调函数可以使用 - 谓词函数
返回 bool 类型值的函数
目的:表达某项任务是否完成或某个条件是否满足 - 函数重载(C++)
定义同名但参数不完全相同的函数
示例:
int Max( int x, int y );
char Max( char x, char y );
bool Max( bool x, bool y );
函数实现实例
- Add 求和函数
编写函数 Add,求两个整数之和
int Add( int x, int y )
{
int t;
t = x + y;
return t;
}
- Compare 比较函数
编写函数 Compare,比较两个整型数据 x、y 的大小。若 x 等于 y 返回 0,若 x 大于 y 返回 1,若 x 小于 y 返回 -1
int Compare(int x,int y)
{
if (x==y){
return 0;
}
else if (x>y){
return 1;
}
else{
return -1;
}
}
- Swap 互换函数
编写函数 Swap,互换两个整型数据 x、y 的值
void Swap(int x,int y){
int t;
t=x;
x=y;
y=t;
}
- 谓词函数
编写函数 IsLeap,判断某个给定年份是否为闰年
bool IsLeap(int year){
return year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
}
3.2 函数调用规范
函数调用示例
编写程序将用户输入的两个整数相加,要求尽可能使用函数将程序中的操作独立出来
逻辑思考方式:从整体面向局部
#include <iostream>
using namespace std;
void Welcome();
int GetInteger(int idx ););
int Add(int x,int y);
void Result();
int main(){
int a,b,sum;
Welcome();
a = GetInteger(1);
b = GetInteger(2);
sum = Add (a,b);
cout << sum << endl;
return 0;
}
void Welcome(){
cout<<"Give me two integer, I will tell you the sum\n";
}
int GetInteger(int idx){
int t;
cout << "Please input the No: \n";
cin >> t;
cout <<"Your input is: " << t << endl;
return t;
}
int Add(int x,int y){
int add;
add = x+y;
return add;
}
void Result(){
cout<<"The sum is : \n";
}
运行结果:
Give me two integer, I will tell you the sum
Please input the No:
10
Your input is: 10
Please input the No:
20
Your input is: 20
30
值传递机制
- 形式参数即整型 x和y 在函数调用时才分配存储空间,并接受实际参数的值
int Add( int x, int y )
- 实际参数可以为复杂的表达式,在函数调用前获得计算
- 形式参数与实际参数可同名,也可不同名
- 参数较多时,实际参数值逐一赋值,它们必须保持数目、类型、顺序的一致
- 值的赋值过程是单向不可逆的,即只能是实际参数向形式参数赋值,函数内部对形式参数值的修改不会反映到实际参数中去
- 函数参数一般为函数输入集的一部分,函数输出集一般使用返回值表示,只有使用特殊的手段才可以将函数参数作为函数输出集的一部分
函数调用栈框架
3.3 编程实践
- 1.编写函数IsPrime,判断某个大于2的正整数是否为素数。
#include <iostream>
using namespace std;
void Welcome();
int GetInteger();
void IsPrime(int x);
int main() {
int n;
Welcome();
n=GetInteger();
if (n == -1) {
cout << "Fail\n";
}
else {
IsPrime(n);
}
return 0;
}
void Welcome() {
cout << "This is a program to judge a integer(>0) is a prime.\n";
}
int GetInteger() {
int a;
cout << "Please input an integer > 2\n";
cin >> a;
if (a<=2) {
cout << "Error Input!!!!" << endl ;
return -1;
}
else {
return a;
}
}
void IsPrime(int x) {
int i;
int not_prime=0;
for (i = 2; i < x; i++) {
if (x % i == 0) {
not_prime++;
}
}
if (not_prime == 0) {
cout << x << " is prime\n";
}
else
cout << x << " is not prime\n";
}
运行结果1:
This is a program to judge a integer(>0) is a prime.
Please input an integer > 2
1
Error Input!!!!
Fail
运行结果2:
This is a program to judge a integer(>0) is a prime.
Please input an integer > 2
100
100 is not prime
运行结果3:
This is a program to judge a integer(>0) is a prime.
Please input an integer > 2
150
150 is not prime
运行结果4:
This is a program to judge a integer(>0) is a prime.
Please input an integer > 2
31
31 is prime
- 2.编写函数gcd与lcm,分别求两个正整数的最大公约数与最小公倍数。
#include <iostream>
using namespace std;
void Welcome();
int GetInteger(int i);
int Greatest_Common_Divisor(int x, int y);
int Least_Common_Multiple(int x, int y);
int main() {
int a,b;
int gcd, lcm;
Welcome();
a = GetInteger(1);
if (a == -1) {
cout << "The 1st integer is wrong!\n";
return 0;
}
b = GetInteger(2);
if (b == -1) {
cout << "The 2nd integer is wrong!\n";
return 0;
}
gcd=Greatest_Common_Divisor(a, b);
cout << "The Greatest common divisor of " << a << " and " << b << " is " << gcd << endl;
lcm = Least_Common_Multiple(a, b);
cout << "Least common multiple of " << a << " and " << b << " is " << lcm << endl;
return 0;
}
void Welcome() {
cout << "This is a program to get Greatest common divisor and Least common multiple of two integers.\n";
}
int GetInteger(int i) {
int a;
cout << "Please input an integer > 0 \n";
cin >> a;
cout << "No." << i << " is " << a << endl;
if (a <= 0) {
cout << "Error Input!!!!" << endl;
return -1;
}
else {
return a;
}
}
int Greatest_Common_Divisor(int x, int y) {
int i;
int min = 0;
int max = 0;
int d = 0;
min = x < y ? x : y;
max = x > y ? x : y;
for (i = 1; i <= min; i++) {
if ((min % i == 0)&&( max % i == 0)){
d = i;
}
}
return d;
}
int Least_Common_Multiple(int x, int y) {
int i;
int min = 0;
int max = 0;
int m = 0;
min = x < y ? x : y;
max = x > y ? x : y;
for(i = max; i >= max; i++){
if ((i % min == 0) && (i % max == 0)) {
m = i;
return m;
}
}
}
运行结果1:
This is a program to get Greatest common divisor and Least common multiple of two integers.
Please input an integer > 0
-7
No.1 is -7
Error Input!!!!
The 1st integer is wrong!
运行结果2:
This is a program to get Greatest common divisor and Least common multiple of two integers.
Please input an integer > 0
3
No.1 is 3
Please input an integer > 0
-2
No.2 is -2
Error Input!!!!
The 2nd integer is wrong!
运行结果3:
This is a program to get Greatest common divisor and Least common multiple of two integers.
Please input an integer > 0
50
No.1 is 50
Please input an integer > 0
100
No.2 is 100
The Greatest common divisor of 50 and 100 is 50
Least common multiple of 50 and 100 is 100