C语言函数使用小试牛
来源:互联网 发布:php九大接口编程 编辑:程序博客网 时间:2024/06/18 17:24
函数章节碎片知识小结:
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 choicesint menu(void);// returns number of nights desiredint getnights(void);// calculates price from rate, nights// and displays resultvoid 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;}
指针并不是整数类型,处理整数的操作不能用来处理指针,也不能进行两个指针的乘除,可以理解为指针为一个新类型,不是整数类型。
0 0
- C语言函数使用小试牛
- C语言函数使用
- C语言函数使用笔记
- C语言snprintf函数使用
- C语言qsort函数使用
- C语言inline函数使用
- C语言 strtok函数使用
- C语言 select函数使用
- C语言 select函数使用
- C语言 strtok函数使用
- C语言之函数使用
- c语言函数指针使用注意事项
- C语言的memset()函数的使用
- C语言中va函数的使用
- C语言中getch()函数的使用
- C语言指针,malloc.h函数使用
- C语言使用extern函数要注意
- C语言中Exit函数的使用
- centos6.5环境Redis下载及编译安装
- AngularJS 1.x 和TypeScript(ES6)开发的最佳实践
- IOS手机浏览器无法获取网站视频与音乐,安卓与电脑都可以
- JavaEE之静态,非静态的调用
- 算法16_笔试_数字逻辑推理题2
- C语言函数使用小试牛
- style文件 android window 部分属性说明
- 基础知识之HTTP协议
- 隐马尔可夫模型(HMM)攻略
- 顺序表应用8:最大子段和之动态规划法
- vim常用操作
- 聚类(2)——层次聚类 Hierarchical Clustering
- leetcode 60. Permutation Sequence
- 使用session对购物车的简单实现(oracle数据库+数据源)