Function (detailed)

1. Function declaration, implementation and call.


A function is a code module that completes a specific function. Its program code is independent and usually requires a return value, which can also be

null value.

The general form is as follows:

<Data Type> <Function Name>( <Formal Parameter Description>){

statement sequence;

return[(<expression>)];

}


<data type> is the return value type of the entire function.

The value of the expression in the return[( <expression> )] statement must be consistent with the <data type> of the function. If no return value should be

It should be written as void type.

<form parameter description> is the description form of multiple variables separated by commas ","

The pair of curly braces {<statement sequence>} is called the function body; <statement sequence> is composed of more than or equal to zero statements.


The description of the function refers to the prototype of the function. Among them, <formal parameter description> can default the name of the variable description, but the type cannot be defaulted.

For example:

double Power(doublex, int n),

double Power(double, int);

#include <stdio. h> 
void show()
{
printf("Hello everyone!\n" );
}
int main()
{
printf( start----\n );
show();
printf(”end----\n" );
return 0;
}

Define a function to calculate the value of x (x is a real number, n is a positive integer)

#include <stdio. h> 
double power(double x, int n)
{
double r = 1;
int, 1;
for(i=1;i<=n;i++)
r*= X;
return r;
}
int main()
{
double X,ret;
int n;
printf( "input:" ") ;
scanf("%lf%d", &x,&n);
ret = power(x, n);
printf("%lf %d = %lf\n", X,n, ret);
return 0;
}
#include <stdio. h> 
double power(double x, int n);
int main()
{
double X,ret;
int n;
printf( "input:" ") ;
scanf("%lf%d", &x,&n);
ret = power(x, n);
printf("%lf %d = %lf\n", X,n, ret);
return 0;
}
double power(double x, int n)
{
double r = 1;
int, 1;
for(i=1;i<=n;i++)
r*= X;
return r;
}

The use of a function is also called a function call, and the form is as follows:

function name (<actual argument>)

Actual parameters are the data passed from the calling function to the called function when the function is used. Exact data is required.

A function call can appear in an expression as an operand or it can form a statement by itself. For functions that return no value

In other words, only one function call statement can be formed.

A function can first declare its implementation and then call it, or it can declare it first and then call it and then implement it.

There are function declarations in the header file. If you don’t want to use the header file to delete, you only need to add the required header file declaration.


Second, the parameter passing of the function.

How to pass parameters between functions

①Global variables

Global variables are variables declared outside the function body, and they are visible in every function in the program. Once defined, global variables are visible anywhere in the program. Depending on where the function is called, the execution result of the program may be affected. Not recommended .

#include <stdio. h> 
double x=2;
int n=3;
double power();
int main( )
{
//double X;
double ret;
//int n;
printf( "input:" ") ;
scanf("%lf%d", &x,&n);
ret = power(x, n);
printf("%lf %d = %lf\n", X,n, ret);
return 0;
}
double power( )
{
double r = 1;
int, 1;
for(i=1;i<=n;i++)
r*= X;
return r;
}

②Copy delivery method

The calling function passes the actual parameter to the called function, and the called function will create a formal parameter of the same type and initialize it with the actual parameter. The formal parameter is a newly opened storage space, so changing the value of the formal parameter in the function will not affect the actual parameter.

#include <studio.h>
int main()
{
double x=2;
int n=3;
double ret;
printf("&x=%p &n=%pn", &x, &n);
ret = power(x, n);
printf("%lf %d = %lf\n", x, n, ret)
return 0;
}
double power(double a, int b) //double a =x ,double b=n .
{
double r = 1;
int i;
printf("&a=%p &b=%p\n", &a, &b);
for(i=1;i<=b;i++)
r*=a;
return  r;
}

③Address delivery method

Transfer by address, the actual parameter is the address of the variable, and the formal parameter is the same type of pointer own operation).

void swap(int * X,int * y);
int main( )
{
int a=10;
int b=20;
printf( "before:%d %d\n", a, b);
swap(&a, &b);
printf("after:%d %d\n", a, b);
return 0 ;
}
//int *x=&a;int*y=&b;
void swap(int *x,int*y)
{
int t;
t=*x;//a
*x = *y;
*y = t;
}

Exercise: Write a function that counts the number of lowercase letters in a string and converts the lowercase letters in the string to uppercase.

#include <stdio.h>
int str_fun(char* p);
int main(int argc,char *argv[])
{
char s[] = "welcome2017Beijing";
int n;
n = str_fun(s);
printf("n=%d  %s\n",n,s);
return 0;
}
int str_fun(char * p)//char * p= S;
{
int num = 0;
while(*p !='0'){  //while (*p)
if(*p<='z'&&*p >= 'a'){
num++;
*p-='  ';
}
p++;
}
return num;
}

3. Pass array parameters in the function.

①Global array transfer method

②Copy delivery method

The actual parameter is a pointer to the array, and the formal parameter is the name of the array (essentially a pointer variable)

③Address delivery method

The actual parameter is a pointer to the array, and the formal parameter is a pointer variable to the type

 #include <stdio.h>
int array_ sum(int data[], int n);//int array_ sum(int *data, int n);
int main(int argc, const char *argv[ ] )
{
int a[ ]={1,2,3,4,5,6,7,8};
int sum=0;
sum=array_ sum(a, sizeof(a)/sizeof(int));
printf( "sum=%d\n",sum) ;
return 0;
}
int array_ sum(int data[], int n){//int array_ sum(int *data, int n){
int ret=0;
int i;
for(i=0;i<n;i++){
ret+=data[i];
}
return ret;
}

Exercise 2: Write a function to remove spaces in strings

#include <stdio.h>
#include <string.h>
void del_ space(char *s1);
int main(int argc, const char *argv[ ] )
{
char s[]="a d sd" ;
puts(s);
del_ space(s);
puts(s);
return 0;
void del_ space(char *s1)
{
char *s2;
s2=s1:
while(*s1){
if(*s1==' ')
{
s1++ ;
}else{
*s2=*s1;
s1++ ;
s2++ ;
}
}
*s2='\0';
}

Fourth, the pointer function.

A pointer function is a function whose return value is an address.

The general form of a pointer function definition is as follows:

<data type> *<function name>(<parameter description>){

statement sequence;

}

The return value can return: address of global variable/address of static variable/address of string constant/address of heap

Exercise 1: Write a pointer function that removes spaces from a string.

#include <stdio.h>
char *del_space(char *s);
int main( int argc, const char *argv[]){
char *r;
char str[]="la la l a";r=del_space(str);
printf(" %s\n" ,r);
puts(str);
return 0;
}
char *del_space(char *s){
char *p=s;
char *r=s;while(*s){
if(*s==' ')
S++;
else{
*p=*s;p++;s++;
}
}
*p='\0';
return r;
}

Exercise 2: Write a pointer function to implement concatenation in strings.

#include <stdio. h>
char *strcat(char *a,char *b);
int main(int argc,
const char *argv[])
{
char a[50]="hello";
char b[ ]="word";
puts(strcat(a,b));
return 0;
}
char *strcat(char *a,char *b){
char *c=a;
while(*a){
a++ ;
}
while(*b){
*a=*b;
a++ ;
b++ ;
*a='\0' ;
return C;
}

Exercise 2: Write a pointer function to convert an integer into a string.

Method 1. Create a new space p

#include <stdio. h>
char * zhuan(int n);
int main(int argc, const char *argv[ ] )
{
char *S;
int n;
printf("请输入要转换为字符串的整数\n" );
scanf( " %d" ,&n); 
S= zhuan(n);
puts(s);
return 0,
char * zhuan(int n){
int r,i=0,j;
static char p[50];
while(n){
r=n%10 ;
n=n/10;
p[i]=r+'0' ;
i++ ;
}
p[i]='\0';
j=i-1;
i=0;
while(i<j){
r=p[i];
p[i]=p[j];
p[j]=r:
i++;
j--;
}
return p;
}

Method 2. Save in the original space s[ ]

#include <stdio.h>
char * zhuan(char *p,int n);
int main(int argc, const char *argv[])
{
char s[50].*r;
int n;
printf("请输入要转换为字符串的整数\n" );
scanf( " %d" ,&n); 
r=zhuan(s,n);
puts(r );
puts(s);
return 0;
}
char * zhuan(char *p,int n){
int r ,i=0,j;
while(n){
r=n%10;
n=n/10;
p[i]=r+'0';
i++;
p[i]='\0';
j=i-1;
i=0;
while(i<j){
r=p[i];
p[i]=p[j];
p[j]=r;
i++;
j--;
}
return p;
}

5. Recursive functions and function pointers.

1. Recursive function

A recursive function refers to a function that directly or indirectly calls the function itself in its function body.

The execution process of a recursive function call is divided into two phases:

①Recursive stage: starting from the original problem (according to the recursive formula, recursively from unknown to known, and finally reach the recursive termination condition.

②Regression stage: Find the result according to the recursive termination condition, reverse and gradually substitute into the recursive formula, and return to the original problem to solve.

Exercise 1: Find n!

#include <stdio. h>
int fac(int n);
int main(int argc, char *argv[])
int n;
printf(" input:" );
scanf("%d",&n);
printf("%d\n", fac(n));
return 0;
}
int fac(int n)
{
if(n==0||n==1)
return 1;
return n * fac(n-1);
}

Exercise 2: Fibonacci Sequence

#include <stdio. h>
int main(int argc, char *argv[])
{
int n=1;
while (n<=10) {
printf("%d",fib(n));
n++:
}
printf("\n" );
return 0
}

int fib(int n)
{
if(n==1||n==2)
return 1 ;
return fib(n-1)+fib(n-2);
}

2. Function pointer

The function pointer is used to store the address of the function, which is the entry address of a function.

The function name represents the entry address of the function;

The general form of a function pointer variable declaration is as follows:

<data type> (*<function pointer name>)( <parameter description list>) ;

<data type> is the return value type of the function pointed to by the function pointer.

<parameter description list> should be consistent with the formal parameter description of the number pointed to by the function pointer.

In (*<function pointer name>), * indicates that the pointer ( ) cannot be defaulted, indicating that it is a pointer to a function.

#include <stdio.h>

int add(int a, int b) {
        return a+b;
}
int sub(int a, int b) {
        return a-b;
}
int mul(int a, int b) {
        return a*b;
}

int main(int argc, char *argv[])
{
        int m = 10, n = 20;
        
        int  (* p)(int, int);    //函数指针的声明

        p = add;                  //函数指针的赋值

        //printf("%d\n", add(m, n));
        printf("%d\n", (*p)(m, n));   //函数指针的调用

        p = sub;
        printf("%d\n", (*p)(m, n));

        return 0;
}

array of function pointers

The function pointer array is an array that holds several function names

The general form is as follows:

<data type> (*<function pointer array name>[<size>])(<parameter description list>);

• Among them, <size> refers to the number of elements of the function pointer array

•Others are the same as ordinary function pointers

#include <stdio.h>

int add(int a, int b) {
        return a+b;
}
int sub(int a, int b) {
        return a-b;
}
int mul(int a, int b) {
        return a*b;
}

int main(int argc, char *argv[])
{
        int m = 10, n = 20;
        
        int  (* p[2])(int, int);//int * p[3]

        p[0] = add;

        //printf("%d\n", add(m, n));
        printf("%d\n", (*p[0])(m, n));

        p[1] = sub;
        printf("%d\n", (*p[1])(m, n));

        return 0;
}

Use of qsort function

#include <stdio.h>
#include <stdlib.h>

int compare(const void *, const void *);

int main(int argc, char *argv[])
{
        int s[] = {89, 23, 10, 8, 7, 61}, n, i;

        n = sizeof(s)/sizeof(int);

        qsort(s, n, sizeof(int), compare);

        for (i = 0; i < n; i++)
                printf("%d ", s[i]);
        puts("");

        return 0;
}

int compare(const void * p, const void * q) 
{
        return (*(int *)p - *(int *)q);

}

Guess you like

Origin blog.csdn.net/qq_52049228/article/details/129680510