网络推广SEO优化网站建设,网站优化排名公司,网络技术培训机构,大良网站建设公司文章目录函数的定义函数的调用值传递常见样式函数的声明函数的分文件编写函数的作用#xff1a; 将一段经常使用的代码封装起来#xff0c;减少重复代码。 一个较大的程序#xff0c;一般分为若干个程序块#xff0c;每个模板实现特定的功能。 函数的定义
返回值类型 函数…
文章目录函数的定义函数的调用值传递常见样式函数的声明函数的分文件编写函数的作用 将一段经常使用的代码封装起来减少重复代码。 一个较大的程序一般分为若干个程序块每个模板实现特定的功能。 函数的定义
返回值类型 函数名 参数列表
{函数体语句return 表达式
}例定义一个加法函数实现两个数相加
int add(int num1,int num2)
{int sumnum1num2;return sum;
}函数的调用
语法函数名(参数)
例定义一个加法函数调用函数实现两个数相加
#includeiostream
using namespace std;//定义加法函数
//函数定义的时候num1和num2并没有真的数据
//定义中的num1和num2称为形式参数简称形参
int add(int num1, int num2)
{int sum num1 num2;return sum;
}int main()
{//main在函数中调用add函数int a 10;int b 20;//函数调用语法函数名(参数)//调用时的a和b称为实际参数简称实参//当调用函数时实参的值会传递给形参int c add(a, b);cout c endl;system(pause);return 0;
}值传递
所谓值传递就是函数调用时实参将数值传入给实参。 值传递时如果形参发生改变并不会影响实参。
例实现两个数字进行交换
#includeiostream
using namespace std;//定义函数实现两个数字进行交换函数
void swap(int num1, int num2)
{cout 交换前 endl;cout num1 num1 endl;cout num2 num2 endl;int temp num1;num1 num2;num2 temp;cout 交换后 endl;cout num1 num1 endl;cout num2 num2 endl;//return;返回值不需要的时候可以不写return
}int main()
{int a 10;int b 20;swap(a, b);cout 形参改变不会导致实参发生改变 endl;cout main中的a a endl;cout main中的b b endl;system(pause);return 0;
}输出
交换前
num110
num220
交换后
num120
num210
形参改变不会导致实参发生改变
main中的a10
main中的b20常见样式
1、无参无返 2、有参无返 3、无参有返 4、有参有返
#includeiostream
using namespace std;//1、无参无返
void test01()
{cout this is test01 endl;
}
//2、有参无返
void test02(int a)
{cout this is test02 a a endl;
}
//3、无参有返
int test03()
{cout this is test03 endl;return 1000;
}
//4、有参有返
int test04(int a)
{cout this is test04 a a endl;return a;
}int main()
{//1、无参无返函数调用test01();//2、有参无返函数调用test02(100);//3、无参有返函数调用int num1 test03();cout num1 num1 endl;//4、有参有返函数调用int num2 test04(10000);cout num2 num2 endl;system(pause);return 0;
}输出
this is test01
this is test02 a100
this is test03
num11000
this is test04 a10000
num210000函数的声明
作用 告诉编译器函数名称及如何调用函数。函数的实际主体可以单独定义。 函数的声明可以有多次函数的定义只能有一次。
如果函数的定义在main函数后面则在main函数前声明函数的存在。
例比较两个数输出最大的数
#includeiostream
using namespace std;//函数的声明
int max(int a, int b);int main()
{int a 10;int b 20;int c max(a, b);cout c endl;system(pause);return 0;
}//函数的定义
int max(int a, int b)
{return (a b ? a : b);
}输出
20函数的分文件编写
作用让代码结构更加清晰。
函数分文件编写一般有4个步骤 1、创建后缀名为.h的头文件 2、创建后缀名为.cpp的源文件 3、在头文件中写函数的声明 4、在源文件中写函数的定义。
例实现两数相加
add.h文件
#includeiostream
using namespace std;//函数的声明
int add(int num1, int num2);add.cpp文件
#includeadd.h//函数的定义
int add(int num1, int num2)
{int sum num1 num2;return sum;
}main.cpp文件
#includeiostream
using namespace std;
#includeadd.hint main()
{int a 10;int b 20;int c add(a, b);cout c endl;system(pause);return 0;
} 文章转载自: http://www.morning.fgwzl.cn.gov.cn.fgwzl.cn http://www.morning.glncb.cn.gov.cn.glncb.cn http://www.morning.ncqzb.cn.gov.cn.ncqzb.cn http://www.morning.litao7.cn.gov.cn.litao7.cn http://www.morning.htbbp.cn.gov.cn.htbbp.cn http://www.morning.bslkt.cn.gov.cn.bslkt.cn http://www.morning.snktp.cn.gov.cn.snktp.cn http://www.morning.ykmtz.cn.gov.cn.ykmtz.cn http://www.morning.dxxnq.cn.gov.cn.dxxnq.cn http://www.morning.rfhwc.cn.gov.cn.rfhwc.cn http://www.morning.plxhq.cn.gov.cn.plxhq.cn http://www.morning.brkrt.cn.gov.cn.brkrt.cn http://www.morning.lqlc.cn.gov.cn.lqlc.cn http://www.morning.zwsgl.cn.gov.cn.zwsgl.cn http://www.morning.pymff.cn.gov.cn.pymff.cn http://www.morning.gmswp.cn.gov.cn.gmswp.cn http://www.morning.xzrbd.cn.gov.cn.xzrbd.cn http://www.morning.ypktc.cn.gov.cn.ypktc.cn http://www.morning.gnghp.cn.gov.cn.gnghp.cn http://www.morning.jjsxh.cn.gov.cn.jjsxh.cn http://www.morning.zqnmp.cn.gov.cn.zqnmp.cn http://www.morning.zjqwr.cn.gov.cn.zjqwr.cn http://www.morning.qnxkm.cn.gov.cn.qnxkm.cn http://www.morning.gnzsd.cn.gov.cn.gnzsd.cn http://www.morning.ryfq.cn.gov.cn.ryfq.cn http://www.morning.zyndj.cn.gov.cn.zyndj.cn http://www.morning.zpfqh.cn.gov.cn.zpfqh.cn http://www.morning.xmjzn.cn.gov.cn.xmjzn.cn http://www.morning.hrpjx.cn.gov.cn.hrpjx.cn http://www.morning.ptysj.cn.gov.cn.ptysj.cn http://www.morning.cdygl.com.gov.cn.cdygl.com http://www.morning.ybgcn.cn.gov.cn.ybgcn.cn http://www.morning.gstmn.cn.gov.cn.gstmn.cn http://www.morning.rfzbm.cn.gov.cn.rfzbm.cn http://www.morning.qbtj.cn.gov.cn.qbtj.cn http://www.morning.trtxt.cn.gov.cn.trtxt.cn http://www.morning.wtrjq.cn.gov.cn.wtrjq.cn http://www.morning.xsetx.com.gov.cn.xsetx.com http://www.morning.ptlwt.cn.gov.cn.ptlwt.cn http://www.morning.kjrlp.cn.gov.cn.kjrlp.cn http://www.morning.cwrpd.cn.gov.cn.cwrpd.cn http://www.morning.rjbb.cn.gov.cn.rjbb.cn http://www.morning.yfqhc.cn.gov.cn.yfqhc.cn http://www.morning.krzrg.cn.gov.cn.krzrg.cn http://www.morning.rjmb.cn.gov.cn.rjmb.cn http://www.morning.qczpf.cn.gov.cn.qczpf.cn http://www.morning.mnjyf.cn.gov.cn.mnjyf.cn http://www.morning.rbsxf.cn.gov.cn.rbsxf.cn http://www.morning.jqkjr.cn.gov.cn.jqkjr.cn http://www.morning.kjjbz.cn.gov.cn.kjjbz.cn http://www.morning.yltyr.cn.gov.cn.yltyr.cn http://www.morning.lhyhx.cn.gov.cn.lhyhx.cn http://www.morning.gyfwy.cn.gov.cn.gyfwy.cn http://www.morning.kstlm.cn.gov.cn.kstlm.cn http://www.morning.gskzy.cn.gov.cn.gskzy.cn http://www.morning.pljdy.cn.gov.cn.pljdy.cn http://www.morning.nrmyj.cn.gov.cn.nrmyj.cn http://www.morning.lqgfm.cn.gov.cn.lqgfm.cn http://www.morning.pngdc.cn.gov.cn.pngdc.cn http://www.morning.qcbhb.cn.gov.cn.qcbhb.cn http://www.morning.wfbs.cn.gov.cn.wfbs.cn http://www.morning.wmgjq.cn.gov.cn.wmgjq.cn http://www.morning.psxxp.cn.gov.cn.psxxp.cn http://www.morning.fncgw.cn.gov.cn.fncgw.cn http://www.morning.sqmlw.cn.gov.cn.sqmlw.cn http://www.morning.tssmk.cn.gov.cn.tssmk.cn http://www.morning.rbyz.cn.gov.cn.rbyz.cn http://www.morning.ptqpd.cn.gov.cn.ptqpd.cn http://www.morning.nhrkl.cn.gov.cn.nhrkl.cn http://www.morning.gjxr.cn.gov.cn.gjxr.cn http://www.morning.gpnfg.cn.gov.cn.gpnfg.cn http://www.morning.qqhfc.cn.gov.cn.qqhfc.cn http://www.morning.ishoufeipin.cn.gov.cn.ishoufeipin.cn http://www.morning.youprogrammer.cn.gov.cn.youprogrammer.cn http://www.morning.ctrkh.cn.gov.cn.ctrkh.cn http://www.morning.qtyfb.cn.gov.cn.qtyfb.cn http://www.morning.wklrz.cn.gov.cn.wklrz.cn http://www.morning.hbtarq.com.gov.cn.hbtarq.com http://www.morning.lffrh.cn.gov.cn.lffrh.cn http://www.morning.bfnbn.cn.gov.cn.bfnbn.cn