C++从0到1的入门级教学(六)——函数

举报
ArimaMisaki 发表于 2022/08/09 00:16:00 2022/08/09
【摘要】 文章目录 6 函数6.1 概述6.2 函数的定义6.3 函数的调用6.4 值传递6.5 函数的常见形式6.6 函数的声明6.7 函数的分文件编写 6 函数 6.1 概述 作用:将一段...

6 函数

6.1 概述

作用:将一段经常使用的代码封装起来,减少重复代码。

一个较大的程序,一般分为若干个程序块,每个模块实现特定的功能。

乐趣

C++自带了一个包含函数的大型库(标准的ANSI库加上多个C++类),但真正的乐趣在于编写自己的函数,另外,如果要提升效率,可更深入学习STL和BOOST C++提供的功能。

6.2 函数的定义

函数的定义一般主要有5个步骤:

  1. 返回值类型:一个函数可以返回一个值
  2. 函数名:给函数起个名称
  3. 参数表列:使用该函数时,传入的数据
  4. 函数体语句:花括号内的代码,函数内需要执行的语句
  5. return表达式:和返回值类型挂钩,函数执行完后,返回相应的数据

语法

返回值类型 函数名 ()
{
	函数体语句
	return 表达式
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5

例如加法函数:

image-20211030112101375

关于return

对于有返回值的函数,必须使用return语句,以便将值返回给调用函数。值本身可以是常量、变量,也可以是表达式,只是其结果必须为typeName类型或可以被转换为typeName(例如:在声明的时候就说返回double,结果最后return的是一个int,那么这个int将会被强转为double)。然后函数就会把值返回给调用函数。

C++对返回值的类型有一定的限制,不能是数组,但是可以是其他任何类型,比如整数浮点数甚至是结构和对象。

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

#include <iostream>
using namespace std;

int add(int x,int y) {
	int c = x + y;
	return c;
}

int main() 
{
	int a = 1;
	int b = 2;
	int result = add(a, b);
	cout << "a和b加起来为:" << result << endl;
	system("pause");
	return 0;
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

6.3 函数的调用

功能:使用定义好的函数

语法:函数名 (参数)

示例

#include <iostream>
using namespace std;

//函数定义
int add(int num1, int num2)//定义中的num1,num2称为形式参数,简称形参
{
	int sum = num1 + num2;
	return sum;
}

int main() 
{
	int a = 10;
	int b = 10;
	//调用add函数
    //a和b称为实际参数,简称实参
	int sum = add(a, b);
	cout << "sum = " << sum << endl;

	int sum2 = add(100, 500);
	cout << "sum2 = " << sum2 << endl;

	system("pause");
	return 0;

}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

6.4 值传递

所谓值传递,就是函数调用时实参将数值传入给形参

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

示例

#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;
	int b = 20;

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

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

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

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40

值传递的原理

image-20211030213440798

【总结:值传递时,形参是修饰不了实参的。】


6.5 函数的常见形式

常见的函数样式有4种

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

示例

#include <iostream>
using namespace std;

//函数常见形式
//1、无参无反
void test01() 
{
	cout << "this is test01" << endl;
}

//2、有参无返
void test02(int a) 
{
	cout << "this is test02 a = " << a << endl;
}

//3、无参有反
int test03()
{
	cout << "this is test03" << endl;
	return 1000;
}

//4、有参有反
int test04(int a) 
{
	cout << "this is test04 a = " << a << endl;
	return a;
}

int main()
{
	test01();

	//有参无返函数调用
	test02(100);

	//无参有反函数调用
	int num1 = test03();
	cout << "num1 = " << num1 << endl;

	//有参有反函数调用
	int num2 = test04(10000);
	cout << "num2 = " << num2 << endl;

	system("pause");
	return 0;
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48

6.6 函数的声明

作用:告诉编译器函数名称以及如何调用函数,函数的实际主体可以单独定义。

  • 函数的声明可以多次,但是函数的定义只能有一次

【注:在有些书上函数声明也叫函数原型。】

示例

#include <iostream>
using namespace std;

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

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

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

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

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

6.7 函数的分文件编写

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

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

  1. 创建后缀名为.h的头文件
  2. 创建后缀名为.cpp的源文件
  3. 在头文件中写函数的声明
  4. 在源文件中写函数的定义

示例

#include "swapfunc.h"
using namespace std;
//函数的定义
void swapfunc(int a, int b)
{
	int temp = a;
	a = b;
	b = temp;

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

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
#pragma once
#include <iostream>
void swapfunc(int a, int b);

  
 
  • 1
  • 2
  • 3
#include <iostream>
#include "swapfunc.h"
using namespace std;


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

	swapfunc(a, b);
	system("pause");
	return 0;
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

文章来源: blog.csdn.net,作者:ArimaMisaki,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/chengyuhaomei520/article/details/123725975

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。