C++学习7(函数)

main函数代码

#include <iostream>
#include"20210519_2swap2.h"
using namespace std;


/*函数定义五个步骤:
	1.返回值类型
	2.函数名
	3.参数表列
	4.函数语句
	5.return表达式*/

	/*返回值类型   函数名(参数列表)
	{
		函数体语句

		return表达式
	}*/
//加法函数,实现两个整型相加,并且将相加结果返回
int add(int num1, int num2)
{
	int sum = num1 + num2;
	return sum;
	}//num1和num2并没有真实数据,只是形式上的参数,简称形参
//实现两个数交换
void swap(int num3, int num4)//如果函数不需要返回值,声明时可以写void
{
	cout << "交换前" << endl;
	cout << "num3=" << num3 << endl;
	cout << "num4=" << num4 << endl;
	int temp = num3;
	num3 = num4;
	num4 = temp;
	cout << "交换后" << endl;
	cout << "num3=" << num3 << endl;
	cout << "num4=" << num4 << endl;

	return;
}
//注意:自定义函数要放在main函数前面,要先声明后使用。 当然也有可放在后边的,自行查找百度经验
//函数的常见形式
	/*1.无参无返
	2.有参无返
	3.无参有返
	4.有参有返*/
void  test1()//无参无返
{
	cout << "this  is  test1" << endl;
}
void test2(int a)//有参无返
{
	cout << "this  is  test2  a= " << a << endl;
}
int test3()//无参有返
{
	cout << "this  is  test3  " << endl;
	return 1000;
}
int test4(int a)//有参有返
{
	cout << "this is test4 a= " << a << endl;
	return a;
}

//函数的声明
//提前告诉编译器函数的存在,可以利用函数的声明(解决函数放在main函数后无法调用怎么办)
int max(int a, int b);
//注意函数的声明和函数定义的区别(函数的声明只写:返回值类型  函数名(形参))
//具体定义在main函数后
//函数声明可以写多次,但是定义只能写一次

/*函数的分文件编写(作用:让代码结构更加清晰
函数分文件的编写一般有四个步骤
	1.创建后缀名为.h的头文件
	2.创建后缀名为.cpp的源文件
	3.在头文件中写函数的声明
	4.在源文件中写函数的定义
	//实现梁个数字进行交换
声明
void swap2(int a, int b);
定义
void swap2(int a, int b)
{
	int temp = a;
	a = b;
	b = temp;
	cout << "a=" << a << endl;
	cout << "b=" << b << endl;
}*/
int main()
{
	//main函数中调用add函数
	cout << "函数调用实现两数相加" << endl;
	int a = 10;
	int b = 20;//a和b称为实际参数,简称实参
	//函数调用语法:函数名 (参数)
	int c = add(a, b);
	//当调用函数时候,实参的值会传递给形参
	cout << "c=" << c << endl;
	cout << "\n " << endl;
	//值传递
	//所谓值传递就是函数调用时实参将值传入形参,值传递时,如果形参发生任何改变不会影响实参

	int m = 22;
	int n = 33;
	cout << "m=" << m << endl;
	cout << "n=" << n << endl;
	//当做值传递时,函数的形参发生改变并不会影响实参
	swap(m, n);
	cout << "m=" << m << endl;
	cout << "n=" << n << endl;
	cout << "\n " << endl;
	//函数几种常见形式部分
	cout << "函数几种常见形式的调用" << endl;
	test1();//无参无返函数的调用
	test2(100);//有参无返函数的调用,此处不写100会报错,不写参数不行
	int num5 = test3();//无参有返函数的调用
	cout << num5 << endl;
	int num6 = test4(10000);//有参有返的函数调用
	cout << "num6=" << num6 << endl;
	cout << "\n " << endl;
	
	
	//函数声明部分
	cout << "函数声明部分输出" << endl;
	int x = 400;
	int y = 500;
	cout << max(x, y) << endl;
	cout << "\n " << endl;

	//函数分文件编写部分
	cout << "函数分文件编写" << endl;
	swap2(55, 66);

	cout << "\n " << endl;
	system("pause");
	return 0;

	
}

//函数的声明
//比较函数,实现两个整型数字进行比较,返回较大的值
int max(int a, int b)
{
	return a > b ? a : b;
}

头文件.h文件中的函数声明

#include <iostream>
using namespace std;
//声明
void swap2(int a, int b);

源文件.cpp中的函数定义

#include"20210519_2swap2.h"
//定义
void swap2(int a, int b)
{
	int temp = a;
	a = b;
	b = temp;
	cout << "a=" << a << endl;
	cout << "b=" << b << endl;
}

使用vs编写C++代码的文件目录

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值