C语言函数基础介绍

#include 
#include 
  void valueTwoNumber(int *a,int *b){   
int temp;
temp = *a;
*a = *b;
*b = temp;  
}

void valueTwoString(char **str,char **str1){

char *temp;
temp = *str;
*str = *str1;
*str1 = temp;
}

//函数:对一个数组进行排序
void sortArray(int a[],int n){
int i,j;    
int temp = 0;
for(i = 0;i < n;i++){
    for(j = 0;j < n-1-i;j++){
        if(a[j] < a[j+1]){
            temp = a[j];
            a[j] = a[j+1];
            a[j+1] = temp;
        }
    }
}
for(i = 0;i < n;i++){
    printf("%d\n",a[i]);
  } 
} 

//写函数:实现数组中能同时被2和3整除的数的和 
//找出这个同时能被2和3整除的数的个数并返还 
int sumArray(int a[],int n,int *count){

int i,sum = 0;
*count = 0;
for(i = 0;i < n;i++){
    
    if(a[i] % 2==0&&a[i]%3==0){
        
        sum +=a[i];
        (*count)++;
    }
}
return sum;

} 
/*
构造函数要点:
1、首先需要了解函数的实现功能 
2、判断函数是否有返回值
3、针对参数传递时,遵守尽量简洁方便、灵活, 
*/
//构造一个函数:比较两个字符串的大小,并返还大的字符串 
char *compareTwoString(char *str,char *str1){
int k;
k = strcmp(str,str1);
if(k == 0){
    return str;
}else if(k < 0){
    
    return str1;
}else{
    
    return str;
  } 
}

//构造一个函数:比较两个字符串的大小,并返还大的字符串 
char *compareTwoStr(char *str,char *str1){
int n;
n = strcmp(str,str1);
if(n == 0){
    return str;
}else if(n < 0){
    
    return str1;
}else {
    return str;
  } 
}
//构造一个函数:判断一个字符在这个字符串里边?
int judgeCharInString(char *str,char ch){

int n = 0;
char *s1 = NULL;
s1 = strchr(str,ch);
if(s1 !=NULL){
    n = 1;
}else{
    n = 0;
 }  
    return n;
} 

int  judgeStringInString(char *str,char *str1){

char *s;
s = strstr(str,str1);
if(s != NULL){
    return 1;
}else{
    
    return 0;
}
}

//构造一个指针函数,通过返还一个指针得到两个整数的和
int *backTwoNumberSum(int a,int b){

int *p; 
int sum = 0;
sum = a + b;
p = ∑   
return p;

}
//函数指针 
/*
int (*sum)(int a,int b){

}
*/
//sum  sum1;

typedef (*SumTwoNumber)(int a,int b);//SumTwoNumber是函数指针类  型名称 
int sumNumber(int a,int b)
{
return a+b;
}
int chengNumber(int a,int b){

return a * b;
}

//构造一个函数,求1+2+3+---+100的和
int sumOneToThoud(int n){
if(n == 1){
    return 1;
}else{
    return n+sumOneToThoud(n-1);
}   
} 
//通过使用递归函数,求100以内能被2  3  5  同时整除的数

int valueToThous(int n){

if(n == 1){
    return 0;
}   
if(n % 12 == 0){
    printf("%d\n",n);
    
}
return valueToThous(n-1);   
} 

 //使用递归函数,求任何数的阶乘 5! = 5 *4*3*2*1=120
 int jiecheng(int n){

if(n == 1)return 1;
else return n * jiecheng(n-1);

 } 

 int backArrayBigValue(int a[],int n){
int max = a[0];
int i;
for(i = 0; i < n;i++){
    if(max < a[i])
    max = a[i];
 }  
return max; 
 }

//abcdef   fedcba
char *stringBackString(char str[],int n){
char *p = NULL,*q = NULL;
char s;
p = str;
q = str + n-1;
while(p < q){
    s = *p;
    *p = *q;
    *q = s;
    p++;
    q--;        
}
printf("%s\n",str);
return str; 
}

int main(int argc, char **argv)
{

int a[] = {
    7,8,45,123,458,1655,87895
};
int sum = backArrayBigValue(a,sizeof(a)/sizeof(int));
printf("sum = %d\n",sum);
char str[] = "helloworld";
int n = strlen(str);
char *s; 
s = stringBackString(str,n);
printf("%s\n",*s);


int a = 5;
int  sum = jiecheng(20);
printf("sum = %d\n",sum);

int backSum = 0; 
SumTwoNumber sum;//创建一个函数指针变量 
sum = sumNumber;//为函数指针变量赋值,这个值就是地址,就是函数的名称,就是函数的入口
backSum = (*sum)(10,20);
printf("%d\n",backSum);


int sum = 0;
sum = sumOneToThoud(5);
printf("sum = %d\n",sum);*/


int n = valueToThous(100);



//数组和函数之间的关系
//数组作为形参传递

int a[] = {65,99,12,36,45,67};
int n,count,sum = 0;
n = sizeof(a)/sizeof(int);
printf("n = %d\n",n);
sum = sumArray(a,n,&count); 
printf("count = %d sum = %d\n",count,sum);


char *str = "helloworld";
char *str1 = "iiiiii";
char *p;
p = compareTwoStr(str,str1);
printf("%s\n",p);

char *str = "adfasdfsadfsadf";
char ch ='d';
int n;
n = judgeCharInString(str,ch);
if(n == 1){
    printf("找到了\n");
}else{
    printf("没有找到\n");
} 


char *str = "mynameisjack";
char  *str1 = "jack";
int n;
n = judgeStringInString(str,str1);
if(n == 1){
    
    printf("找到了\n");
}else{
    
    printf("没有找到\n");
}


    数据类型 * 函数的名称(形参){
        
        语句块; 
    
        return  指针; 
    } 



int a = 20;
int b = 30;
int *sum;
sum = backTwoNumberSum(a,b);
printf("sum = %d\n",*sum);


//递归函数


    函数本身调用函数本身,但是总会有一个条件出口,从而结束该函数调用。 
    
return 0;

}

你可能感兴趣的:(C语言函数基础介绍)