C++自学笔记

06 函数

本次记录函数,还请各位大佬批评指正!

两数相加

    #include<iostream>
	using namespace std;
	
	int add(int a, int b)
    {
    	int sum = a + b;
	    return sum;
    }
    
    int main()
    {
    int a, b;  //a,b为实参
	cout << "输入第一个数" << endl;
	cin >> a;
	cout << "输入第二个数" << endl;
	cin >> b;
	cout << "两数和为" << add(a, b) << endl; //调用函数的时候,实参的值会传递给形参
    
    system("pause");
	return 0;
    }

值传递

函数调用时将实参的数值传给形参;
值传递时,形参发生变化,并不影响实参

    #include<iostream>
	using namespace std;
	
	void swap(int a, int b) //两个数字交换;不需要返回值,声明的时候可以写void
	{
		cout << "交换前:" << endl;
		cout << "a=" << a << endl;
		cout << "b=" << b << endl;

		int temp = a;
		a = b;
		b = temp;

		cout << "交换后:" << endl;
		cout << "a=" << a << endl;
		cout << "b=" << b << endl;
		return;
	    }
    
    int main()
    {
    int a, b;  //a,b为实参
	cout << "输入第一个数" << endl;
	cin >> a;
	cout << "输入第二个数" << endl;
	cin >> b;
	cout << "交换前:" << endl;
	cout << "a=" << a << endl;
	cout << "b=" << b << endl;
	swap(a, b);
	cout << "交换后:" << endl;
	cout << "a=" << a << endl;
	cout << "b=" << b << endl;
    
    system("pause");
	return 0;
    }

函数常见样式

无参无返;有参无返;无参有返;有参有返

    #include<iostream>
	using namespace std;
	
	void test01()  //无参无返
{
	cout << "test01" << endl;
}

void test02(int a)  //有参无返
{
	cout << "test02" << endl;
}

int test03()  //无参有返
{
	//cout << "test03" << endl;
	return 100;
}

int test04(int a)  //有参有返
{
	cout << "test03" << endl;
	return a;
}
    
    int main()
    {
    test01();
    
	test02(100);

	test03();
	cout << "b=" << test03() << endl;

	int b;
	b=test04(100);
	cout << "b=" << b << endl;
    
    system("pause");
	return 0;
    }

函数的声明

在main前声明函数,就可以在main之后定义函数。如果不声明,在main之后定义函数就会报错,main中的程序找不到自己定义的函数。

    #include<iostream>
	using namespace std;
	
	//函数的声明   声明可以写多次,定义只能写一次
    int max(int a, int b);
    
    int main()
    {
    //比较个值,返回较大的
    int a, b;  //a,b为实参
	cout << "输入第一个数" << endl;
	cin >> a;
	cout << "输入第二个数" << endl;
	cin >> b;
	cout << "较大的数为" << max(a,b) << endl;
    
    system("pause");
	return 0;
    }
    
    int max(int a, int b)
    {
    	return a > b ? a : b;
    }

函数的分文件编写

以交换函数为例。
1、创建.h后缀名的头文件
工程文件中找到头文件,右键,添加,新建项,Visual C++,C++文件,名称swap.h,添加,完成。

2、创建.cpp后缀名的源文件
工程文件中找到源文件,右键,添加,新建项,Visual C++,C++文件,名称swap.cpp,添加,完成。

3、在头文件里写函数声明

    //这里边写函数的声明
    #include<iostream>
    using namespace std;
    void swap(int a, int b);

4、在源文件里写函数定义

#include"swap.h"  //跟头文件swap.h完成关联
//交换函数的定义
void swap(int a, int b) //两个数字交换;不需要返回值,声明的时候可以写void
{
	int temp = a;
	a = b;
	b = temp;
	cout << "a=" << a << endl;
	cout << "b=" << b << endl;
}
    #include<iostream>
    #include"swap.h"  //自己定义的函数要包含到头文件里,用双引号
	using namespace std;
    
    int main()
    {
    int a, b;  //a,b为实参
	cout << "输入第一个数" << endl;
	cin >> a;
	cout << "输入第二个数" << endl;
	cin >> b;
	swap(a, b);
    
    system("pause");
	return 0;
    }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值