函数(c++)✍(自学总结笔记)

函数

1、概述

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

2、函数的定义

五个步骤:

  • 返回值类型:一个函数可以返回一个值,在函数中定义
  • 函数名:给函数起个名字
  • 参数列表:使用该函数传入的数据
  • 函数体语句:花括号内的代码,函数内需要执行的语句
  • return表达式:和返回值类型挂钩,函数执行完毕后,返回相应的数据
    语法:
返回值类型 函数名 (参数列表)
{

       函数体语句

       return表达式

}

示例: 定义一个加法函数,实现两个数相加

int add(int add1, int add2)
{
	int sum = add1 + add2;
	return sum;
}

3、函数的调用

  • ** 功能:** 使用定义好的函数
  • 语法: 函数名(参数)
  • 函数定义里小括号称为形参,函数调用时传入的参数称为实参
    示例:
#include<iostream>
using namespace std;

int add(int add1, int add2)//此处的add1和add2为形参
{
	int sum = add1 + add2;
	return sum;
}

int main()
{
	int a = 6;
	int b = 8;
	//调用函数
	int sum = add(a, b);//此处a,b为实参
	cout << "sum = " << sum << endl;
	system("pause");
	return 0;
}
运行结果:
sum = 14
请按任意键继续. . .

4、值传递

  • 所谓值传递,就是函数调用时将实参数值传入给形参
  • 值传递时,如果形参发生,并不会影响实参
    示例:
#include<iostream>
using namespace std;

void swap(int num1, int num2)
{
	cout << "交换前:" << endl;
	cout << "num1 = " << num1 << "   " << "num2 = " << num2 << endl;
	int temp = num1;
	num1 = num2;
	num2 = temp;
	cout << "交换后:" << endl;
	cout << "num1 = " << num1 << "   " << "num2 = " << num2 << endl;
}
int main()
{
	int a = 10;
	int b = 20;
	swap(a, b);
	cout << "main中的a = " << a << "    " << "main中的b = " << b << endl;
	system("pause");
	return 0;
}
运行结果:
交换前:
num1 = 10   num2 = 20
交换后:
num1 = 20   num2 = 10
main中的a = 10    main中的b = 20
请按任意键继续. . .

5、函数的常见样式

  1. 无参无返
  2. 有参无返
  3. 无参有返
  4. 有参有返

示例:

#include<iostream>
using namespace std;

//1、无参无返回
void test1()
{
	//void a= 10;//无类型不可以穿件变量,原因是无法分配内存
	cout << "this is test1" << endl;
}
//2、有参无返回
void test2(int a)
{
	cout << "this is test02" << endl;
	cout << "a = " << a << endl;
}
//3、无参有返
int test3()
{
	cout << "this is test3" << endl;
	return 20;
}
//4、有参有返回
int test4(int a, int b)
{
	cout << "this is test04 " << endl;
	int sum = a + b;
	return sum;
}
int main()
{
	test1();
	int a = 10;
	test2(a);
	int b = test3();
	int sum = test4(a, b);
	cout <<"sum = " << sum << endl;
	system("pause");
	return 0;
}
运行结果:
this is test1
this is test02
a = 10
this is test3
this is test04
sum = 30
请按任意键继续. . .

6、函数的声明

  • 作用: 告诉编译器函数名称及如何调用函数。函数实际主体可以单独定义。
  • 函数的声明可以多次,但是函数的定义只能有一次

示例:

#include<iostream>
using namespace std;

//声明可以多次,定义只能一次
//声明
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 = 100;
	int b = 200;

	cout << max(a, b) << endl;

	system("pause");

	return 0;
}

7、函数在分文件编写

  • 作用: 让代码结构更加清晰

函数分文件编写一般有四个步骤

  • 创建后缀名为.h的头文件
  • 穿件后缀名为.cpp的源文件
  • 在头文件中写函数的声明
  • 在源文件中写函数的定义

示例:

//swap.h文件
#include<iostream>
using namespace std;

//实现两个数字交换的函数声明
void swap(int a, int b);

//swap.cpp文件
#include "swap.h"

void swap(int a, int b)
{
	int temp = a;
	a = b;
	b = temp;

	cout << "a = " << a << endl;
	cout << "b = " << b << endl;
}

//main函数文件
#include "swap.h"
int main() {

	int a = 100;
	int b = 200;
	swap(a, b);

	system("pause");

	return 0;
}
运行结果:
a = 200
b = 100
请按任意键继续. . .
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值