(五)C++—函数的调用、声明、数值交换、分文件编写

1.函数的定义

1.返回值类型
2.函数名
3.参数表列
4.函数体语句
5.return 表达式

返回值类型 函数名 (参数列表) int add()
{
函数集语句
return 表达式
}

#include<iostream>
using namespace std;

//函数的定义
//语法
//返回值类型 函数名(参数列表){函数体语句 return表达式}
int add(int num1, int num2)
{
	int sum = num1 + num2;
	return sum;
}

int main()
{
	system("pause");
	return 0;
}

2函数的调用

#include<iostream>
using namespace std;

//函数的定义
//语法
//返回值类型 函数名(参数列表){函数体语句 return表达式}
int add(int num1, int num2)//num 1 num 2称为形参
{
	int sum = num1-num2;
	return sum;
}

//函数的调用
int main()
{
	int a = 10;//a,b实际参数
	int b = 10;//
	//调用函数语法
	int c = add(a,b);
	cout <<"c="<< c << endl;

	system("pause");
	return 0;
}


3函数数值交换

#include<iostream>
using namespace std;

//值传递:就是函数调用的时候,实参将数值调用到形参
//值传递的时候,如果形参发生变化,并不会影响到实参。
//值传递
//定义函数,实现两个数字进行交换函数

//如果函数不需要返回值,声明的时候void(无类型的)
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;//a,b实际参数
	int b = 30;//
	//调用函数语法
	swap(a,b);//实现一个交换
	cout << "a=" << a << endl;
	cout << "b=" << b << endl;

	system("pause");
	return 0;
}

4函数的常见样式

无参无返
有参无返

#include<iostream>
using namespace std;

//1 无参无返
void test01()
{
	cout << "this is rapper kdd" << endl;
}
//2 有参无返
void test02( int a)//void 无类型
{
	cout << "this is rapper kdd2" <<a<< endl;

}
//3.无参数有返
int test03()
{
	cout << "this is rapper kdd 3" << endl;
	return 1000;
}
//4.有参有返
int test04( int a)
{
	cout << "this is rapper kdd 3 a=" << endl;
	return a;
}

int main()//主函数
{
	test02 (100);

	int num1 = test03();
	cout << "num1=" << num1 << endl;
	//有参有返
	int num2 = test04 (10000);
	cout << "num2=" << num2 << endl;
	system("pause");
	return 0;
}

5函数的声明

#include<iostream>
using namespace std;

//函数的声明
//比较函数,实现两个整型数字进行比较,返回较大的值

//提前告诉编译器函数的存在,可以利用函数的声明。
//声明可以写多次,但是定义只能有一次
int max(int a, int b);
int max(int a, int b);
int max(int a, int b);

int max(int a, int b)
{
	return a > b ? a : b;
}

int main()//主函数
{
	int a = 10;
	int b = 20;
	cout << max(a,b) << endl;
	system("pause");
	return 0;
}


6函数的分文件编写(以后大型的项目开发需要)

一般4个 步骤
1创建 .h的头文件
2.创建 .cpp的源文件
3.在头文件.h中写函数的声明
4.在源文件.cpp中写函数的定义

(1)首先 需要创建swap.h的头文件,上面写上 函数的声明。

#include<iostream>
using namespace std;//框架得加上

void swap(int a, int);//函数的声明

(2)创建swap.cpp的源文件,写上 函数的定义 void()

#include"swap.h"//这个极其重要 需要加上
//函数的定义
void swap(int a, int b)
{
	int temp = a;
	a = b;
	b = temp;
	cout << "a=" << a << endl;
	cout << "b=" << b << endl;
}

(3)回到主文件 ,使用主函数 运行求解,同时一定调用swap.h头文件

#include<iostream>
using namespace std;
#include "swap.h"//必须包含swap的头文件
//函数的分文件编写

//void swap(int a, int);//函数的声明
//void swap(int a, int b)
//{
//	int temp = a;
//	a = b;
//	b = temp;
//	cout << "a=" << a << endl;
//	cout << "b=" << b << endl;
//}
//1创建 .h的头文件
//2.创建.cpp的源文件
//3.在头文件.h中写函数的声明
//4.在源文件cpp中写函数的定义


int main()//主函数
{
	int a = 10;
	int b = 20;
	swap(a, b);//函数的调用

	cout << "a=" << a << endl;
	cout << "b=" << b << endl;
	
	system("pause");
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

TillerB

各位土豪赏点钱,帮我买条秋裤!

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

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

打赏作者

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

抵扣说明:

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

余额充值