优秀的编程知识分享平台

网站首页 > 技术文章 正文

C++笔记:函数

nanyue 2024-12-31 13:38:42 技术文章 9 ℃

6.1概述

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

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

6.2 函数的定义

函数定义步骤

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

语法:

     int add(int num1, int num2)
     {
         int sum = num1 + num2;
         return sum;
     }

6.3 函数的调用

功能:使用定义好的函数

 #include<iostream>
 using namespace std;
 //num1,num2为形式参数
 int add(int num1, int num2)
 {
     int sum = num1 + num2;
     return sum;
 }
 int main()
 {
     //main函数中调用add函数
     int a = 10;
     int b = 20;
     //函数调用语法 函数名称(参数)
     int c =  add(a, b); 
     //a,b称为实际参数
     cout << c << endl;
     return 0;
 }

调用函数的时候,注意数据类型要一样

就是把实参的值给了形参,形参计算后在返回

6.4 值传递

就是实参的数给了形参

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

 #include<iostream>
 using namespace std;
 void swap(int num1, int num2)
 {
     cout << "交换前1:" << endl;
     cout << "num1 = " << num1 << endl;
     cout << "num2 = " << num2 << endl;
     int temp = num1;
     num1 = num2;
     num2 = temp;
     cout << "交换后1:" << endl;
     cout << "num1 = " << num1 << endl;
     cout << "num2 = " << num2 << endl;
 }
     int main()
     {
         int a = 10;
         int b = 20;
         cout << "交换前2" << endl;
         cout << "a = " << a << endl;
         cout << "b = " << b << endl;
         swap(a, b); 
         cout << "交换后2:" << endl;
         cout << "a = " << a << endl;
         cout << "b = " << b << endl;
         return 0;
     }


由于num1,num2(形参)发生了改变,导致a,b(实参)不会发生改变

6.5 函数的常见样式

无参无返:

 #include<iostream>
 using namespace std;
 //无参无返
 void test01()
 {
     cout << "this is test01" << endl;
 }
 int main()
 {
     //无参无返函数调用
     test01();
     return 0;
 }

有参无返:

 #include<iostream>
 using namespace std;
 //有参无返
 void test02(int a)
 {
     cout << "this is test02 a = "<< a << endl;
 }
 int main()
 {
     //有参无返函数调用
     test02(100);
     return 0;
 }

输出结果:this is test02 a = 100

 #include<iostream>
 using namespace std;
 void swap(int a, int b)
 {
     int temp = a;
     a = b;
     b = temp;
     cout << "a = "<< a << endl;
     cout << "b = "<< b << endl;
 }
 int main()
 {   
     int a = 10;
     int b = 20;
     swap(a,b);
     return 0;
 }

输出结果

a = 20

b = 10

无参有返:

 #include<iostream>
 using namespace std;
 //无参有返
 int test03()
 {
     return 1000;
 }
 int main()
 {
     //调用无参有返函数
     int num1 = test03();
     cout << num1 << endl;
     return 0;
 }

输出结果:1000

有参有返:

 #include<iostream>
 using namespace std;
 //有参有返
 int test04(int a)
 {
     cout << "this is test04 a = " << a << endl;
     return 2;
 }
 int main()
 {
     //调用有参有返函数
     int num1 = test04(1);
     cout << "num1 = "<< num1 << endl;
     return 0;
 }

输出结果:this is test04 a = 1 \n num1 = 2

解释:把1给函数test04,故a等于1,而test04函数的返回值是2,所以会返回2并赋值给num1,因此num1的值为2

6.6 函数的声明

 #include<iostream>
 using namespace std;
 int max(int a, int b)
 {
     return a > b ? a : b;
 } 
 int main()
 {
     int a = 10;
     int b = 20;
     cout << max(a, b) << endl;
     return 0;
 }

一般来说把max函数放前面,然后是main函数,因为在main函数里要调用max函数,如果反过来则无法运行,不过可以先声明max函数

 #include<iostream>
 using namespace std;
 int max(int a, int b);//函数的声明,提前告诉编译器函数max存在
 int max(int a, int b);//可以声明多次,定义只能一次
 int main()
 {
     int a = 10;
     int b = 20;
     cout << max(a, b) << endl;
     return 0;
 }
 int max(int a, int b)
 {
     return a > b ? a : b;
 }

6.7 函数的分文件编写

让代码变清晰

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

main源文件.cpp

 #include<iostream>
 using namespace std;
 #include"swap.h"//主要的源文件加上这一行即可
 int main()
 {   
     int a = 10;
     int b = 20;
     swap(a,b);
     return 0;
 }

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;
 }

其实swap.h中的#include<iostream>和using namespace std;可以放在swap.cpp中

Tags:

最近发表
标签列表