函数章节碎片知识小结:
1、函数的声明、函数调用、函数定义之间的区别:
函数声明是一般在主函数前,以分号结束,
函数定义一般在主函数后,用于实现该函数功能的代码,
函数调用在主函数内部如:stabar(参数1,参数2);
函数调用在主函数内部如:stabar(参数1,参数2);
2、形参与实参
void stabar(void);
第一个void表示函数类型(无返回值),第二个表示函数不带参数
形参是被调函数中的变量,实参是主调函数赋给被调函数的具体值//显示菜单由用户进行选择
//如果不在1~3之间重复显示菜单并要求重新输入,输入4退出
#include <stdio.h>
void show(void); //显示菜单函数
int show_choice(int ,int); //读取输入函数
int main()
{
int rec;
int i;
show();
while((rec=show_choice(1,4))!=4) //判断show_choice函数返回值是否为4
{
printf("i like the choice %d.\n", rec);
show_choice;
}
printf("bey!\n");
return 0;
}
void show(void)
{
printf("please choose one of the following:\n");
printf("1) copy file");
printf("\t\t2) move files\n");
printf("3) remove files");
printf("\t\t4) quit\n");
printf("Enter the number of your choice:\n");
}
int show_choice(int low, int hig)
{
int status;
int good;
while((status=scanf("%d",&good)) !=1 ||(good<low || good>hig)) //判断是否成功读入
{ //输入是否在区间内
printf("%d is not a valid choice; try again:\n",status );
show();
scanf("%d",&status);
}
if(status!=1) //判断输入值非法
{
printf("non-numer intput\n");
good=4;
}
return (good); //将读到的值或者4返回到主函数
}
强调:因为被调函数的值是从主调函数中拷贝而来,被调函数一般情况改变不了主调函数的变量值,
要想改变主调函数的变量值就需要使用地址或指针作为参数来改变
3、递归
递归是函数调用它自己。递归必须有条件测试部分即是有确定的递归层级不能无限制连续调用下去。形象化理解:fun1调用fun2,fun2调用fun3,fun3调用fun4;当fun4结束时控制传回fun3,
fun3结束控制传回fun2,fun2结束控制传回fun1;只不过递归是自己调用自己。
尾递归是在正好在return语句之前,相当于循环。
递归的缺点:1.每次递归都会创建一组变量,占用内存比较严重2.函数调用需要一定是时间,执行速度慢
非常好的函数学习的程序
四家酒店,每家房价不同但是所有的房间价格是一样的,连续住有折扣,设计让用户制定酒店和入住天数后显示费用;
/* usehotel.c -- room rate program */
/* compile with Listing 9.10 */
#include <stdio.h>
#include "hotel.h" /* defines constants, declares functions */
int main(void)
{
int nights;
double hotel_rate;
int code;
while ((code = menu()) != QUIT)
{
switch(code)
{
case 1 : hotel_rate = HOTEL1;
break;
case 2 : hotel_rate = HOTEL2;
break;
case 3 : hotel_rate = HOTEL3;
break;
case 4 : hotel_rate = HOTEL4;
break;
default: hotel_rate = 0.0;
printf("Oops!\n");
break;
}
nights = getnights();
showprice(hotel_rate, nights);
}
printf("Thank you and goodbye.\n");
return 0;
}
/* hotel.c -- hotel management functions */
#include <stdio.h>
#include "hotel.h"
int menu(void)
{
int code, status;
printf("\n%s%s\n", STARS, STARS);
printf("Enter the number of the desired hotel:\n");
printf("1) Fairfield Arms 2) Hotel Olympic\n");
printf("3) Chertworthy Plaza 4) The Stockton\n");
printf("5) quit\n");
printf("%s%s\n", STARS, STARS);
while ((status = scanf("%d", &code)) != 1 ||
(code < 1 || code > 5)) //先检查是否成功读入再检查code的值
{< //pritf中添加*表示*后面的形参代替*的位置,实现动态格式输出
if (status != 1) //scanf中添加*的部分会被忽略,直接跳至下一个空白字符
scanf("%*s"); // dispose of non-integer input
printf("Enter an integer from 1 to 5, please.\n");
}
return code;
}
int getnights(void)
{
int nights;
printf("How many nights are needed? ");
while (scanf("%d", &nights) != 1)
{
scanf("%*s"); // dispose of non-integer input
printf("Please enter an integer, such as 2.\n");
}
return nights;
}
void showprice(double rate, int nights)
{
int n;
double total = 0.0;
double factor = 1.0;
for (n = 1; n <= nights; n++, factor *= DISCOUNT)
total += rate * factor;
printf("The total cost will be $%0.2f.\n", total);
}
/* hotel.h -- constants and declarations for hotel.c */
#define QUIT 5
#define HOTEL1 180.00
#define HOTEL2 225.00
#define HOTEL3 255.00
#define HOTEL4 355.00
#define DISCOUNT 0.95
#define STARS "**********************************"
// shows list of choices
int menu(void);
// returns number of nights desired
int getnights(void);
// calculates price from rate, nights
// and displays result
void showprice(double rate, int nights);
printf("%*s",10,s);等价于printf("%10s",s);
更改主调函数的值注意事项:
/* swap1.c -- first attempt at a swapping function */
#include <stdio.h>
void interchange(int u, int v); /* declare function */
int main(void)
{
int x = 5, y = 10;
printf("Originally x = %d and y = %d.\n", x , y);
interchange(x, y);<span style="white-space:pre"> </span>
printf("Now x = %d and y = %d.\n", x, y);
<span style="white-space:pre"> </span>//此时的x,y还是主调函数内的原值,并没有被被调函数修改
return 0;
}
void interchange(int u, int v) /* define function */
{
int temp;
temp = u;
u = v;
v = temp;
}
用指针可以实现更改主调函数的变量值/* swap1.c -- first attempt at a swapping function */
#include <stdio.h>
void interchange(int u, int v); /* declare function */
int main(void)
{
int x = 5, y = 10;
printf("Originally x = %d and y = %d.\n", x , y);
interchange(&x, &y);
printf("Now x = %d and y = %d.\n", x, y);
return 0;
}
void interchange(int *u, int *v) /* define function */
{
int temp;
temp = *u;
* u = *v;
* v = temp;
}
指针并不是整数类型,处理整数的操作不能用来处理指针,也不能进行两个指针的乘除,可以理解为指针为一个新类型,不是整数类型。