08. 函数1

函数

函数的基础知识

  1. 没有返回值的函数称为void函数

    void functionName(parameterList)  // 定义参数的时候需要加上参数类型
    {
        statemtns;
        return;  // return可选
    }
    
  2. 有返回值的函数会return一个值,这个值会被返回给调用函数

    typeName functionName(parameterList)  // 开头写上返回值类型
    {
        statements;
        return value;
    }
    
  3. 例子

    #include <iostream>
    void cheers(int);  // 函数的原型 
    double cube(double x);
    int main()
    {
     
     using namespace std;
     cheers(5);  // 调用函数 
     cout << "输入一个数字:";
     double size;
     cin >> size;
     double volume = cube(size);
     cout << "冰块的大小是:"<<volume<<"立方米"<< endl;
     return 0;
    }
    
    void cheers(int n)
    {
     using namespace std;
     for(int i=0;i<n;i++)
     {
         cout << "干杯!";
     }
     cout << endl;
    }
    
    double cube(double x){
     return x*x*x;
    }
    
    • 在主函数main()之前要写上函数的原型,具体格式如下

      typeName/void functionName(parameteList);  // 不要求提供变量名,只需要有类型列表
      
    • 在主函数main()中调用函数

    • 原型描述了函数到编译器的接口,原型会将函数返回值的类型以及参数的类型和数量告诉编译器

    • 原型可以确保编译器正确处理函数返回值;确保编译器检查使用的参数数量是否正确;确保编译器检查使用的参数类型是否正确

函数和数组

  1. 例子:函数传递数组参数

    #include <iostream>
    const int SIZE = 8;
    int arr_he(int arr[],int n); // arr是一个指针
    int main()
    {
     using namespace std;
     int cookies[SIZE] = {1,2,45,67,12,5,8,90};
     int he = arr_he(cookies,SIZE);
     cout << "总共吃了"<< he << "个烧饼\n";
     return 0;
    }
    
    int arr_he(int arr[], int n){
     int total = 0;
     for (int i=0;i<n;i++){
         total += arr[i];
     }
     return total;
    }
    

函数和二维数组

  1. 数组名是这个数组的地址,函数的形参是指针

    1. 例子

      int he(int a1[][4],int size){
          int total = 0;
          for (int i = 0;i < size;i++){
              for (int j = 0;j<4;j++){
                  total += a1[i][j]
              }
          }
          return total;
      }
      
      • 注意,这里的形参只限定了列数是4,行数没有限制,具体行数由参数size确定

函数和字符串

  1. C-风格字符串由一系列字符组成,以空值字符结尾

  2. 将c-风格字符串作为函数的参数

    1. 字符串的方式有三种:char数组;用引号括起来的字符串常量;被设置为字符串的地址的char指针

    2. 例子

      #include <iostream>
      unsigned int c_in_str(const char * str1,char ch); // 函数原型 
      int main()
      {
        using namespace std;
        char a[15] = "minium";
        
        char *b = "next";
        
        unsigned int m = c_in_str(a,'m');
        unsigned int n = c_in_str(b,'n');
        
        cout << a << "中,总共有" << m << "个字母m" <<endl;
        cout << b << "中,总共有" << n << "个字母n" <<endl;
        
        return 0;
        
       } 
       
      unsigned int c_in_str(const char * str1, char ch){
        unsigned int count = 0;
        while (*str1){  // 当str1是\0的时候结束循环 
            if (*str1 == ch){
                count++;
            }
            str1++;  // 移动指针 
        }
        return count;
      } 
      
  3. 返回C-风格字符串的函数

    1. 例子

      #include <iostream>
      char * str(char c,int n);
      int main()
      {
        using namespace std;
        int times;
        char ch;
        
        cout << "输入一个字母:";
        cin >> ch;
        
        cout << "输入一个整数:";
        cin >> times;
        
        char * pointer = str(ch,times);
        cout << pointer<< endl;
        
        delete [] pointer;  // 不要忘记释放内存
        
        pointer = str('+',20);
        cout << pointer << "分割线" << pointer << endl;
        delete [] pointer; 
        return 0;
      }
      
      char * str(char c,int n){
          // 变量pointer的作用域是函数str,当函数结束之后这个变量的内存被释放
        char * pointer = new char[n+1];
        pointer[n] = '\0';
        while (n-->0){
            pointer[n] = c;
        }
        return pointer;
      }
      

函数和结构

  1. 传递和返回结构

    1. 例子

      #include <iostream>
      // 定义一个 结构体,表示旅行的时间 
      struct travel_time{
        int hours;
        int mins;
      };
      
      const int MIN = 60;
      
      // 结构体作为参数时的函数原型 
      travel_time sum(travel_time t1,travel_time t2);
      void show_time(travel_time t);
      
      int main()
      {
        using namespace std;
        travel_time day1 = {5,45}; // 5小时45分钟
        travel_time day2 = {3,35}; // 3小时35分钟 
        
        travel_time trip = sum(day1,day2);
        cout << "两天总共的旅行时间是:\n";
        show_time(trip); 
        
        travel_time day3 = {4,16};
        cout << "三天总共的旅行时间是:\n";
        show_time(sum(trip,day3));
        return 0;
      }
      
      travel_time sum(travel_time t1,travel_time t2){
        travel_time total;
        total.mins = (t1.mins + t2.mins)% MIN;  // 计算总共的分钟数
        total.hours = t1.hours+t2.hours + (t1.mins+t2.mins) /MIN;  // 计算总共的小时数
        
        return total;
      }
      
      void show_time(travel_time t){
        using namespace std;
        cout << "旅行小时数:" << t.hours << endl;
        cout << "旅行分钟数:" << t.mins << endl;
      }
      
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 姓名:张立斐 学号:19020700001 学院:电子工程学院 转自:https://blog.csdn.net...
    zlf阅读 396评论 0 0
  • *面试心声:其实这些题本人都没怎么背,但是在上海 两周半 面了大约10家 收到差不多3个offer,总结起来就是把...
    Dove_iOS阅读 27,219评论 30 472
  • 1.new 、 delete 、 malloc 、 free 关系 delete 会调用对象的析构函数 , 和 n...
    可不期诺Cappuccino阅读 949评论 0 0
  • 写在前面: 犹豫了一下,不知道该不该发这么多。毕竟题目虽全,但是其实很多人看了不到一半,估计就会默默的收藏保存,等...
    Yt_cc阅读 14,744评论 1 6
  • 本文章分为知识点、例子和心得,交流群728483370,一起学习加油! 3.函数重载 3.1非成员函数重载 3.2...
    程序爱好者阅读 587评论 0 0