Chapter 3: Functions in C++

Chapter 3: Functions in C++

functions in C++

In C++, functions are an important tool for organizing and reusing code. A function can encapsulate a piece of code with a specific function and interact with other code by providing input parameters and return values. This article will discuss functions in C++ in depth, including function definitions, function calls, function parameters and return values, etc., and help you use functions better through rich code examples and practical cases.

Function definition and call

A function definition includes a function header and a function body. The function header specifies the function's name, parameter list, and return type. The function body is the actual execution code of the function.

Here is an example of a simple function definition and call:

// 函数定义
int add(int a, int b) {
    
    
    return a + b;
}

// 函数调用
int result = add(3, 4);
cout << "结果是:" << result << endl;

In the above code, we defined a addfunction called that takes two integers as arguments and returns their sum. Then, call the function in the main program addand assign the result to a variable result. Finally, the output is "The result was: 7".

function parameters

Functions can accept zero or more arguments. Parameters are values ​​or variables passed to a function that a function can use to perform a specific action. In a function definition, parameters are represented by parameter lists, and each parameter has a type and a name.

Here's an example that demonstrates function definition and invocation with multiple parameters:

int multiply(int x, int y) {
    
    
    return x * y;
}

int result = multiply(5, 3);
cout << "结果是:" << result << endl;

In the above code, we defined a multiplyfunction called that takes two integers as arguments and returns their product. Then, call the function in the main program multiplyand assign the result to a variable result. Finally, the output is "The result was: 15".

function return value

Functions can return a value to provide the result of a computation or execution status. In a function definition, indicate the return value type of the function by specifying the return type. If the function does not return a value, it can be used voidas the return type.

Here is an example showing a function definition and call with a return value:

// 返回两个整数中的较大值
int findMax(int x, int y) {
    
    
    if (x > y) {
    
    
        return x;
    } else {
    
    
        return y;
    }
}

int max = findMax(10, 7);
cout << "最大值是:" << max << endl;

In the above code, we defined a findMaxfunction named , which takes two integers as parameters and returns the larger value. Then, call the function in the main program findMaxand assign the result to a variable max. Finally, the output is "The maximum value is: 10".

actual case

Let's look at a more complex example, combining multiple functions and control flow:

// 判断一个数是否为素数
bool isPrime(int num) {
    
    
    if (num <= 1) {
    
    
        return false;
    }

    for (int i = 2; i < num; i++) {
    
    
        if (num % i == 0) {
    
    
            return false;
        }
    }

    return true;
}

// 打印小于给定数值的所有素数
void printPrimes(int limit) {
    
    
    cout << "小于" << limit << "的素数是:";

    for (int i = 2; i < limit; i++) {
    
    
        if (isPrime(i)) {
    
    
            cout << i << " ";
        }
    }

    cout << endl;
}

// 调用函数打印小于10的所有素数
printPrimes(10);

In the above code, we defined two functions: isPrimeand printPrimes. The function isPrimechecks whether a number is prime and returns a Boolean value. The function printPrimesprints all prime numbers less than this value according to the given constraints. Finally, call it in the main program printPrimes(10)to print all prime numbers less than 10.

The output is "prime numbers less than 10 are: 2 3 5 7".

Guess you like

Origin blog.csdn.net/qq_51447496/article/details/132241128