The basic use _c language for functions in C + + programming

Source: Internet
Author: User
Tags function prototype integer numbers numeric

Formal parameters and actual parameters

In most cases, when calling a function, the function is parameterized. There is a data transfer relationship between the calling function and the called function. As mentioned earlier: the variable name in parentheses after the function name is defined as the formal parameter (formal parameter, abbreviation parameter), and when a function is called in the calling function, the argument in parentheses after the function name (which can be an expression) is called the actual argument (actual parameter , referred to as the argument).

The data delivery when the function is invoked by the "example".

#include <iostream>
using namespace std;
int max (int x,int y)//define the parameter function max
{
  int z;
  z=x>y?x:y;
  return (z);
}
int main ()
{
  int a,b,c;
  cout<< "Please enter two integer numbers:";
  cin>>a>>b;
  C=max (a,b);//Call Max function, given argument is a,b. function value assigned to C
  cout<< "max=" <<c<<endl;
  return 0;
}

The operating conditions are as follows:

Please enter two integer numbers:2 3↙
max=3

For a description of the formal participation argument:
1 The formal parameter specified when the function is defined, when the function call is not present, they do not account for memory cells in storage, so they are called formal or virtual parameters, indicating that they are not actual data, and that only when a function call is in place can the formal parameters in function max be allocated to the memory unit to receive data from the argument. At the end of the call, the memory unit that the formal parameter occupies is also freed.

2 arguments can be constants, variables, or expressions, such as Max (3, a+b), but require a and b to have a certain value. To assign the value of the argument to the formal parameter when the function is called.

3 when defining a function, you must specify the type of the formal parameter at the first part of the letter (see the example Program line 3rd).

4 The type of the actual participating formal parameter should be the same or assignment compatible. In Example 4.2, the arguments and the formal parameters are integral types, which is legal and correct. If the argument is an integral type and the formal parameter is solid, or vice versa, the assignment rules for different types of numeric values are converted. For example, the value of argument A is 3.5, and the formal parameter x is an integer, the 3.5 is converted to an integer 3, and then the form parameter B is sent. Character and integral types can be used in general.

5 The real parametric of the parameter variables is "value transfer", that is, one-way transmission, only from the actual parameters to the formal parameters, and not from the formal parameters to return to the argument. When a function is invoked, the compilation system temporarily assigns a storage cell to the parameter.

Note that the actual parameter units and the formal parameter units are different units. The following figure indicates that the values 2 and 3 of arguments A and B are passed to the corresponding formal parameters x and Y.


When the call is finished, the formal parameter unit is released, and the argument unit retains and maintains the original value. Therefore, when a called function is executed, the value of the formal parameter, if changed, does not change the value of the argument in the calling function. For example, if the values of parameter x and Y are changed to 10 and 15 during the execution of the Max function, arguments A and B are still 2 and 3 after the call ends, as shown in the previous figure.
The return value of the function

1 The return value of the function is obtained by returning statements in the function. The return statement is taken back to the calling function with a certain value in the called function.

The parentheses behind the return statement can or should not be. The value after return can be an expression.

2) The type of the function value. Since the function has a return value, this value should of course belong to a certain type, and the type of function value should be specified when the function is defined.

3 If the type of the function value is inconsistent with the value of the expression in the return statement, the function type determines the type of the returned value. For numeric data, type conversions can be done automatically.

General form of function call

The general form of a function call is:

  function name ([argument list column]);


If you are calling a parameterless function, then the argument table column may not be available, but the parentheses cannot be omitted. If the Argument table column contains more than one argument, the arguments are separated by commas. The number of actual participating formal parameters should be equal, and the type should match (same or assignment compatible). The actual participation parameters correspond sequentially, passing the data on a one-to-one basis. It should be stated, however, that if the argument table column contains more than one argument, the order of the values for the arguments is not determined.
How the function is called

By the function in the statement, you can have the following 3 kinds of function call methods:
(1) Function statement
Calling a function as a single statement does not require a function to bring back a value, but simply requires the function to complete a certain operation. As in Example 4.1, Printstar ();
(2) Expression of function
function appears in an expression that requires the function to bring back a certain value to participate in the expression's operation. such as C=2*max (A, b);
(3) Function parameters
Function call as an argument to a function. Such as:

