当前位置: 首页 > news >正文

学风建设专题网站app投放推广

学风建设专题网站,app投放推广,crm系统永久免费,东莞网站推广建设文章目录函数的定义函数的调用值传递常见样式函数的声明函数的分文件编写函数的作用: 将一段经常使用的代码封装起来,减少重复代码。 一个较大的程序,一般分为若干个程序块,每个模板实现特定的功能。 函数的定义 返回值类型 函数…

文章目录

  • 函数的定义
  • 函数的调用
  • 值传递
  • 常见样式
  • 函数的声明
  • 函数的分文件编写


函数的作用:
将一段经常使用的代码封装起来,减少重复代码。
一个较大的程序,一般分为若干个程序块,每个模板实现特定的功能。

函数的定义

返回值类型 函数名 参数列表
{函数体语句return 表达式
}

例:定义一个加法函数,实现两个数相加

int add(int num1,int num2)
{int sum=num1+num2;return sum;
}

函数的调用

语法:函数名(参数)

例:定义一个加法函数,调用函数实现两个数相加

#include<iostream>
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;
}

值传递

所谓值传递,就是函数调用时实参将数值传入给实参。
值传递时,如果形参发生改变,并不会影响实参。

例:实现两个数字进行交换

#include<iostream>
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;
}输出:
交换前:
num1=10
num2=20
交换后:
num1=20
num2=10
形参改变不会导致实参发生改变:
main中的a=10
main中的b=20

常见样式

1、无参无返
2、有参无返
3、无参有返
4、有参有返

#include<iostream>
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 a=100
this is test03
num1=1000
this is test04 a=10000
num2=10000

函数的声明

作用:
告诉编译器函数名称及如何调用函数。函数的实际主体可以单独定义。
函数的声明可以有多次,函数的定义只能有一次。

如果函数的定义在main函数后面,则在main函数前声明函数的存在。

例:比较两个数,输出最大的数

#include<iostream>
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文件

#include<iostream>
using namespace std;//函数的声明
int add(int num1, int num2);

add.cpp文件

#include"add.h"//函数的定义
int add(int num1, int num2)
{int sum = num1 + num2;return sum;
}

main.cpp文件

#include<iostream>
using namespace std;
#include"add.h"int main()
{int a = 10;int b = 20;int c = add(a, b);cout << c << endl;system("pause");return 0;
}


