The best quick start of station c - function

 

1. Custom function

2. Parameters of the function

3. Function call

4. Function recursion

5. Function declaration

A C program, no matter its size, consists of functions and variables.

This section of the article does not talk about library functions

1. Custom function

As the name implies, it is a function defined by itself. How to define it yourself? Let's look directly at the main function and start from the main function to understand it.

int main()
{
    return 0;
}

The basic form of a function definition is:

return value type function name (0 or more parameters)

{

        declaration section;

        statement sequence

}

From here we can see that defining a function requires a function name (main here) , parameters , return value (return 0) and the type that accepts the return value (int). Let me talk about it here: function name, must have, Parameters, return values ​​and types may not be required.

Let's write a function that calculates the addition of two numbers:

#include <stdio.h>
int add(int a, int b)
{
	return a + b;
}
int main()
{
	int a, b,sum;
	scanf("%d%d", &a, &b);
	sum = add(a, b);
	printf("%d\n", sum);
	return 0;
}

The above is the function we define ourselves

2. Parameters of the function

Parameters can be divided into formal parameters and actual parameters

The formal parameter is the added function , and the parameter  in the () after the addition is the parameter

int add(int a, int b)

The actual parameters are the parameters passed in the function call, where a and b are the actual parameters

sum = add(a, b);

A formal parameter is just a temporary copy of an actual parameter

3. Function call (pass by value and pass by address)

Either pass-by-value or call-by -reference is just a temporary copy of the actual parameter , except that the call-by-reference can change the data pointed to by the actual parameter .

If you want to change the actual parameter, pass the address. Call by value if you don't want to change it.

4. Function recursion (that is, calling your own process by yourself)

Here are some points to pay attention to in the recursive process:

1) In the recursive process , the condition of the recursive exit should be continuously approached .

2) Because of the constant use of space in the recursive process, pay attention to stack overflow .

5. Function declaration (tell the compiler the parameters and return types in the function)

Before calling the function, there is a declaration of the function. If it is not declared, it cannot be used. It must be declared first and then defined, but I always put the definition of the function in front of the main function , which avoids this process of declaration.

The following is a group for answering questions. There are learning materials in it. Interested readers can join. Hope it can help you

Guess you like

Origin blog.csdn.net/m0_60598323/article/details/122619062