M=max (A, max (b, c)); Max (b, c) is a function call whose value is an argument to the outer max function call

Declaration and function prototypes for called functions

Calling another function in one function (that is, a called function) requires the following conditions:
The function that is called first must be a function that already exists.
If you use a library function, you should generally use the #include command to "include" the header file in this file at the beginning of this file.
If you use a function defined by a user that is in the same program unit as the function that called it (the calling function), and the position is after the calling function, you must declare the called function before calling this function.

The so-called function declaration (declare), is in the function is not defined, in advance of the function of the relevant information to the compiler system, so that the compilation can be normal.

"Example" declares a function that is called.

#include <iostream>
using namespace std;
int main ()
{
  float add (float x,float y);//Declaration of Add function
  float a,b,c;
  cout<< "Please enter a,b:";
  cin>>a>>b;
  C=add (a,b);
  cout<< "sum=" <<c<<endl;
  return 0;
}
Float Add (float x,float y)//define Add function
{
  float z;
  Z=x+y;
  return (z);
}

The operating conditions are as follows:

Please enter a, b:123.68 456.45↙
sum=580.13

Note: the definition and declaration of a function are not the same thing. Definition refers to the establishment of function function, including specifying function name, function type, formal parameter and its type, function body, etc., which is a complete and independent function unit. And the function of the declaration is to put the name of the function, the type of function and the number of parameters, the type and order (note, excluding the function body) notifies the compiled system to check against the statement that contains the function call (for example, if the function name is correct, and the type and number of the actual participating parameters are the same).

In fact, in a function declaration, you can also write formal parameter names without writing them, such as

  Float Add (float, float);

This function declaration is called a functional prototype (function prototype). Using functional prototypes is an important feature of C and C + +. Its main function is: According to the function prototype in the program compile phase of the call function of the legality of a comprehensive check. If you find a function call that does not match the function prototype, you report a compilation error. It belongs to a grammatical error. Users can easily detect and correct errors based on the error message displayed on the screen.

The general form of a function prototype is:

  function type function name (parameter type 1, parameter type 2 ...);


Or

  function type function name (parameter type 1 parameter name 1, parameter type 2 parameter name 2 ...);

The first form (1) is the basic form. In order to facilitate the reading of the program, also allow the function prototype to add parameter name, it becomes the first (2) Form. However, the compilation system does not check the parameter names. So it doesn't matter what the name of the parameter is. The declaration in the above program can also be written as

  Float Add (float A, float b); Parameter names do not use X, Y, and A, b


The effect is exactly the same.

It should be ensured that the function prototype is consistent with the first letter, that is, function type, function name, number of parameters, parameter type and parameter order must be the same. The function name, argument type, and number of arguments should be consistent with the function prototype when the function is called.

Two point description:
1 as explained earlier, you may not need to declare a called function before it appears in the calling function. Because the compiler already knows the defined function type in advance, the function is checked for correctness according to the information provided in the first part of the letter.

Experienced programming staff generally put the main function in the front, so that the entire structure and role of the program at a glance, overview of the overall situation, and then specifically understand the details of each function. In addition, using a function prototype to declare a function can also reduce errors that may occur when you write a program. Because the position of the function declaration is close to the position of the function call statement, it is convenient to write the function call in the near reference function prototype when writing the program, so it is not easy to make mistakes. So you should develop the habit of declaring all the functions you use. This is an important link to ensure the correctness and readability of the program.

2 The position of the function declaration can be in the function where the function is called, or outside the function. If a function declaration is placed outside of a function, before all function definitions, there is no need to declare the called function in each of the calling functions. For example:

Char Letter (char, char); The bank and the following two lines of functions are declared before all functions and
float f (float, float) outside the function;//Thus the scope is the entire file
int i (float, float);
int main ()
{...} In the main function, you do not have to declare the function that it calls
Char letter (Char C1, char C2)//define the letter function
{...}
float f (float x, float y)//define F function
{...}
int I (float j, float K)//define I function
{...}

If a function is called by more than one function, it is better to use this method than to repeat the declaration in each calling function.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.