使用函数组织代码

前面我们学习的都只有一个main函数,但是当代码比较长时,应使用函数的调用,使代码更具有逻辑性、可读性。

函数的编写类型

函数的编写有很多种方法,下面介绍几种常用方法,分别用例子来表述:
1 带有多个参数的函数
计算圆柱的面积

#include "stdafx.h"
#include "iostream"
#include "string"
using namespace std;
const double pi = 22 / 7.0;
double  cylinderArea(double radius, double height)
{
	return 2 * radius*radius*pi + 2 * radius*height;
}
int main()
{
	double inradius = 0;
	cout << "Please input a radius : ";
	cin >> inradius;
	double inheight = 0;
	cout << "Please input a height : ";
	cin >> inradius;
	cout<<"the cylinderArea is : "<<cylinderArea( inradius, inheight)<<endl;
	system("pause");
    return 0;
}

2 没有返回值和参数的函数(这个比较简单)
3 带有默认参数的函数
该方法在定义时默认参数pi,如果不输入形参,在调用时默认使用定义时的参数


#include "stdafx.h"
#include "iostream"
#include "string"
using namespace std;

double  cylinderArea(double radius, double height,  double pi = 22 / 7.0)
{
	return 2 * radius*radius*pi + 2 * radius*height;
}
int main()
{
	double inradius = 0;
	cout << "Please input a radius : ";
	cin >> inradius;
	double inheight = 0;
	cout << "Please input a height : ";
	cin >> inradius;
	cout << "Do you want to change the pi (y/n)";
	char changelatter = '\0';
	cin >> changelatter;
	if (changelatter == 'y')
	{
		cout << "Please input the newpi : ";
		double newpi = 0;
		cin >> newpi;
		cout << "the cylinderArea is : " << cylinderArea(inradius, inheight,newpi) << endl;
	}
	else
		cout << "the cylinderArea is : " << cylinderArea(inradius, inheight) << endl;
	system("pause");
    return 0;
}

4 可以调用自己的函数-递归函数
求出斐波那契数列,对比前面的嵌套循环求解https://mp.csdn.net/mdeditor/83987253

#include "stdafx.h"
#include "iostream"
#include "string"
using namespace std;

int fibonacci(int num)
{
	if (num < 2)
		return num;
	else
		return fibonacci(num-1) + fibonacci(num-2);
}
int main()
{
	int innum = 0;
	cout << "Please input a num : ";
	cin >> innum;
	int index = 0;
	for (index=0;index<innum;index++)
		cout << "the fibonacci is : " << fibonacci(index) << endl;
	system("pause");
    return 0;
}

在这里插入图片描述
5 包含多条return语句的函数

使用函数处理不同类型的数据

1 将数组传递给参数
先来对比在定义时传递一个参数与传递数组的区别
void inputnumber(int numbers);
void inputnumber(int numbers[],int length);

#include "stdafx.h"
#include "iostream"
#include "string"
using namespace std;

void inputnumber(int number[], int length)
{
	int index = 0;
	for (index = 0; index < length; index++)
	{
		cout << "the number is : " << number[index] << endl;
	}
}
int main()
{
	
	int innumber[5] = {1,2,3,4,5};
	inputnumber(innumber, 5);
	system("pause");
    return 0;
}

2 函数重载
即具有相同的名称和返回类型的函数,参数可以不同。
用函数的重载求解圆形和圆柱的面积

#include "stdafx.h"
#include "iostream"
#include "string"
using namespace std;
const double pi = 22 / 7.0;
double circleorcylinderArea(double radius)
{
	return radius * radius*pi;
}
double circleorcylinderArea(double radius, double height)
{
	return 2 * circleorcylinderArea(radius) + 2 * pi*radius*height;
}
int main()
{
	cout << "please input  a radius : ";
	double inradius = 0;
	cin >> inradius;
	cout << circleorcylinderArea(inradius) << endl;
	cout << "please input  a height : ";
	double inheight = 0;
	cin >> inheight;
	cout<<circleorcylinderArea(inradius, inheight)<<endl;
	system("pause");
	return 0;
}

在这里插入图片描述
记录一个错误:在函数return返回值后,误以为直接调用可以有值的输出,导致运行没有值的输出。所以定要用cout命令输出到窗口, cout<<circleorcylinderArea(inradius, inheight)<<endl。
3 按引用传递参数
运用&告诉编译器,不要将实参复制给函数,而是将指向该实参的引用传递给函数。
以上一个求解的面积为例子

#include "stdafx.h"
#include "iostream"
#include "string"
using namespace std;
const double pi = 22 / 7.0;
void circleorcylinderArea(double radius,double & result)
{
    result=radius * radius*pi;

}
void circleorcylinderArea(double radius, double height,double & result)
{
	result= 2 * radius*radius*pi+ 2 * pi*radius*height;
}
int main()
{
	cout << "please input  a radius : ";
	double inradius = 0;
	cin >> inradius;
	double outresult = 0;
	circleorcylinderArea(inradius, outresult);
	cout << "the circlrArea is " << outresult << endl;
	cout << "please input  a height : ";
	double inheight = 0;
	cin >> inheight;
	circleorcylinderArea(inradius, inheight, outresult);
	cout << "the cylinderArea is " << outresult << endl;
	system("pause");
	return 0;
}

注意:
1 在运用指向实参的引用传递参数时,函数没有返回值,所以为void类型
2 直接用cout输出引用指向的结果参数,例如:cout << "the cylinderArea is " << outresult << endl;
要和上面 cout<<circleorcylinderArea(inradius, inheight)<<endl;
直接调用参数并输出区分开来。

猜你喜欢

转载自blog.csdn.net/qq_33713592/article/details/84025869