[Xiaobai Learning C++ Tutorial] Six, C++ built-in functions and function parameter transfer

@Author: Runsen

Article Directory

A function is a set of statements that perform a task together. Every C++ program has at least one function, the main function main() . All simple programs can define other additional functions.

Built-in function

Before we learn how to create functions, let us first understand some built-in functions...

In C++ are some common library functions sqrt(), abs(), isdigit() and so on.

In order to use library functions, we usually need to include header files that define these library functions.

For example, in order to use sqrt()mathematical functions such as sum abs(), we need to include the header file cmath.

#include <cmath>

Now we have the sqrt()ability to call the square root of any number.

To call a basic function, we only need the function name followed by a pair of parentheses, such as sqrt(9). For example:

#include <iostream>
#include <cmath>

int main(){
    std::cout << sqrt(9) << "\n";
}

Use rand()the modulo operator call to generate random numbers. For example, rand() % 29the output 0to the 28random number between

function

C++ functions are composed of two different parts:

  • Declaration: This includes the name of the function, what the return type is, and any parameters (if the function will accept input values, it is called a parameter).
  • Definition: Also called the function body, it contains instructions for what the function should do.

The general form of function definition in C++ is as follows:

return_type function_name( any, parameters, you, have ) {
 
   // Code block here
 
   return output_if_there_is_any;
 
}

In C++, a function consists of a function header and a function body. All the components of a function are listed below:

Return type: The function can return a value. The return_type is the data type returned by the value function. Some functions perform required operations without returning a value. In this case, return_type is the keyword void.

Function name: This is the actual name of the function. The function name and parameter list together form the function signature.

Parameters: Parameters are like placeholders. When a function is called, you pass a value to the parameter. This value is called the actual parameter or actual parameter. The parameter list refers to the type, order, and number of function parameters. The parameters are optional; that is, a function may not contain any parameters.

Function body: The function body contains a set of statements that define the function of the function.

The following is an example of a function declaration.

void greet() {
    std::cout << "Hello World";
}

In the above program, we declared a greet()function named . To use this function greet(), we need to call it.

Here is how we call the above greet()function.

int main() {
     
    // calling a function   
    greet(); 

}

Pass parameters

In C++, the code add of the function declaration should be before the function call main.

There are three ways to pass function parameters in C++: pass by value, pass by address, and pass by reference.

Here first learn the simplest passing value. In this transfer parameter
, we transfer the parameter value stored or copied to the function formal parameter. Therefore, the original value remains unchanged, but the parameters inside the function have changed.

void calc(int x);

int main()
{
    int x = 10;
    calc(x);
    printf("%d", x);
}

void calc(int x)
{
    x = x + 10 ;
}

In this case, the actual variable x will not change, because we passed the parameter by value, so a copy of x is passed, the copy has been changed, and the copied value is destroyed (out of range) at the end of the function. So the value of the variable x in main() is still 10.

But we can change this program to modify the original X, by making the function return a value when calculating, and store the value of X.

int calc(int x);

int main()
{
    int x = 10;
    x = calc(x);
    printf("%d", x);
}

int calc(int x)
{
    x = x + 10 ;
    return x;
}

20

Formal parameters (pointer transfer)

Pass, formal parameters can be used as references or pointers, in both cases, they will change the value of the original variable.

void calc(int *p);

int main()
{
    int x = 10;
    calc(&x);     // 传递x的地址作为参数
    printf("%d", x);
}

void calc(int *p)
{
    *p = *p + 10;
}

The end result is 20.