Function
What is the function of a person who is a beginner of C language? Here I have a popular explanation; a function is a box in which you can encapsulate the function you want to implement, and then call it through the main function.
Why use a function?
For a sequence, modular design is extremely important, and the function embodies a divide-and-conquer idea, the use of functions can avoid code reuse in the program, in addition to the program can play top-down, gradually refine the role, while making the program in testing and optimization easier. For a language, it usually has a well-defined function library, which can be called directly when the developer needs to use it. The same is true of the C language, which also has his own standard library functions. Developers can also use custom functions if their standard library functions are not up to the developer's requirements, and of course, these functions are managed by the main () function for unified invocation.
To give a simple example of a function:
Eg: Write a function that calculates the factorial of an integer n.
Long Fact (int n)//declares a function with a function named Fact, which is the formal parameter in the function name brackets
{
int i;
Long result=1;
for (int i=2;i<=n;i++)
{
Result *=i; The ability to implement factorial
}
return result; Returns the result of a function
}
For a function, you should first understand his function, the function in this example is used to calculate the factorial, the function name of the formal parameter represents a function of the entrance, if not the entrance, the void is used to represent the contents of the parameter, in addition, a function should also have a corresponding return value type, When declaring a function, it is necessary to indicate that if a function does not return a value type, then the function name is declared with void in front of it.
Then merge the above functions into a complete program:
#include <stdio.h>
Long Fact (int n); Function prototypes
int main ()
{
int m;
LONG ret;
printf ("Please enter M:");
scanf ("%d", &m);
Ret=fact (m); Call the function and pass the result of the function to RET
printf ("%d!=%ld\n", M,ret);
return 0;
}
Long Fact (int n)//is equivalent to a sub-function
{
int i;
Long result=1;
for (i=2;i<=n;i++)
{
Result*=i;
}
return result;
}
The primary role of a function prototype is to tell the compiler that the function fact () will receive an int parameter from the calling program, and the compiler checks the type and number of arguments in the function call statement for matching the function prototype.
In the function also pay special attention to the encapsulation of the function, that is, the protection of the box, increase the function of the security judgment, as if the above program can not input negative, which is to enhance the robustness of the program.
Basic principles of function design:
1. The size of the function is small, because such code is easy to maintain, the probability of error is relatively small;
2. Each function should be as simple as possible, and do not design functions that have multiple uses.
3. Each function has only one entry and one exit.
4. In the function interface, clearly define the behavior of the function, including the entry parameters, exit parameters, return type, exception handling, etc., so that the caller is aware of the success of the function can be done, should consider as possible the case of error.
5. At the entrance of the function, check the validity of the parameters.
6. Before performing certain sensitive operations, you should check the legality of the operand and its type to avoid errors caused by data overflow, except 0.
Recursive calls and recursive functions for functions
Let's start with an example: Fibonacci sequence problems.
The recursive method is used to implement:
#include <stdio.h>
Long Fact (int n);
int main ()
{
int n,i,x;
printf ("Input N:");
scanf ("%d", &n);
for (i=1;i<=n;i++)
{
X=fact (i);
printf ("Fact (%d) =%d\n", i,x);
}
return 0;
}
Long Fact (int n)
{
if (n==0)
return 0;
else if (n==1)
return 1;
Else
Return (Fact (n-1) +fact (n-2));
}
The so-called recursion is a function of its own invocation, call itself to implement the function, recursive usage in the memory space is very wasteful, so for a problem may not be the optimal algorithm. To improve the efficiency of program execution, we should try to replace recursive form with iterative form
Functions of C language