第五周学习总结
函数
定义
函数是一个完成特定功能的代码模块,其程序代码独立,通常要求有返回值,也可以是空值
一般形式如下:
<数据类型><函数名称> (<形式参数>)
{
语句序列;
return[(<表达式>)];
}
·数据类型:是整个函数的返回值类型。
·return语句中表达式的值要和函数的数据类型一致,如无返回值应写为void。
·形式参数说明是逗号","分隔的多个变量的说明形式。
·大括弧对语句序列,称为函数体,是大于等于零个语句构成的。
·函数的说明就是指函数原型。
·其中,形式参数说明可以缺省说明的变量名称,但是类型不能缺省。
例如
double Power(double x,int n);
double Power(double ,int );
函数的使用也叫做函数的调用,形式如下:
函数名称(<实际参数>)
实参就是在使用函数时,调用函数传递给被调函数的数据。需要确切的数据
函数调用可以作为一个运算量出现在表达式中,也可以单独形成一个语句,对于无返回值的函数来讲只能形成一个函数调用语句。
题目,编写一个函数并显示

题目:带参传递
定义求x^n指的函数(x是实数,n为正整数)
#include<stdio.h>
#include<stdlib.h>
double power(double x,int n) //函数声明
{
int i; //定义
double r=1;
for(i=0;i<n;i++)
{
r = r*x;
}
return r;
}
int main() //函数调用
{
double a;
int b;
double result;
printf("输入: ");
scanf("%lf %d",&a,&b);
result=power(a,b);
printf("result = %lf",result);
return 0;
}
函数的传递参数
函数之间的参数传递方式:
1.全局变量
2.复制传递的方式
实参为数组的指针,形参为数组名(本质是一个指针变量)
3.地址传递的方式
实参为数组的指针,形参为同类型的指针变量
全局变量
全局变量就是在函数体外说明的变量,它在程序中的每个函数里都是可见的。
全局变量一经定义后就会在程序的任何地方可见。函数调用的位置不同
程序的执行结果可能会受到影响,不建议使用。
#include<stdio.h>
#include<stdlib.h>
double a=2; //把函数调用中的a,b移到头文件这里了,能安全运行
int b=3; //但是调用时改变了ab大小会出错
double power(double x,int n)
{
int i;
double r=1;
for(i=0;i<n;i++)
{
r = r*x;
}
return r;
}
int main()
{
double result;
// scanf("%lf %d",&a,&b);
result=power(a,b);
printf("result = %lf",result);
return 0;
}
复制传递方式
调用函数将实参传递给被调用函数,被调用函数将创建同类型的形参并用实参初始化。
形参是新开辟的存储空间,因此在函数中改变形参的值,不会影响到实参。
#include<stdio.h>
#include<stdlib.h>
double power(double x,int n) //x,n为形参
{
int i;
double r=1;
printf("&x = %p &n = %p \n",&x,&n);
for(i=0;i<n;i++)
{
r = r*x;
}
return r;
}
int main()
{
double a; //a,b为实参
int b;
double result;
scanf("%lf %d",&a,&b);
printf("&a = %p &b = %p \n",&a,&b);
result=power(a,b);
printf("result = %lf",result);
return 0;
}

地址传递方式
按地址传递,实参为变量的,而形参为同类型的指针
被调用函数中对形参的操作,将直接改变实参的值
(被调用函数对指针的目标操作相当于对实参本身的操作)
用函数实现位置交换
#include<stdio.h>
#include<stdlib.h>
void swap (int *x,int *y) //指针交换地址
{
int t;
t=*x;
*x=*y;
*y=t;
}
int main()
{
int a=10;
int b=20;
int t;
printf("before swap,a= %d b=%d \n",a,b);
swap(&a,&b);
printf("before swap,a= %d b=%d \n",a,b);
return 0;
}

题目:编写一个函数统计字符串中大小字母的个数,并把小写转化成大写,大写转换成小写
#include<stdio.h>
#include<stdlib.h>
int strfun(char *p);
int main()
{ //调用
char s[] = "helloworld2026";
int n;
n = strfun(s);
printf("total is %d \n",n);
printf("str is %s \n",s);
return 0;
}
int strfun(char *p) //函数声明
{
int n=0;
while(*p != '\0') //定义
{
if(*p <='z' && *p >= 'a')
{
n++;
*p -= 32; //这行用于将小写字符变为大写字符
}
p++;
}
return n;
}