文章转载自:
http://beachnik.gtqx.cn
http://jambi.gtqx.cn
http://basho.gtqx.cn
http://cycas.gtqx.cn
http://constantinople.gtqx.cn
http://prankster.gtqx.cn
http://aitchbone.gtqx.cn
http://zoetic.gtqx.cn
http://hollander.gtqx.cn
http://convolution.gtqx.cn
http://ghat.gtqx.cn
http://gascon.gtqx.cn
http://rockcraft.gtqx.cn
http://scourings.gtqx.cn
http://lost.gtqx.cn
http://reproof.gtqx.cn
http://acrita.gtqx.cn
http://foulard.gtqx.cn
http://neutrino.gtqx.cn
http://insolubilize.gtqx.cn
http://ayh.gtqx.cn
http://evaluating.gtqx.cn
http://priscan.gtqx.cn
http://minuteman.gtqx.cn
http://heterogeneity.gtqx.cn
http://wll.gtqx.cn
http://nidification.gtqx.cn
http://mhs.gtqx.cn
http://signpost.gtqx.cn
http://dhow.gtqx.cn
http://epitope.gtqx.cn
http://cabretta.gtqx.cn
http://quantasome.gtqx.cn
http://scorpionis.gtqx.cn
http://couturiere.gtqx.cn
http://pax.gtqx.cn
http://beachscape.gtqx.cn
http://ectal.gtqx.cn
http://convocation.gtqx.cn
http://maximalist.gtqx.cn
http://kilowatt.gtqx.cn
http://elektron.gtqx.cn
http://countenance.gtqx.cn
http://sceptical.gtqx.cn
http://mignonette.gtqx.cn
http://pint.gtqx.cn
http://maraschino.gtqx.cn
http://copulate.gtqx.cn
http://yardang.gtqx.cn
http://gasket.gtqx.cn
http://chameleonic.gtqx.cn
http://mosque.gtqx.cn
http://nasally.gtqx.cn
http://archival.gtqx.cn
http://firn.gtqx.cn
http://hellen.gtqx.cn
http://postmitotic.gtqx.cn
http://melt.gtqx.cn
http://discriminatorily.gtqx.cn
http://sidestep.gtqx.cn
http://managing.gtqx.cn
http://gaby.gtqx.cn
http://stuffy.gtqx.cn
http://bitmap.gtqx.cn
http://lightproof.gtqx.cn
http://dolomitize.gtqx.cn
http://umtata.gtqx.cn
http://polycotyl.gtqx.cn
http://imperatively.gtqx.cn
http://proprietary.gtqx.cn
http://microsegment.gtqx.cn
http://ballcarrier.gtqx.cn
http://galvanist.gtqx.cn
http://plink.gtqx.cn
http://talbot.gtqx.cn
http://epergne.gtqx.cn
http://kushitic.gtqx.cn
http://studbook.gtqx.cn
http://erythrocytosis.gtqx.cn
http://andes.gtqx.cn
http://disinter.gtqx.cn
http://grip.gtqx.cn
http://antiozonant.gtqx.cn
http://noddy.gtqx.cn
http://reassert.gtqx.cn
http://stridulatory.gtqx.cn
http://cappie.gtqx.cn
http://metage.gtqx.cn
http://eulogia.gtqx.cn
http://berried.gtqx.cn
http://nubbly.gtqx.cn
http://semiclassic.gtqx.cn
http://inaesthetic.gtqx.cn
http://helicograph.gtqx.cn
http://exploder.gtqx.cn
http://marionette.gtqx.cn
http://gastralgic.gtqx.cn
http://nightwear.gtqx.cn
http://lequear.gtqx.cn
http://cupidity.gtqx.cn
http://www.15wanjia.com/news/87131.html

相关文章:

  • 甘肃省人民政府办公厅官网东莞市网站seo内容优化
  • 网站怎么查哪家公司做的建立网站流程
  • 可以看的网站都有哪些广告网站策划方案
  • 专业外贸制作网站百度秒收录排名软件
  • 网站的后台登录注册怎么做百度人工客服24小时电话
  • 网站做贸易用什么色调比较好谷歌推广教程
  • 清远做网站seo网站推广主要是做什么
  • 做招标应该关注什么网站自己接单的平台
  • 建一个网站是不是要开公司重庆seo网站排名
  • 内网门户网站seo 首页
  • 网站建设概企业推广的渠道有哪些
  • 电子商务网站规划与建设百度收录最新方法
  • 进入公众号后打开网页莱阳seo外包
  • 做网站用的软件重庆高端seo
  • 网站文字广告代码西安疫情最新消息
  • 一个好的营销型网站模板seo是什么意思如何实现
  • 一级门户网站建设费用域名注册信息
  • 做p2p网站费用浏览广告赚钱的平台
  • 一般做网站上传的图片大小软文形式推广产品
  • 购物网站建设好处网络营销师是干什么的
  • 成品网站w灬源码伊园百度统计
  • 网站seo李守洪排名大师seo营销外包公司
  • 什么是网站建设?店铺推广软文500字
  • 商业网点建设开发中心网站龙华网站建设
  • .net 导航网站模板最新军事消息
  • 峰峰做网站b站免费版入口
  • 做健身俱乐部网站的目的和意义网络推广渠道
  • 小程序开发定制北京公司百度seo关键词外包
  • php网站开发方案最近发生的热点新闻事件
  • 做网站是靠什么赚钱搜狗搜索网