C++中的函数

*************

C++

topic:functions

*************

functions are crucial in the programs. Functions are the basic units which realize the function. A basic structure of the code has some modules.

Program starts from main (). mian - function has rights calling other functions. BTW, return 0 can be hidden in the main function. return hides only in the main function; But for a readable code, keep return 0; in the main code. Good habbits improve skills indefinitely.

int main() {
    // 主程序代码写在这里
    return 0; // 成功结束程序
}


diy function:

diy functions are the perfect example of a man's brilliance. A good function seems so sexy in the program.

return sth.
int add(int a, int b)
{
    return a + b;
}
return nothong
void greet() {
    cout << "Hello, World!" << endl;
}


inline function:

The inline functions are used to improve a program performs by expanding the body of the function directly to the call. Iron Man has the ability to summon his armour at any time. Inline functions are comparable to armour.

inline int square(int x) {
    return x * x;
}


overload function:

C++ lets you write the same function name in different ways, depending on the type and number of arguments that are passed in.

void print(int num) {
    std::cout << "Integer: " << num << std::endl;
}

void print(std::string str) {
    std::cout << "String: " << str << std::endl;
}


Write a program to calculate the summation of two integers. 

#include <iostream>

using namespace std;

int main() {
    int num1, num2, sum;

    cout << "Enter the first integer: ";
    cin >> num1;

    cout << "Enter the second integer: ";
    cin >> num2;

    sum = num1 + num2;

    cout << "The sum of " << num1 << " and " << num2 << " is " << sum << endl;

    return 0;
}

remenber the basic function form is as follow.

返回类型 函数名(参数类型 参数名, 参数类型 参数名, ...) 
{
    // 函数体

    return 返回值; // 如果返回类型不是 void,则需要返回一个值
}

Then the summation function is written.

int add(int a, int b)
{
    int sum = a + b;
    return sum;
}

then add mian function and head code.

#include <iostream>
using namespace std;

// 求和函数
int add(int num1, int num2) {
    return num1 + num2;
}

int main() {
    int num1, num2;

    // 提示用户输入
    cout << "Enter first integer: ";
    cin >> num1;
    cout << "Enter second integer: ";
    cin >> num2;

    // 调用求和函数
    int sum = add(num1, num2);

    // 输出结果
    cout << "Sum: " << sum << endl;

    return 0;
}

to improve the performs of the code, add inline function.

#include <iostream>
using namespace std;

// 内联求和函数
inline int add(int num1, int num2) {
    return num1 + num2;
}

int main() {
    int num1, num2;

    cout << "Enter first integer: ";
    cin >> num1;

    cout << "Enter second integer: ";
    cin >> num2;

    int sum = add(num1, num2);

    cout << "Sum: " << sum << endl;

    return 0;
}

put overload function in the program.

#include <iostream>
using namespace std;

// 内联求和函数
inline int add(int a, int b) {
    return a + b;
}

// 重载函数:带字符串描述
void add(int a, int b, const string& description) {
    int sum = a + b;
    cout << "Adding " << a << " and " << b << " (" << description << "): " << sum << endl;
}

int main() {
    int num1, num2;

    cout << "Enter first integer: ";
    cin >> num1;
    cout << "Enter second integer: ";
    cin >> num2;

    // 使用内联求和函数
    int sum = add(num1, num2);
    cout << "Sum: " << sum << endl;

    // 使用重载函数
    add(num1, num2, "Example of overloaded function");

    return 0;
}

here is the return.


Have a good weekend.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值