C Language Programming Case Tutorial (2nd edition) code note (ii)

Source: Internet
Author: User
Tags function prototype

Scattered knowledge points
    • modularity : The process of decomposing a problem into several sub-problems becomes modular.
    • the advantages of modularity : Not only can a complex problem be decomposed into several relatively simple problems, but also improve the reusability of the program code.
    • function : A function is the basic unit that forms a C program. The function consists of the first part of the function and the two parts of the body, the first part of which contains the function's return type, the function name, and the parameter table declaration, and the function body contains the sequence of statements required to implement a particular function.
    • function Invocation principle : The C language stipulates that all functions must be "first defined and then used". That is, if the called function is defined earlier, it can be called directly; otherwise, the function prototype of the called function needs to be declared before, in order to advertise the C compiler, the complete definition of the function is behind.
    • Common standard function header files
      1. math.h--contains math-related functions
      2. ctype.h--contains functions related to character processing
      3. string.h--contains functions related to string processing
      4. stdio.h--contains functions related to input and output
      5. stdlib.h--contains functions related to dynamically allocating storage space and generating random values
    • Basic format for custom functions

< function return type > < function name > (< parameter table >)

{

< function body >;

}

Description

    1. < function return type > refers to the data type to which the function return value belongs after the function has finished executing;
    2. < The name of the function name > should conform to the rules of user-defined identifiers in C, and it is best to "see the name", preferably with a ;  
    3. < parameter table > indicates the number of arguments that need to be supplied to the function and the data type to which each parameter belongs when calling the function, which is the interface for exchanging information between functions; .
    4. after calling the function, the parameter passes through two basic steps: 1) allocates storage space for each form parameter according to the formal parameter declaration format. 2) then assign the value of the actual parameter to the corresponding formal parameter.
    5. The process of passing an actual parameter to a formal parameter has the following basic characteristics: a) having one-way, the actual parameter can be either a variable or an expression; b) After the value of the actual parameter is assigned to the formal parameter, There is no longer any relationship between the actual parameter and the formal parameter.
    6. < function body > A sequence of statements consisting of statements that need to be executed, is the core part of the function.
    • Call to function

The basic format of the function call statement is:< functions > (< actual parameter table >);

    1. The < function name > is the one that needs to be called;
    2. The parameters in the < actual parameter table > correspond to the formal parameter table of this function;
    3. Example:
1 intdataarray[ -];2 intN;3 DoubleAve;4 5 //Custom Functions6 DoubleAverageintValue[],intnum) {7     intI, sum =0;8      for(i =0; i < num; i++){9Sum + =Value[i];Ten         returnsum*1.0/num; One     } A } -  - //function Call theAve = Average (DataArray, n);
Application examples of custom functions
    • Output multiplication Table
1#include <stdio.h>2 voidDrawLineintNCharch); Must exist3 4 Main ()5 {6     intI, J;7printf"\ 9*9 table\n");8DrawLine $,'=');9printf"\ n 1 2 3 4 5 6 7 8 9");TenDrawLine $,'='); This must be a single quote ' ', or it will have garbled characters One      for(i =1; I <=9; i++){ Aprintf"\n%4d", i); -          for(j =1; J <=9; J + +){ -printf"%4d", i*j); the         } -         if(I <9) -DrawLine $,'_'); -         Else +DrawLine $,'='); -     } + } A  at voidDrawLineintNCharch) { -     inti; -Putchar ('\ n'); -      for(i =1; I <= N; i++){ - Putchar (CH); -     } in}

C Language Programming Case Tutorial (2nd edition) code note (ii)

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.