C + + Basics: C + + judgment

Time:2021-9-7

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 additional functions.

You can divide the code into different functions. It’s up to you to divide the code into different functions, but logically, the division is usually based on each function performing a specific task.

functionstatementTell the compiler the name, return type, and parameters of the function. functiondefinitionProvides the actual body of the function.

The C + + standard library provides a large number of built-in functions that programs can call. For example, functions  strcat()  Used to connect two strings, function  memcpy()  Used to copy memory to another location.

There are many other names for functions, such as methods, subroutines or programs, and so on.

 

Define function

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

return_type function_name( parameter list )
{
   body of the function
}

 

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

  • Return type:A function can return a value.return_type  Is the data type of the value returned by the function. Some functions perform the required operation 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 the parameter list together form the function signature.
  • Parameters:Parameters are like placeholders. When the function is called, you pass a value to the parameter, which is called the actual parameter. The parameter list includes the type, order and quantity of function parameters. Parameters are optional, that is, functions may not contain parameters.
  • Function body:The function body contains a set of statements that define the task that the function performs.

example

Here is  max()  Function source code. This function has two parameters num1 and num2, which will return the larger of the two numbers:

//Function returns the larger of the two numbers
 
int max(int num1, int num2) 
{
   //Local variable declaration
   int result;
 
   if (num1 > num2)
      result = num1;
   else
      result = num2;
 
   return result; 
}

Function declaration

functionstatementIt tells the compiler the name of the function and how to call it. The actual body of a function can be defined separately.

The function declaration includes the following parts:

return_type function_name( parameter list );

For the function max () defined above, the following is the function declaration:

int max(int num1, int num2);

In the function declaration, the name of the parameter is not important. Only the type of the parameter is required. Therefore, the following is also a valid declaration:

int max(int, int);

When you define functions in a source file and call functions in another file, function declarations are required. In this case, you should declare the function at the top of the file that calls the function.

Call function

When you create a C + + function, you define what the function does, and then call the function to complete the defined tasks.

When a program calls a function, program control is transferred to the called function. The called function executes the defined task. When the return statement of the function is executed or the end bracket of the function is reached, the program control will be returned to the main program.

When calling a function, pass the required parameters. If the function returns a value, you can store the return value. For example:

#include 
using namespace std;
 
//Function declaration
int max(int num1, int num2);
 
int main ()
{
   //Local variable declaration
   int a = 100;
   int b = 200;
   int ret;
 
   //Call the function to get the maximum value
   ret = max(a, b);
 
   cout << "Max value is : " << ret << endl;
 
   return 0;
}
 
//Function returns the larger of the two numbers
int max(int num1, int num2) 
{
   //Local variable declaration
   int result;
 
   if (num1 > num2)
      result = num1;
   else
      result = num2;
 
   return result; 
}

Put the max () function and the main () function together and compile the source code. When the last executable is run, the following results are generated:

Max value is : 200

Function parameters

If a function wants to use parameters, it must declare variables that accept parameter values. These variables are called functionsFormal parameters

Formal parameters, like other local variables in a function, are created when entering the function and destroyed when exiting the function.

When calling a function, there are three ways to pass parameters to the function:

By default, C + + usesValue passing callTo pass parameters. In general, this means that the code in the function cannot change the parameters used to call the function. The example mentioned earlier uses the same method when calling the max () function.

Default values for parameters

When you define a function, you can specify a default value for each subsequent parameter in the parameter list. When calling a function, this default value is used if the value of the actual parameter is left blank.

This is done by using assignment operators in the function definition to assign values to parameters. When calling a function, if the value of the parameter is not passed, the default value will be used. If a value is specified, the default value will be ignored and the passed value will be used. See the following example:

#include 
using namespace std;
 
int sum(int a, int b=20)
{
  int result;
 
  result = a + b;
  
  return (result);
}
 
int main ()
{
   //Local variable declaration
   int a = 100;
   int b = 200;
   int result;
 
   //Call a function to add a value
   result = sum(a, b);
   cout << "Total value is :" << result << endl;
 
   //Call the function again
   result = sum(a);
   cout << "Total value is :" << result << endl;
 
   return 0;
}

When the above code is compiled and executed, it will produce the following results:

Total value is :300
Total value is :120

Lambda functions and expressions

C + + 11 provides support for anonymous functions, called lambda functions (also known as lambda expressions).

Lambda expressions treat functions as objects. Lambda expressions can be used like objects, such as assigning them to variables and passing them as parameters, and evaluating them like functions.

Lambda expressions are essentially very similar to function declarations. The specific form of lambda expression is as follows:

[capture](parameters)->return-type{body}

For example:

[](int x, int y){ return x < y ; }

If there is no return value, it can be expressed as:

[capture](parameters){body}

For example:

[]{ ++global_x; }

In a more complex example, the return type can be explicitly specified as follows:

[](int x, int y) -> int { int z = x + y; return z + x; }

In this case, a temporary parameter Z is created to store intermediate results. Like a normal function, the value of Z will not be retained until the next time the unnamed function is called again.

If a lambda function does not return a value (such as void), its return type can be completely ignored.

Variables in the current scope can be accessed within a lambda expression, which is the closure behavior of a lambda expression. Unlike JavaScript closures, C + + variable passing is different from passing values and references. You can specify through the preceding []:

There is another point to note. For the form of [=] or [&], lambda expressions can directly use this pointer. However, for the form of [], if you want to use this pointer, you must explicitly pass in:

[this]() { this->someFunc(); }();

 

If youyesC / C + + interested, want to learn programming, Xiaobian recommends oneC/C++Technical exchange group [Click to enter]!

Involving: introduction to programming, game programming, network programming, windows programming, Linux programming, QT interface development, hackers, etc