Precautions for learning C ++ Functions

Source: Internet
Author: User

The C ++ function in C ++ is A correspondence in mathematics between A non-null number set A and A real number set B. Simply put, A changes with C, then A is the function of C. The corresponding law and the definition domain are two main elements in the function field.

C ++ functions are separated from the main program. These code segments are called and executed when a specific action is required in the program ). For example, a function may take two values and perform complex mathematical operations on them.

 
  1. 1: #include <iostream.h>   
  2.  
  3. 2: #include <conio.h>   
  4.  
  5. 3: #pragma hdrstop   
  6.  
  7. 4:   
  8.  
  9. 5: int multiply(int,int)   
  10.  
  11. 6: void showResult(int);   
  12.  
  13. 7:   
  14.  
  15. 8:int main(int argc,char **argv);   
  16.  
  17. 9:{   
  18.  
  19. 10: int x,y,result;   
  20.  
  21. 11: cout << end1 << "Enter the first value:";   
  22.  
  23. 12: cin >> x;   
  24.  
  25. 13: cout << "Enter the second value: ";   
  26.  
  27. 14: cin >> y;   
  28.  
  29. 15: result=multiply(x,y);   
  30.  
  31. 16: showResult(result);   
  32.  
  33. 17: cout << end1 << end1 << "Press any key to continue...";   
  34.  
  35. 18: getch();   
  36.  
  37. 19: return 0   
  38.  
  39. 20: }   
  40.  
  41. 21:  

Then return the result. The function may take a string for analysis and return part of the analysis string. The new term function) is a pre-defined service that is separated from the main program. Functions are an important part of various programming languages, and C ++ is no exception. The simplest function returns void without parameters.) Other functions may include one or more parameters and may return a value.

The function name rule is the same as the variable name. Figure 1.5 shows the components of a function. The new term parameter is the value passed to the function, used to change the operation or indicate the operation degree. The function declaration or prototype tells the compiler function the number of parameters, the Data Type of each parameter, and the Data Type of the function return value.

Listing 1.4 lists this concept. Prototype is a declaration or definition of the function appearance. Line 11 to 14 of this program uses the standard input stream cin to take two numbers to the user, line 3 calls the multiply () function to multiply the two numbers, and line 3 calls showResult () the function returns the result of multiplication.

Note the prototype declaration of the multiply () and showResult () functions in front of the main program. Only the Data Types of the return type, function name, and function parameters are listed in the prototype. This is the most basic requirement of function declaration. The function prototype can also contain the variable name used to document the function.

The function declaration of the multiply () function can be written as follows: int multiply (int firstNumber, int secondNumber); here the function multiply () has obvious functions, but the code can be both explained and documented by the Code itself. Note that the definition of the function multiply () in listing 1.4 (22 to 25 rows) is out of the main function definition code segment (8 to 20 rows.

The function definition contains the actual C ++ function body. The function body here is the most basic, because the function only multiply two parameters of the function and returns the result. In listing 1.4, the function multiply () can be called in multiple ways, and the results of variables, direct numbers, or other function calls can be passed:

  • Mode description of the VC ++ Development Environment
  • In-depth description of C ++ open source program history
  • How to Learn C ++ applications correctly
  • Detailed description of the C ++ program design structure
  • Introduction to the design principles of C ++

Note that the return value is not used in the last example. In this example, it makes no sense to call the function multiply () without returning the value, but the return value is often ignored in C ++ programming. Many functions return a value after performing a specific action to indicate the function call status. Sometimes the return value is irrelevant to the program and can be ignored.

If the returned value is ignored, this value is abandoned without any harm. For example, the previous sample program ignores the return value of the getch () function and returns the ASCII value of the key ). A function can call other functions or even itself. This call is called recursion ).

This is a complicated issue in C ++ programming. The new term "recursion" is the process in which the function calls itself. The functions described in this section refer to independent functions in C or C ++ programs that are not members of the class ). The independent functions in C ++ can be used in the same way as those in C. However, the C ++ functions are further deepened and will be introduced later in C ++.

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.