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

【c++】函数

文章目录

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


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

函数的定义

返回值类型 函数名 参数列表
{函数体语句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://www.lryc.cn/news/4165.html

相关文章:

  • [golang gin框架] 1.Gin环境搭建,程序的热加载,路由GET,POST,PUT,DELETE
  • 【开源】祁启云网络验证系统V1.11
  • 震源机制(Focal Mechanisms)之沙滩球(Bench Ball)
  • C++入门:多态
  • 华为OD真题_工位序列统计友好度最大值(100分)(C++实现)
  • [ruby on rails]MD5、SHA1、SHA256、Base64、aes-128-cbc、aes-256-ecb
  • 《NFL星计划》:拉斯维加斯突袭者·橄榄1号位
  • 韩顺平Linux基础学习(1)
  • Rust学习入门--【6】Rust 基础语法
  • LINUX提权入门手册
  • MSI_MSI-X中断之源码分析
  • Docker--consul
  • ESP-01S使用AT指令连接阿里云
  • 【Kafka】【三】安装Kafka服务器
  • 关于适配器模式,我遗漏了什么
  • SQL Serve 日志体系结构
  • 【C++1】函数重载,类和对象,引用,string类,vector容器,类继承和多态,/socket,进程信号,public,ooci
  • asio网络编程 tcp、udp、rpc
  • 双目测距------双目相机V1.0,将双目相机采集到任意一点的深度数据进行串口传输(带源码)
  • jetson nano(ubuntu)安装Cmake
  • 图的基本介绍和表示方式
  • 本周大新闻|传微软解散工业元宇宙团队,MIT研发垂直堆叠全彩Micro LED
  • SpringMVC:拦截器(12)
  • 计算机网络3:HTTP1.0和HTTP1.1的区别
  • Urho3D 编辑器说明
  • C++类基础(十一)
  • Windows安装系列:SVN Server服务
  • 快速傅里叶算法(FFT)快在哪里?
  • 利用Markdown写学术论文资料汇总贴
  • MySQL 高级查询