C++——函数

1,概述

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

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

2,函数的定义

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

1,返回值类型:一个函数可以返回一个值,在函数定义中。

2,函数名:给函数起个名称。

3,参数列表:使用该函数时,传入的数据。

4,函数体语句:花括号内的代码,函数内需要执行的语句。

5,return 表达式:和返回值类型挂钩,函数执行完成后,返回相应的数据。,2,语法

示例

 3,函数功能的调用

功能:使用定义好的函数

语法:函数名(参数)

示例:

#include<bits/stdc++.h>
using namespace std;
//定义加法函数
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);
    cout<<"c="<<c<<endl;
}

        需要注意的是,在函数定义时,也就是int add(int num1,int num2)时,num1和num2被称为函数的形式参数,简称形参。在main函数中调用add函数时传入的a,b被称为实际参数,简称实参。在函数调用的过程中实参的值会被传递给形式参数。

4,值传递

  • 所谓值传递,就是函数调用时实参将数值传入给形参
  • 值传递时,如果形参发生,并不会影响实参
#include<bits/stdc++.h>
using namespace std;
void swap(int num1,int num2){
	cout<<"before swap: "<<endl;
	cout<<"num1= "<<num1<<endl;
	cout<<"num2= "<<num2<<endl;
	int temp=num1;
	num1=num2;
	num2=temp;
	cout<<"after swap: "<<endl;
	cout<<"num1= "<<num1<<endl;
	cout<<"num2= "<<num2<<endl;
	//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;
}	

        可以看出,虽然以a,b为形参执行了一遍互换的操作,但是其本身的值(实参)还是没有变

原理:涉及到计算机组成原理,内存管理与分配问题

 5,函数的常见样式

常见的函数样式有4种,即在函数定义过程中函数的四种格式,他们也分别对应了四种调用方法:

1,无参无返

2,有参无返

3,无参有返

4,有参有返

示例:

#include<bits/stdc++.h>
using namespace std;
//无参无返
void test01(){
	cout<<"this is test01"<<endl;
}
//有参无返
void test02(int a){
	cout<<"this is test02 a= "<<a<<endl;
}
//无参有返
int test03(){
	cout<<"this is test03"<<endl;
	return 100;
}
//有参有返 
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;
} 

结果:

6,函数的声明

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

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

#include<bits/stdc++.h>
using namespace std;
//函数的声明
int max(int a,int b);
//函数的定义 
int max(int a,int b){
	return a>b?a:b;
} 
int main(){
	cout<<max(10,20)<<endl;
}

输出:20

7,函数的分文件编写

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

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

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

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

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

      4,在源文件中写函数的定义     

示例:  

#include<bits/stdc++.h>
using namespace std;
//函数的分文件编写
//实现两个数字进行交换的函数
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);
}

拆分为:

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

函数的分文件编写.cpp

#include<iostream>
using namespace std;
#include"swap.h" 
//函数的分文件编写
int main(){
	int a=10;
	int b=20;
	swap(a,b);
}

  • 7
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值