First, Function:
Function Prototypes:
All functions should have prototypes, especially those that return values that are not integers.
Function prototype of an invisible parameter int * func (void);
Scalar arguments passed to a function ———— value call
Array arguments passed to the function ———— address call
Example: A function that returns the position of an int data in an array:
1#include <stdio.h>2 int* Find_int (intKeyintArray[],intlength)3 {4 intI=0;5 for(i=0; i<length;i++){6 if(Array[i]) = =key)7 return&array[i];//returns a pointer to the location8 }9 return NULL;//do not omit here, more standardizedTen}
The functions and invocations of data exchange in C:
Function:
1 void swap (int *x,int *y)2{3 int temp; 4 temp = *x; 5 *x = *y; 6 *y = temp; 7 }
function call: swap (&a, &b);
ADT (abstract data type) and black box:
C can limit the scope of functions and data definitions by designing and implementing abstract data types
The basic idea of the abstract data type----module with functional description----The task and interface instructions performed by the module----The use of the module
The user of the module does not need to know any details of the module implementation, and the user cannot access the module in any way except for those interfaces that have already been defined.
Basic idea: restricting access to a module is achieved through the use of the static keyword, and he can restrict access to functions and data that are not interfaces.
Recursive functions:
When a function is called, the variable's space is created on the run-time stack. The variables of the previous function remain on the stack and are overwritten.
Example://Receive an integer number, convert to character, and print
1#include <stdio.h>2 voidBinary_to_ascii (unsignedintValue//Suppose the input is 42673 {4Unsignedintquotient;5quotient = value/Ten;6 if(Quotient! =0)7 binary_to_ascii (quotient);8Putchar (value%Ten+'0');9}
Recursion and iteration:
such as Fibonacci functions, recursive calls will design some runtime overhead, the parameters must be pressed on the stack, the local variables allocated memory space, the value of the register must be saved.
N<=1:1
Fibonacci (n) = N=2:1
N>2:fibonacci (n-1) + Fibonacci (n-2)
As with recursive invocation cost: More than one redundant computation, each recursion triggers the other two recursive calls.
Using the iterative method to achieve:
1 LongFibonacci (intN)2 {3 Longresult;4 LongPrevious_result;5 LongNext_older_result;6result = Previous_result =1;7 while(n>2){8n-=1;9Next_older_result =Previous_result;TenPrevious_result =result; Oneresult = previous_result+Next_older_result; A } - returnresult; -}
Variable parameter list
Macro Stdarg definition with Stdarg.h header file (including a type va_list and three macros Va_start va_arg va_end)
Example: (Calculates the average of a specified number of values)
1#include <stdarg.h>2 floatAverageintN_values, ...)//Note the ellipsis of the argument list, with at least one named parameter in the parameter list3 {4Va_list Var_arg;//declaring a variable of type va_list to use with three macros5 intcount;6Flout sum =0;7 8Va_start (var_arg,n_values);//prepare to access variadic Var_arg by Va_start macro9 Ten for(Count =0; Count <n_values; count+=1) One { Asum+= Va_arg (Var_arg,int);//adds a value taken from a mutable argument list. - } - theVa_end (VAR_ARG);//complete processing of mutable parameters - - returnSum/n_values;
Second, array
C Basic Review 4