The importance of functions in programming is self-evident, so this article will briefly describe some contents related to functions.
What is a function
Function, translated as a method or function. The main function achieves a certain purpose by calling a user-defined function or a function in the function library, or obtains the desired data.
Main function
The main function (main function) is the entry function of C program. The execution of the program starts from the main function, and the transfer of other functions is also carried out directly or indirectly in the main function.
main function writing
1. No parameter, no return value
In the C89 standard, this writing method is acceptable (some compilers will have warnings), and its return value will default to int. In fact, if the function does not explicitly declare the return type, the compiler defaults the return value to int.
Writing method:
#include <stdio.h> main(){ }
Example:
#include <stdio.h> main(){ printf("Hello world!"); }
compile:
function:
2. Return void without parameters (don't use it if you can't use it!)
This form is often seen in the question area. Normally, void main() is not allowed in the standard syntax of C language, and I don't know where this writing method came from. I haven't seen this usage since I started learning.
Writing method:
#include <stdio.h> void main(){ }
Example:
#include <stdio.h> void main(){ printf("Hello world!"); }
compile:
You can see the error message. The main function must return int. The return value of the main function is used to describe the exit state of the program. If 0 is returned, the program exits normally; The meaning of returning other numbers is determined by the system. A non-zero return indicates that the program exits abnormally. If you use void, you can't query the end state of the program.
3. Return int without parameter
This is a common way of writing (personal use is also more, especially when testing some small function algorithms)
Writing method:
#include <stdio.h> int main(){ }
Example:
#include <stdio.h> int main(){ printf("Hello world!"); }
compile:
function:
You can see the final prompt with return value 0, which means that the program ends normally.
4. The parameter is a command line parameter and returns int
This writing requires command line arguments.
(command line parameters:
Argc: represents the number of command line parameters when starting the program. C and C + + languages specify that the file name of the executable program itself is also a command line parameter. Therefore, the value of argc is at least 1.0
argv []: pointer array, in which each element is a pointer of char * type, which points to a string in which the command line parameters are stored.)
Writing method:
#include <stdio.h> int main(int argc,char *argv[]){ }
Example:
#include <stdio.h> int main(int argc,char *argv[]){ printf("%d %s %s",argc,argv[0],argv[1]); }
compile:
Run through dev:
You can see that the value of argc is 1. Because no second argv is passed in, the value of argv[1] is null
Run through the console (cmd or virtual machine terminal):
When helloworld is not passed in, the output is consistent with the result of running in dev. after the second parameter helloworld is passed in, argc becomes 2 because two parameters are passed in. argv[0] is the relative path of the calling program, and argv[1] is helloworld.
Functions in function library
C language provides us with hundreds of callable library functions, such as strlen, strcat and strlwr related to strings Or printf, scanf, which are provided for us by C language. When we use a library function, we need to embed (#include < >) the header file required by the function in the program.
Example: connect two strings into one
Do not use function libraries
#include<stdio.h> #define MAX 1024 int main(){ char s1[MAX] = "hello"; char s2[MAX] = "world"; char s3[MAX] ; int i,j; printf("Before connection:\n%s\n%s\n",s1,s2); for(i=0;s1[i]!='\0';i++){ s3[i]= s1[i]; } for(j =0;s2[j]!='\0';j++){ s3[i+j] =s2[j]; } s3[i+j]='\0'; printf("After connection:%s\n",s3); }
Operation results:
Use function library
#include<stdio.h> #include<string.h> #define MAX 1024 int main(){ char s1[MAX] = "hello"; char s2[MAX] = "world"; char *str1,*str2; str1=s1; str2=s2; strcat(s1,s2); printf("%s",s1); }
Operation results:
You can see that the functions that are not applicable to the function library need to traverse twice to complete the connection function, and you need to add the terminator '\ 0' at the end. Using strcat in the library can quickly complete the connection of strings without other operations, reducing the amount of code and running time.
Custom function
To customize a function, you first need to determine the return value, function name, and parameters of the function.
Define format:
Method I <return type> <Function name>(Parameters);//Function declaration int main(){} <return type> <Function name>(Parameters){//Function realization Function realization } Method 2 <return type> <Function name>(Parameters){ Function realization } int main(){}
There is no big difference between method 1 and method 2. Both can be used. The only thing to note is that if you define a function with method 2, you should note that the user-defined function must be implemented before it can be called! Next, it will be explained by several examples
Example 1: Declaration and Implementation
#include<stdio.h> void fun(); //If the declaration is removed, the compiler will report an error, indicating that fun() is not defined void fun1(){ printf("this is fun1"); } int main(){ fun(); } void fun(){ printf("this is fun"); }
You can see that in the above code, void is the return type of the function, fun is the name of the function (note that the name of the function should also comply with the naming specification of C language), and the function of the function is to output this is fun
Operation results:
You can see that the prompt fun is not declared after compilation, so don't forget to declare when defining a function with method 1.
Example 2: nested calls to functions
#include<stdio.h> void fun1(); void fun2(); void fun3(){ printf("this is fun3\n"); fun1(); } int fun(); int main(){ fun1(); fun3(); if(fun()==1) printf("this is main\n"); } void fun1(){ printf("this is fun1\n"); fun2(); } void fun2(){ printf("this is fun2\n"); } int fun(){ return 1; }
Operation results:
You can see the execution result in the figure above. The main function calls fun1. After fun1 outputs the content, fun2 is called internally; After output, fun2 returns, and then fun1 returns to the main function; The main function calls fun3 to output and then returns. The main function calls fun3 to get the return value of 1 and then outputs. However, it should be noted that functions can be called nested but not defined nested!
Example 3: sum a and b (return value and parameters of function)
#include<stdio.h> int sum_return(int a,int b){ return a+b; } void sum_primer(int a,int b,int *c){ *c=a+b; } void sum(int a,int b,int c){ c=a+b; } int main(){ int a = 2,b = 3,c = 0; sum(a,b,c); printf("sum c=%d\n",c); sum_primer(a,b,&c); printf("sum_primer c=%d\n",c); printf("sum_return d=%d",sum_return(a,b)); }
Operation results:
You can see that when using the sum() function, c cannot return the sum of a and b, and sum_ In primer (), c can sum a and b. here, it should be noted that the value transfer will not affect the original value. After the address transfer through the pointer, the value in the address where c is located can be changed, so that the sum value return can be realized (normally, most functions seeking multiple values will save the value through address transfer). And sum_return is the normal return value.
Example 4: find n! (recursive function)
#include<stdio.h> int fun(int n) { if(n==1||n==0) return 1;//Returns 1 if the parameter is 0 or 1 return n*fun(n-1);//Otherwise, it returns the product of n and the next recursion } int main() { int n; printf("Please enter n:") ; scanf("%d",&n); printf("%d\n",fun(n)); return 0; }
Operation results:
When using recursive functions, we must pay attention to the conditions for the end of recursion. If there is no restriction, it is equivalent to an infinitely nested function
The above is a brief description of the content of the function. I hope it will be of little help to you