What is a function?
A function is a piece of code that receives 0 or more parameters, does one thing, and returns 0 or a value.
You can think of a function in math first:
Y=f (x)
For example, a function definition that asks begin to end
void sum (intint end) //Void is the return type (Nothing returned) sum is the function name (int begin, int end) as the parameter table
) { int i; int sum; for (i=0; i<=end;i++) { + =i; } printf ("%d to%d and is%d\n", begin, end, sum);}
Calling functions
Function name (parameter table);
() plays an important role in representing function calls, even if no parameters are ()
The values of the parameters are used sequentially to initialize the parameters in the function.
Return a value from a function (single exit)
int max (intint b) { int ret;
if (a>b) {
Ret=a;
}
else{
Ret=b;
}
return ret;
}
Return stops the execution of the function and sends back a value
return;
return expression ;
You can assign a value to a variable (C=max (a, b);)
Can be passed to the function again (printf ("%d\n", Max (b));)
You can even discard the return value
function with no return value
void function name (parameter table)
You cannot use a return with a value
can not return
cannot return a value assignment when called
Function Succession Relationship
voidSumintBeginintend) { inti; intsum=0; for(i=begin;i<=end;i++{//) write sum () like this because: C's compiler parses your code sum from top to bottom+=i; } printf ("%d to%d and are%d\n", begin,end,sum);} intMain () {sum (1,Ten); SUM ( -, -); SUM ( *, $); return 0;}
voidSumintBeginintend);//The prototype declaration of the function, the prototype can not write the name of the parameter, but generally still writeintMain () {sum (1,Ten); SUM ( -, -); SUM ( *, $); return 0;} voidSumintBeginintend) { inti; intsum=0; for(i=begin;i<=end;i++) {sum+=i; } printf ("%d to%d and are%d\n", begin,end,sum);
}
Calling functions
If the function has arguments, the calling function must be passed to it with the correct number of values
The value that can be passed to a function is the result of an expression, which includes
Literal "C=max (10,12);"
Variable "C=max (A, b);"
The return value of the function "C=max (max (23,45), a);"
The result of the calculation "C=max (23+45,b);"
Type mismatch?
The traditional biggest vulnerability of C language when the value given when calling a function does not match the parameter
The compiler always quietly transforms the type for you, but it's probably not what you expect.
Follow the language, C++/java is very strict in this respect
What is it that passes through?
voidSwapintAintb);intMain () {intA=5; intb=6; Swap (A, b); printf ("a=%d b=%d\n", A, b); Can such code exchange values for a and B? return 0;}voidSwapintAintb) { inttemp =A; A=b; b=temp;}
C language can always pass a value to a function when calling a function
Each function has its own variable space, and the argument is in this separate space, which is not related to the other functions.
Local variables
Each run of the function produces a separate variable space in which the variable is unique to the function, called the local variable
Local variables defined inside the function
Parameter is also a local variable
For local variables, lifetime and scope braces ——— blocks (within blocks of a function's intra-block statement)
Before the program runs into this block, the variable does not exist, leaving the block, where the variable disappears.
The variables defined outside the block are still valid inside.
The variables inside the block that define the same name as the outside cover the outside
You cannot define a variable with the same name within a block
Local variables are not initialized by default
When there are no parameters
(void)
Comma operator
Punctuation in parentheses when calling a function, not an operator
F (A, B)
F ((b))
C language does not allow nested definitions of functions
Primary knowledge of functions in C language (male god Onke teacher Mooc)