C++ 函数

本文探讨了C++中函数的重要性和使用,包括无值、无参、有值、有参函数,以及函数重载和函数模板的概念,强调了函数在代码重用和团队协作中的关键作用。

函数:是简洁、高效地实现代码重用的一种机制。更重要的是,它使分而治之的方法易于实现和表达;使团队成员分工协作成为可能。这也是函数的来源!

将代码封装成函数格式:

**类型  函数名(参数列表)
{
	函数体 或 代码块 
}** 

例:int add(int x,int y) { return x+y; }

各函数举例:

(一)无值函数

#include <cmath>		//调用数学计算函数 
#include <string>
#include <iostream> 
#include <iomanip>	    //小数点控制头文件 
using namespace std;

	/*** 无值函数1 ***/
	
	//void  函数名(参数列表)  {函数体} 
	
	// 形参:定义函数时在参数列表中的参数(全称为形式参数) 
	// 实参:调用函数时提供给参数的实际值(全称为实际参数)
	
	const double pi = 2 * acos(0);		//写定一个pi值
	
	void print (double s)
	{
		cout<<"圆的面积:"<<s<<endl;
	}
	
	void cricle()
	{
		cout<<"请输入圆的半径:";
		double r;
		cin>>r;
		print(pi *r *r); 
	}
	
	/*** 无值函数2 ***/
	const double pi2 =2 * acos(0); 
	
	void print2 (double s)
	{
		cout<<"圆柱的体积:"<<s<<endl;
	}
	
	void cylinder(double r,double h)
	{
		//cout<<"请输分别入圆的半径和圆柱的高:";
		//cin>>r>>h;
		print2 (pi2 *r *r *h);
	}

int main()
{
	/*** 无值函数1 ***/
	 
	cricle();
	
	/*** 无值函数2 ***/
	
	cylinder(2,2);   //在括号内定义圆的半径和圆柱的高 
}

(二)无值无参函数

#include <cmath>		//调用数学计算函数 
#include <string>
#include <iostream> 
#include <iomanip>	    //小数点控制头文件 
using namespace std;

	/*** 无参函数 ***/    //即参数列表为空 ,括号内为参数
	void function1()
	{
	   cout << "无参函数1" << endl;
	} 

	void function2( void )
	{
	   cout << "无参函数2" << endl;
	} 

int main()
{
	function1();              
    	function2(); 
}

(三)有值函数

#include <cmath>		//调用数学计算函数 
#include <string>
#include <iostream> 
#include <iomanip>	    //小数点控制头文件 
using namespace std;

	/***  有值函数  ***/
	//除void以外的类型  函数名(参数列表)  {函数体}
	const double pi3=2*acos(0);
	
	double cricle2(double r)
	{
		return pi3 *r *r;
	}
	
int main()
{
	cout<<"圆的面积:"<< cricle2(2)<<endl;
}

(四)有值有参函数

#include <cmath>		//调用数学计算函数 
#include <string>
#include <iostream> 
#include <iomanip>	    //小数点控制头文件 
using namespace std;

	/*** 有参函数 (只要参数列表不为空就是有参函数)***/
	//类型  函数名(参数列表)   {函数体} 
	bool jiou(int x)
	{
		//cin>>x      它切断了数据传输通道。如果这样,x就不应是形参。错误!!! 
		return x%2;
	}	
	/**有值有参函数是最重要的函数**/
	
int main()
{
	for(int i=1;i<=5;i++)
	{
		if(jiou(i))
			cout<<i<<"是奇数"<<endl;
		else
			cout<<i<<"是偶数"<<endl;
	}
}

(五)函数重载

#include <cmath>		//调用数学计算函数 
#include <string>
#include <iostream> 
#include <iomanip>	    //小数点控制头文件 
using namespace std;

	/**  函数重载   **/
	//函数重载:指在同一作用域内,可以有一组具函数名相同,但参数不同的函数(至少是 参数类型、参数数目 或 参数类型的顺序 不同)
	//例如:int sum(int,int);	 double sum(double,double)
	
	//重载函数,可理解为重载的函数,函数重载的执行过程
	//例如: 
	int add(int, int);                     // 声明int版add 
	double add(double, double);            // 声明double版add  
	
int main()
{
	cout<<add(1,1)<<endl;
	cout<<add(2,2)<<endl;
}

int add(int x,int y)		//调用int add函数模板
{
	return x+y;
}

double add(double x,double y)		//调用double add函数模板
{
	return x-y;
}

函数签名(function signature):

就是函数原型除去函数值类型后剩下的部分。同一作用域内的函数必须有唯一的签名

	//什么是函数签名?例如:
	/*
	int func(int x) 的函数签名是   --->  int func(int)

	相应的还有类似的其他函数签名如:
						float func(float)
						int C::func(int)
						int C::C2::func(int)
						int N::func(int)
						int N::C::func(int)	 
	*/

(六)函数模板

#include <string>
#include <iostream>
using namespace std;

/**   函数模板   **/

template <class type>	//模板<类 类型>	
type add(type x,type y)
{
	return x+y;	// 任何实现+运算符的Type都行 
}

int main()
{
	string a="这是",b="模板"; 
	
	cout<<add(1,1)<<endl;			//调用int add模板
	cout<<add(1.1,1.1)<<endl;		//调用double add模板
	cout<<add(a,b)<<endl;			//调用string add模板
} 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值