zoukankan      html  css  js  c++  java
  • [C++] 用Xcode来写C++程序[4] 函数

    用Xcode来写C++程序[4] 函数

    此节包括引用函数,内联函数,防止修改函数入参,函数自身带有默认值.

    引用函数:防止复制对象,减少系统开销

    内联函数:编译的时候根据具体情形将代码嵌入进去,成不成功编译器说了算,减少系统开销提升性能

    引用函数(防止篡改初始值的入参声明方式):防止修改数据源

    函数参数带有默认值:函数的某个参数可以给定默认值,精简函数的使用

    最简单的函数

    #include <iostream>
    using namespace std;
    
    int addition (int a, int b) {
        return a + b;
    }
    
    int main () {
        int z;
        z = addition (5,3);
        cout << "The result is " << z << endl;
    }

    打印结果

    The result is 8
    Program ended with exit code: 0

    传递引用(int& 表示)

    #include <iostream>
    using namespace std;
    
    void duplicate (int& a, int& b, int& c) {
        a *= 2;
        b *= 2;
        c *= 2;
    }
    
    int main () {
        int x = 1, y = 3, z = 7;
        duplicate (x, y, z);
        cout << "x=" << x << ", y=" << y << ", z=" << z << endl;
        
        return 0;
    }

    打印结果

    x=2, y=6, z=14
    Program ended with exit code: 0

    防止篡改数据源(const 修饰变量)

    #include <iostream>
    #include <string>
    using namespace std;
    
    string concatenate (const string& a, const string& b) {
        return a + b;
    }
    
    int main () {
        
        string x = "You";
        string y = "XianMing";
    
        cout << concatenate(x, y) << endl;
        
        return 0;
    }

    打印结果

    YouXianMing
    Program ended with exit code: 0

    内联函数(减少函数调用开销)

    #include <iostream>
    #include <string>
    using namespace std;
    
    inline string concatenate (const string& a, const string& b) {
        return a + b;
    }
    
    int main () {
        
        string x = "You";
        string y = "XianMing";
    
        cout << concatenate(x, y) << endl;
        
        return 0;
    }

    打印结果

    YouXianMing
    Program ended with exit code: 0

    带默认值的函数(如果不赋值,则有一个默认值)

    #include <iostream>
    using namespace std;
    
    int divide (int a, int b = 2) {
        int r;
        r = a / b;
        return (r);
    }
    
    int main () {
        cout << divide (12)    << endl;
        cout << divide (20, 4) << endl;
        
        return 0;
    }

    打印结果

    YouXianMing
    Program ended with exit code: 0

    函数先声明,后使用

    #include <iostream>
    using namespace std;
    
    void odd (int x);
    void even (int x);
    
    int main() {
        int i;
        do {
            cout << "Please, enter number (0 to exit): ";
            cin >> i;
            odd (i);
        } while (i!=0);
        
        return 0;
    }
    
    void odd (int x)
    {
        if ((x%2)!=0) cout << "It is odd.
    ";
        else even (x);
    }
    
    void even (int x)
    {
        if ((x%2)==0) cout << "It is even.
    ";
        else odd (x);
    }

    递归调用

    #include <iostream>
    using namespace std;
    
    long factorial (long a) {
        if (a > 1)
            return (a * factorial (a-1));
        else
            return 1;
    }
    
    int main () {
        long number = 9;
        cout << number << "! = " << factorial (number);
        return 0;
    }

    打印结果

    9! = 362880
    Program ended with exit code: 0
  • 相关阅读:
    luogu1197 [JSOI2008]星球大战
    luogu2085 最小函数值
    Vijos 1144 小胖守皇宫 【树形DP】
    洛谷 P1941 飞扬的小鸟 【DP+众多特判】
    codevs 1516 平均分数 【数学推理+求逆序对】
    tyvj 1936 太空战队 【强连通分量】
    USACO 2.4 Overfencing 【种子染色法+递推】
    code[vs] 2488 绿豆蛙的归宿【反向拓扑+DP】
    USACO 2.3 Zero Sum 【搜索+字符串处理+模拟计算】
    USACO 2.3 Cow Pedigrees 【DP+前缀和优化】
  • 原文地址:https://www.cnblogs.com/YouXianMing/p/4322843.html
Copyright © 2011-2022 走看看