c Language Functions

  When writing code to create a larger module, it is usually necessary to break a large module into several small m...

  When writing code to create a larger module, it is usually necessary to break a large module into several small modules to complete together, "function" can be generally understood as "small module". A reasonable framework, using "small module" can help others understand our code.

We have previously learned that a program must have a function (that is, the main main main function), in which standard functions provided by the c system (such as printf,scanf) are also used. The functions in this article are not those system functions, but some functions we have created ourselves.

Let's start with a section called "Code for exchanging values":

//Two Value Exchanges #include<stdio.h> //'#'preprocessing directive void swap(int a,int b) { printf("Before swap a by%d,b by%d\n",a,b); int i;//i as a bridge for data transfer i=a;//The value of a is assigned to i a=b;//The value of b is assigned to a b=i;//The value of i is assigned to b printf("After exchange a by%d,b by%d",a,b); } int main() { int a,b; a=1,b=2; swap(a,b); return 0; }

The results are:

  The following sequence explains how this code works and how it works:

1 principle: code a few lines:

void swap(int a,int b) { printf("Before swap a by%d,b by%d\n",a,b); int i;//i as a bridge for data transfer i=a;//The value of a is assigned to i a=b;//The value of b is assigned to a b=i;//The value of i is assigned to b printf("After exchange a by%d,b by%d",a,b); }

For the function definition section, the general format is as follows:

Data type or void   Function name (parameter 1, parameter 2, ~)

{

      Function Body  

}

First look at void swap(int a,int b):

Detail 1: Data type or void? It depends on whether this "small module" has a return value (retuen), whether it is one of the data types, or whether it is a void.

Detail 2: If you write a data type, it is the data type of the return value, that is, the return value is 1, that is int, 1.0, that is float, that is byte a, that is char.

Detail 3: Name of function (swap means swap in Chinese), which can be written freely (except for the!!!!) and try to write English words.

Detail 4: (parameter 1, parameter 2), a parameter can be missing, but this pair of parentheses must be present. A parameter is to accept the data passed by the argument and then take it into a "small module" to "process", so the parameter must correspond one to one. For example, a in swap(a,b) of the main function part of this code is passed to "int a" in "small module", A and B in swap(a,b) in main function is the argument, Some attention must be paid to the one-to-one correspondence in the order of the parameters involved, and the change of the parameters does not affect the value of the parameters. For example, I added a sentence to the program to print the parameters a,b:

//Two Value Exchanges #include<stdio.h> //'#'preprocessing directive void swap(int a,int b) { printf("Before swap a by%d,b by%d\n",a,b); int i;//i as a bridge for data transfer i=a;//The value of a is assigned to i a=b;//The value of b is assigned to a b=i;//The value of i is assigned to b printf("After exchange a by%d,b by%d\n",a,b); } int main() { int a,b; a=1,b=2; swap(a,b); printf("Arguments a by%d,Arguments b by%d\n",a,b); return 0; }

After running:

  So how do we change the arguments? This is the next section on pointers and functions.

Detail 5: Content of the function body. This is the core of the small module and part of everyone's experience. Let's just look at the return value section first. The return statement will end all functions, including the "small module". At the same time, the return (expression) can return an "expression" to the main function, which is a variable or constant, for example:

#include<stdio.h> float avg(int a,int b,int c) { float avg=(a+b+c)/3; return avg; } int main() { int a,b,c; float average; scanf("%d %d %d",&a,&b,&c); average=avg(a,b,c); printf("Average is%.1f",average); }

The avg of return avg of the "small module" is assigned to the average of the main function after the small module runs, and the result is as follows:

  Detail 6: When main uses its own "small modules", if "small modules" follow the main function, they must be declared before, and that's it:

//Two Value Exchanges #include<stdio.h> //'#'preprocessing directive void swap(int a,int b);//Declare a function, (define plus;) int main() { int a,b; a=1,b=2; swap(a,b); printf("Arguments a by%d,Arguments b by%d\n",a,b); return 0; } void swap(int a,int b) { printf("Before swap a by%d,b by%d\n",a,b); int i;//i as a bridge for data transfer i=a;//The value of a is assigned to i a=b;//The value of b is assigned to a b=i;//The value of i is assigned to b printf("After exchange a by%d,b by%d\n",a,b); }

That is, "small module" must be declared before calling.

/

The above is for freshmen of grade 21 who have some inappropriate understanding of the function. Please give some advice. The code is all c and can run directly.

28 November 2021, 12:46 | Views: 3023

Add new comment

For adding a comment, please log in
or create account

0 comments