美文网首页
C++ 函数

C++ 函数

作者: 小潤澤 | 来源:发表于2020-03-16 19:52 被阅读0次

函数简述

函数的作用就是讲一段常用的代码封装起来,减少重复代码

函数定义:

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


#include<iostream>
using namespace std;

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

int main(){
  system("pause");
  return 0;
}

我们看到,定义一个add函数,返回值为sum

函数调用

调用已经定义的函数

#include<iostream>
using namespace std;

int add (int num1 , int num2)
{
      int sum = num1 + num2;
      return sum;
} //这里返回值数据类型为整型

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

  int c = add(a, b);
  
  cout<<"c= "<<c<<end1;
  
  a = 100;
  b = 200;

  c = add(a, b);
  cout<<"c= "<<c<<end1;

system("pause");
  return 0;
}

当a,b有实际的值的时候,我们称为实参

值传递

#include<iostream>
using namespace std;

void swap (int num1 , int num2)
{
    cout<<"交换前: "<<end1;
    cout<<"num1= "<<num1<<end1;
    cout<<"num2= "<<num2<<end1;

    int temp = num1;
    num1 = num2;
    num1 = temp;

    cout<<"交换后: "<<end1;
    cout<<"num1= "<<num1<<end1;
    cout<<"num2= "<<num2<<end1;
//不需要写return
}

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

   cout<<"a= "<<a<<end1;
   cout<<"b= "<<b<<end1;

   swap(a ,b);

   cout<<"a= "<<a<<end1;
   cout<<"b= "<<b<<end1;

  system("pause");
  return 0;
}

函数常见类型

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

#include<iostream>
using namespace std;

//1.无参无反
void test01()
{
    cout<<"this is test01"<<end1;
}
//2.有参无反
void test02(int a)
{
    cout<<"this is test02 a = "<<a<<end1;
}

//3.无参有反
void test03()
{
    cout<<"this is test02 a = "<<a<<end1;
    return 1000;
}

//4.有参有反
void test04(int a)
{
    cout<<"this is test02 a = "<<a<<end1;
    return a;
}

int main(){
  test01();
  test02(100);
  int num1 = test03();
  cout<<"num1 = "<<num1<<end1;
  int num2 = test04(200);
  cout<<"num2 = "<<num2<<end1;
  ##由于num1,num2为整型变量,所以只能输出返回值为整型的内容
  
  system("pause");
  return 0;
}

由于num1,num2为整型变量,所以只能输出返回值为整型的内容

函数声明

告诉编译器,你定义的函数是存在的
往往main函数写在你定义的函数前面,会导致报错,比如下面的写法

#include<iostream>
using namespace std;

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

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

system("pause");
  return 0;
}

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

只要我们做函数声明,就可以运行了

#include<iostream>
using namespace std;

int max(int a, int b);

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

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

system("pause");
  return 0;
}

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

这样就可以运行了,声明可以写多次,但函数定义只能写一次

函数分文件

代码长时,建议分文件
分文件:
1.创建后缀名为.h的头文件
2.创建后缀名为.h的头文件.cpp
3.在头文件中写函数的声明
4.在源文件中写函数的定义

这里我的代码将分文件写

在原始.cpp文件中

#include<iostream>
using namespace std;
#include "swap.h"//双引号

int main(){
  
  system("pause");
  return 0;
}

在.h文件中

#include<iostream>
using namespace std;

//函数声明
void swap(int a, int b);

在另一个新的.cpp文件

#include "swap.h"//双引号

 void swap (int num1 , int num2)
{
    int temp = num1;
    num1 = num2;
    num1 = temp;

    cout<<"num1= "<<num1<<end1;
    cout<<"num2= "<<num2<<end1;
//不需要写return
}

这样就完成了

我们常用的void返回值,是只无类型返回值,比较常用

相关文章

网友评论

      本文标题:C++ 函数

      本文链接:https://www.haomeiwen.com/subject/fgawjhtx.html