[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;
}
AI 代码解读

打印结果
The result is 8
Program ended with exit code: 0
AI 代码解读


传递引用( 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;
}
AI 代码解读

打印结果
x=2, y=6, z=14
Program ended with exit code: 0
AI 代码解读


防止篡改数据源(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;
}
AI 代码解读

打印结果
YouXianMing
Program ended with exit code: 0
AI 代码解读


内联函数(减少函数调用开销)
#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;
}
AI 代码解读

打印结果
YouXianMing
Program ended with exit code: 0
AI 代码解读


带默认值的函数(如果不赋值,则有一个默认值)
#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;
}
AI 代码解读

打印结果
YouXianMing
Program ended with exit code: 0
AI 代码解读

 

函数先声明,后使用

#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.\n";
    else even (x);
}

void even (int x)
{
    if ((x%2)==0) cout << "It is even.\n";
    else odd (x);
}
AI 代码解读

 

递归调用

#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;
}
AI 代码解读

打印结果
9! = 362880
Program ended with exit code: 0
AI 代码解读

目录
打赏
0
0
0
1
49
分享
相关文章
|
1月前
|
【c++】继承(继承的定义格式、赋值兼容转换、多继承、派生类默认成员函数规则、继承与友元、继承与静态成员)
本文深入探讨了C++中的继承机制,作为面向对象编程(OOP)的核心特性之一。继承通过允许派生类扩展基类的属性和方法,极大促进了代码复用,增强了代码的可维护性和可扩展性。文章详细介绍了继承的基本概念、定义格式、继承方式(public、protected、private)、赋值兼容转换、作用域问题、默认成员函数规则、继承与友元、静态成员、多继承及菱形继承问题,并对比了继承与组合的优缺点。最后总结指出,虽然继承提高了代码灵活性和复用率,但也带来了耦合度高的问题,建议在“has-a”和“is-a”关系同时存在时优先使用组合。
118 6
简述 C、C++程序编译的内存分配情况
在C和C++程序编译过程中,内存被划分为几个区域进行分配:代码区存储常量和执行指令;全局/静态变量区存放全局变量及静态变量;栈区管理函数参数、局部变量等;堆区则用于动态分配内存,由程序员控制释放,共同支撑着程序运行时的数据存储与处理需求。
351 22
在 C++中,realloc 函数返回 NULL 时,需要手动释放原来的内存吗?
在 C++ 中,当 realloc 函数返回 NULL 时,表示内存重新分配失败,但原内存块仍然有效,因此需要手动释放原来的内存,以避免内存泄漏。
C++ 多线程之带返回值的线程处理函数
这篇文章介绍了在C++中使用`async`函数、`packaged_task`和`promise`三种方法来创建带返回值的线程处理函数。
221 6
|
6月前
|
C++
C++ 多线程之线程管理函数
这篇文章介绍了C++中多线程编程的几个关键函数,包括获取线程ID的`get_id()`,延时函数`sleep_for()`,线程让步函数`yield()`,以及阻塞线程直到指定时间的`sleep_until()`。
86 0
C++入门3——类与对象2-2(类的6个默认成员函数)
C++入门3——类与对象2-2(类的6个默认成员函数)
65 3
【C++篇】C++类与对象深度解析(三):类的默认成员函数详解
【C++篇】C++类与对象深度解析(三):类的默认成员函数详解
53 3
详解C/C++动态内存函数(malloc、free、calloc、realloc)
详解C/C++动态内存函数(malloc、free、calloc、realloc)
1154 1
C++入门3——类与对象2-1(类的6个默认成员函数)
C++入门3——类与对象2-1(类的6个默认成员函数)
93 1
C++入门6——模板(泛型编程、函数模板、类模板)
C++入门6——模板(泛型编程、函数模板、类模板)
120 0
C++入门6——模板(泛型编程、函数模板、类模板)

热门文章

最新文章

下一篇
oss创建bucket
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等