函数的基本学习

函数

1.概述

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

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

2.函数的定义

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

1.返回值类型

2.函数名

3.参数表列

4.函数体语句

5.return表达式

语法:

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

定义一个add函数

#include<iostream>
using namespace std;
​
​
int add(int num1, int num2)
{
    int sum = num1 + num2;
    return sum;
}
int main()
{
    return 0;
}

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;
    //函数调用语法:函数名称(参数)
    //a和b称为实际参数,简称实参
    //当调用函数的时候,实参的值会传递给形参
    int c = add(a, b);
    cout << c<<endl;
​
    a = 347;
    b = 426;
    c = add(a, b);
    cout << c;
    return 0;
}

运行结果如图:

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 = 0;
    temp = num1;
    num1 = num2;
    num2 = temp;
​
    cout << "交换后" << endl;
    cout << "num1 =" << num1 << endl;
    cout << "num2 =" << num2 << endl;
​
​
    //return;返回值不需要的时候,可以不写return
}
​
int main()
{
    int a = 19;
    int b = 183;
​
    cout << "a=" << a << endl;
    cout << "b=" << b << endl;
​
    swap(a, b);
​
    cout << "a=" << a << endl;
    cout << "b=" << b << endl;
    
    return 0;
}

运行结果:

值递时,形参发生任何改变,实参都不会有变化

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 100;
}
​
//4.有参有返
int test04(int a)
{
    cout << "this is test04  a=" <<a<< endl;
    return 10;
}
​
int main()
{
    //无参无返函数调用
    test01();
​
    //有参无返函数调用
    test02(100);
​
    //无参有返函数调用
    int b = test03();
    cout << b << endl;
​
    //有参有返函数调用
    int c = test04(35);
    cout << c << endl;
​
    return 0;
}

运行结果:

 

6.函数的声明

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

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

函数的定义一般写在main函数前,写在main函数后会报错

若想写在main函数后不报错,需要先声明函数

利用函数的声明,提前告诉编译器函数的存在

示例:

#include<iostream>
using namespace std;
​
//函数的声明
//比较函数,实现两个整型数字进行比较,返回较大的值
​
//提前告诉编译器函数的存在,可以利用函数的声明
//函数的声明
//声明可以写多次,定义只能有一次
​
int max(int a, int b);
​
int main()
{
    int a = 10;
    int b = 30;
​
    cout << max(a, b) << endl;
​
    return 0;
}
​
//定义
int max(int a, int b)
{
    return a > b ? a : b;
}

声明可以写多次,但是没必要,一般只写一次

运行结果:

 

7.函数的分文件编写

作用:让代码结构更清晰

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

1.创建后缀名为.h的头文件

2.创建后缀名为.cpp的源文件

3.在头文件中写函数的声明

4.在源文件写函数的定义

示例:

#include<iostream>
using namespace std;
#include "swap.h"
​
//函数的分文件编写
//实现两个数字进行交换的函数
​
函数的声明
//void swap(int a, int b);
​
函数的定义
//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);
}

1.创建后缀名为.h的头文件

 

右键,添加,新建项

 

创建函数名.h文件

 

点击添加

2.创建后缀名为.cpp的头文件

 

点击源文件,右键,添加,新建项

 

创建函数名.cpp的源文件,点击添加

3.在头文件中写函数的声明(swap.h)

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

4.在源文件写函数的定义(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;
#include "swap.h"//加上头文件,即可调用
​
int main()
{
    int a = 10;
    int b = 20;
    swap(a, b);//函数调用
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值