数组在函数中的传参
全局数组传递的方式
复制传递方式
实参为数组的指针,形参为数组名(本质上是一个指针变量)
地址传递方式
实参为数组的指针,形参为同类型的指针变量。
题目:计算一个一维整型数组的所有元素的和
#include<stdio.h>
#include<stdlib.h>
int array_sum(int array[],int n);
int main()
{
int a[]={1,2,3,4,5};
int sum = 0;
int n = sizeof(a) / sizeof(int);
sum = array_sum(a,n);
printf("sum = %d \n",sum);
return 0;
}
int array_sum(int array[],int n)
{
int sum = 0;
int i=0;
printf("n = %d \n",n);
for(i=0;i<n;i++)
{
sum = sum +array[i];
}
return sum;
}

题目:删除字符串中的空格
#include<stdio.h>
#include<stdlib.h>
void del_space(char *s);
int main()
{
char s[] = "what the hell";
puts (s);
del_space(s);
puts(s);
return 0;
}
void del_space(char *s)
{
char *s2 = s;
while (*s != '\0')
{
if(*s == ' ')
{
s++;
}
else
{
*s2 = *s;
s2++;
s++;
}
}
*s2 = '\0';
}

指针函数
指针函数是指一个函数的返回值为地址量的函数
指针函数的定义的一般形式如下
<数据类型> *<函数名称> (<参数说明>)
{
语句序列;
}
返回值:全局变量的地址/static变量地址/字符串常量的地址
题目:编写一个指针函数,删除一个字符串中的空格
#include<stdio.h>
#include<stdlib.h>
char *del_space(char *s);
int main()
{
char s[] = "Secret Place";
char *r;
puts(s);
r = del_space(s);
puts(r);
return 0;
}
char *del_space(char *s) //与传参函数不同的是,数据类型char后加了个* 号
{
char *p = s;
char *r = s;
while(*s != '\0')
{
if(*s == ' ')
{
s++;
}
else
{
*p = *s;
p++;
s++;
}
}
*p = '\0';
return r;
}
题目:编写一个指针函数,实现字符串连接
系统自带函数实现字符串连接
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
char dest[50]="hello";
char src[]="world";
puts(strcat(dest,src));
return 0;
}
自己编写函数实现
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char *mystrcat(char *dest,const char *src);
int main()
{
char dest[50]="hello";\
char src[]="world";
puts(mystrcat(dest,src));
return 0;
}
char *mystrcat(char *dest,const char *src)
{
char *r=dest;
while(*dest != '\0')
{
dest++;
}
while(*src != '\0')
{
*dest = *src;
dest++;
src++;
}
*dest='\0';
return r;
}
递归函数
递归函数是指一个函数的函数体中直接或间接调用了该函数自身。
递归函数调用的执行过程分为两个阶段:
递推阶段:从原问题出发,按递归公式递推从未知到已知,最终达到递归的终止条件
回归阶段:按递归终止条件求出结果,并逐步代入递归公式,回归到原问题求解
题目:计算N的阶层
#include<stdio.h>
#include<stdlib.h>
int a(int n);
int main()
{
int n=0;
printf("input:");
scanf("%d",&n);
printf("%d \n",a(n));
return 0;
}
int a(int n)
{
if(n ==0 || n ==1)
{
return 1;
}
return n*a(n-1); //此处能让不是0,1的输入返回,直到循环为1或0来实现实现阶乘
}

函数指针
函数指针用来存放函数的地址,这个地址是一个函数的入口地址
函数名代表了函数的入口地址
函数指针变量说明的一般形式如下:
<数据类型> (*<函数指针名称>) (<参数说明列表>);
1.数据类型是函数指针所指向的函数的返回值类型
2.参数说明列表应该与函数指针所指向参数的形参说明保持一致
3.函数指针名称中,* 说明为指针不可缺省,表明为函数指针
#include<stdio.h>
#include<stdlib.h>
int add(int a,int b); //函数返回值类型
int main()
{
int a=1,b=2;
int (*p) (int a,int b); //形式,a,b为形参
p=add;
printf("result = %d \n",(*p)(a,b));
return 0;
}
int add(int a,int b)
{
return a+b;
}
函数指针数组
函数指针数组是一个保持若干个函数名的数组
一般形式:
<数据类型>(函数指针数组名称>)(<大小>)(<参数说明列表>);
1.大小,是指函数指针数组元素个
2.其他同普通的函数指针一样
#include<stdio.h>
#include<stdlib.h>
int add(int a,int b);
int sub(int a,int b);
int main()
{
int a=1,b=2;
int (*p[2]) (int a,int b); //在函数指针的一般形式上加了个[],指针名称变成指针数组了
p[0]=add;
printf("result = %d \n",(*p[0])(a,b));
p[1]=sub;
printf("result = %d \n",(*p[1])(a,b));
return 0;
}
int add(int a,int b)
{
return a+b;
}
int sub(int a,int b)
{
return a-b;
}
AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。
更多推荐



所有评论(0)