C++ 基础入门篇6.3:值传递

值传递:就是函数调用时实参将数值传入形参

值传递,如果形参发生,并不会影响实参

#include<iostream>
using namespace std;

// 值传递
// 定义函数 实现两个数字进行交换函数

// 如果函数不需要返回值,声明时候可以写void

void swap(int num1, int num2)
{
	cout << "交换前:" << endl;
	cout << "num1= " << num1 << endl;
	cout << "num2= " << num2 << endl;

	// return;返回值不需要的时候,可以不写return
	int temp = num1;
	num1 = num2;
	num2 = temp;

	cout << "交换后:" << endl;
	cout << "num1= " << num1 << endl;
	cout << "num2= " << num2 << endl;
}


int main()
{
	int a = 10;
	int b = 20;

	// 我们做值传递时候,函数的形参发生改变时,并不会影响实参
	swap(a, b);

	system("pause");

	return 0;

}

  • 8
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

万喵字节

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值