Latest web development tutorials

C ++ function

Is a set of functions to perform a mission statement together. Every C ++ program has at least one function, that is the main functionmain (), all simple program can define other additional functions.

You can put the code into different functions. How to divide the code into separate functions is up to you to decide, but logically divided usually perform each function based on a specific task to carry out.

Functiondeclaration tells the compiler function name, return type and parameter.Functiondefinition provides the actual body of the function.

C ++ standard library provides a lot of built-in functions of the program can call. For example, the functionstrcat () is used to connect two strings, the function memcpy ()to copy memory to another location.

There are many function is called, such as methods, procedures or subroutines, and so on.

Defined Functions

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

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

In C ++, function consists of a function header and a function body components. Listed below are all part of a function:

  • 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 desired operation without the return value, in this case, return_type keywordvoid.
  • Function name: This is the actual name of the function.Function name and parameter list together constitute the function signature.
  • Parameters: Parameter is like a placeholder.When the function is called, you pass a value to the parameter, this value is called the actual parameters. Parameter list include the type of function parameters, the order quantity. Parameter is optional, that is to say, the function may not contain parameters.
  • Main function: function body contains a set of functions defined mission statement.

Examples

The following are themax () function in the source code.This function takes two parameters num1 and num2, which will return the larger of the two numbers count:

// 函数返回两个数中较大的那个数
 
int max(int num1, int num2) 
{
   // 局部变量声明
   int result;
 
   if (num1 > num2)
      result = num1;
   else
      result = num2;
 
   return result; 
}

Function declaration

Functiondeclaration tells the compiler function name, and how to call the function.The actual body of the function can be defined individually.

Function declaration includes the following sections:

return_type function_name( parameter list );

Defined above for the function max (), the following is a function declaration:

int max(int num1, int num2);

In the function declaration, the name of the parameter is not important, only the type parameter is required, so the following is a valid statement:

int max(int, int);

When you define a function in a source file and call a function in another file, the function declaration is required. In this case, you should call the top of the file function declaration function.

Call functions

When you create a C ++ function that defines what functions do, and then to complete the task by calling the function it has been defined.

When a program calls a function, program control will be transferred to the called function. When the function executes the defined tasks that are invoked when the function return statement is executed, or reach the end bracket function, the program will return control to the main program.

When you call the function, passing the required parameters, if the function returns a value, you can store the return value. E.g:

#include <iostream>
using namespace std;
 
// 函数声明
int max(int num1, int num2);
 
int main ()
{
   // 局部变量声明
   int a = 100;
   int b = 200;
   int ret;
 
   // 调用函数来获取最大值
   ret = max(a, b);
 
   cout << "Max value is : " << ret << endl;
 
   return 0;
}
 
// 函数返回两个数中较大的那个数
int max(int num1, int num2) 
{
   // 局部变量声明
   int result;
 
   if (num1 > num2)
      result = num1;
   else
      result = num2;
 
   return result; 
}

The max () function and the main () function is put a piece, compile the source code. When you run the final executable file will produce the following results:

Max value is : 200

Function Arguments

If you want to use the function parameter, you must declare a variable to accept the parameter values. These variables are known asformal parameters of the function.

Like other forms of parameters local variables within the function is created when entering the function, exit the function is destroyed.

When the function is called, there are two ways to pass parameters to a function:

调用类型描述
传值调用 该方法把参数的实际值复制给函数的形式参数。在这种情况下,修改函数内的形式参数对实际参数没有影响。
指针调用 该方法把参数的地址复制给形式参数。在函数内,该地址用于访问调用中要用到的实际参数。这意味着,修改形式参数会影响实际参数。
引用调用 该方法把参数的引用复制给形式参数。在函数内,该引用用于访问调用中要用到的实际参数。这意味着,修改形式参数会影响实际参数。

By default, C ++ using thecall-by to pass parameters.Generally, this means that the code within the function can not change the parameters used to call the function. Examples mentioned before, call max () function when using the same method.

The default value of the parameter

When you define a function, you can specify a default value for each parameter in the parameter list behind. When the function is called, if the value of the actual parameter is left blank, then use the default values.

This is done using the assignment operator to assign the function definition as a parameter. When the function is called, if the value of the parameter is not passed, it will use the default value if a value is specified, the default value is ignored, using the passed value. Consider the following examples:

#include <iostream>
using namespace std;
 
int sum(int a, int b=20)
{
  int result;

  result = a + b;
  
  return (result);
}

int main ()
{
   // 局部变量声明
   int a = 100;
   int b = 200;
   int result;
 
   // 调用函数来添加值
   result = sum(a, b);
   cout << "Total value is :" << result << endl;

   // 再次调用函数
   result = sum(a);
   cout << "Total value is :" << result << endl;
 
   return 0;
}

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

Total value is :300
Total value is :120