C语言—函数

作者:追风剑情 发布于:2019-9-16 20:47 分类:C

示例一:一个简单函数

  1. //Visual Studio中加上这句才可以使用scanf()
  2. //否则只能使用scanf_s()
  3. #define _CRT_SECURE_NO_WARNINGS
  4. #include <stdio.h>
  5. #include <stdbool.h>
  6.  
  7. #define NAME "GIGATHINK, INC."
  8. #define ADDRESS "101 Megabuck Plaza"
  9. #define PLACE "Megapolis, CA 94904"
  10. #define WIDTH 40
  11.  
  12. void starbar(void); /* ANSI C风格函数原型 */
  13. //void starbar(); /* 对于不识别ANSI C风格的编译器这样声明 */
  14. //一些老的编译器甚至连void都识别不了
  15.  
  16. //argc: 参数个数 argv[]: 参数数组
  17. int main(int argc, char *argv[])
  18. {
  19. // 也可以在函数里面声明
  20. //void starbar(void);
  21.  
  22. starbar();
  23. printf("%s\n", NAME);
  24. printf("%s\n", ADDRESS);
  25. printf("%s\n", PLACE);
  26. starbar(); /* 调用函数 */
  27.  
  28. system("pause");
  29. return 0;
  30. }
  31.  
  32. void starbar(void) /* 定义函数 */
  33. {
  34. int count;
  35. for (count = 1; count <= WIDTH; count++)
  36. putchar('*');
  37. putchar('\n');
  38. }

运行测试
1111.png

示例二:对示例一进行改进

  1. //Visual Studio中加上这句才可以使用scanf()
  2. //否则只能使用scanf_s()
  3. #define _CRT_SECURE_NO_WARNINGS
  4. #include <stdio.h>
  5. #include <stdbool.h>
  6.  
  7. #define NAME "GIGATHINK, INC."
  8. #define ADDRESS "101 Megabuck Plaza"
  9. #define PLACE "Megapolis, CA 94904"
  10. #define WIDTH 40
  11. #define SPACE ' '
  12.  
  13. // 推荐的声明方式
  14. void show_n_char(char ch, int num);
  15. // 推荐的声明方式,根据个人喜好声明函数时也可以不写变量名
  16. //void show_n_char(char, int);
  17.  
  18. // 也可以不声明参数(已废弃)
  19. //void show_n_char();
  20.  
  21. // ANSI C之前的声明形式(已废弃)
  22. //void show_n_char(ch, num)
  23. //char ch;
  24. //int num;
  25.  
  26.  
  27. //argc: 参数个数 argv[]: 参数数组
  28. int main(int argc, char *argv[])
  29. {
  30. int spaces;
  31. show_n_char('*', WIDTH);
  32. putchar('\n');
  33. show_n_char(SPACE, 12);
  34. printf("%s\n", NAME);
  35. spaces = (WIDTH - strlen(ADDRESS)) / 2;
  36. show_n_char(SPACE, spaces);
  37. printf("%s\n", ADDRESS);
  38. show_n_char(SPACE, (WIDTH - strlen(PLACE)) / 2);
  39. printf("%s\n", PLACE);
  40. show_n_char('*', WIDTH);
  41. putchar('\n');
  42.  
  43. system("pause");
  44. return 0;
  45. }
  46.  
  47. void show_n_char(char ch, int num)
  48. {
  49. int count;
  50. for (count = 1; count <= num; count++)
  51. putchar(ch);
  52. }

运行测试
111.png

示例:函数返回值

  1. //Visual Studio中加上这句才可以使用scanf()
  2. //否则只能使用scanf_s()
  3. #define _CRT_SECURE_NO_WARNINGS
  4. #include <stdio.h>
  5. #include <stdbool.h>
  6.  
  7. int imin(int, int);
  8.  
  9. // ANSI C允许使用部分原型(实用于函数参数不固定的情况)。
  10. // 如: int printf(const char *, ...);
  11.  
  12. //argc: 参数个数 argv[]: 参数数组
  13. int main(int argc, char *argv[])
  14. {
  15. int evil1, evil2;
  16. printf("Enter a pair of integers (q to quit):\n");
  17. while (scanf("%d %d", &evil1, &evil2) == 2)
  18. {
  19. printf("The lesser of %d and %d is %d.\n",
  20. evil1, evil2, imin(evil1, evil2));
  21. printf("Enter a pair of integers (q to quit):\n");
  22. }
  23. printf("Bye.\n");
  24.  
  25. system("pause");
  26. return 0;
  27. }
  28.  
  29. // 如果把函数定义放在main()之前,则可以省略函数声明。
  30. int imin(int n, int m)
  31. {
  32. return (n < m) ? n : m;
  33. }

运行测试
1111.png

示例:递归

  1. //Visual Studio中加上这句才可以使用scanf()
  2. //否则只能使用scanf_s()
  3. #define _CRT_SECURE_NO_WARNINGS
  4. #include <stdio.h>
  5. #include <stdbool.h>
  6.  
  7. void up_and_down(int);
  8.  
  9. //argc: 参数个数 argv[]: 参数数组
  10. int main(int argc, char *argv[])
  11. {
  12. up_and_down(1);
  13. system("pause");
  14. return 0;
  15. }
  16.  
  17. void up_and_down(int n)
  18. {
  19. printf("Level %d: n location %p\n", n, &n);
  20. if (n < 4)
  21. up_and_down(n + 1);
  22. printf("LEVEL %d: n location %p\n", n, &n);
  23. }

运行测试
1111.png

示例:尾递归

  1. //Visual Studio中加上这句才可以使用scanf()
  2. //否则只能使用scanf_s()
  3. #define _CRT_SECURE_NO_WARNINGS
  4. #include <stdio.h>
  5. #include <stdbool.h>
  6.  
  7. long fact(int n);
  8. long rfact(int n);
  9.  
  10. //argc: 参数个数 argv[]: 参数数组
  11. int main(int argc, char *argv[])
  12. {
  13. int num;
  14. printf("This program calculates factorials.\n");
  15. printf("Enter a value in the range 0-12 (q to quit):\n");
  16. while (scanf("%d", &num) == 1)
  17. {
  18. if (num < 0)
  19. printf("No negative numbers, please.\n");
  20. else if (num > 12)
  21. printf("Keep input under 13.\n");
  22. else
  23. {
  24. printf("loop: %d factorial = %ld\n",
  25. num, fact(num));
  26. printf("recursion: %d factorial = %ld\n",
  27. num, rfact(num));
  28. }
  29. printf("Enter a value in the range 0-12 (q to quit):\n");
  30. }
  31. printf("Bye.\n");
  32.  
  33. system("pause");
  34. return 0;
  35. }
  36.  
  37. // 使用循环计算
  38. long fact(int n)
  39. {
  40. long ans;
  41. for (ans = 1; n > 1; n--)
  42. ans *= n;
  43. return ans;
  44. }
  45.  
  46. // 使用递归计算
  47. long rfact(int n)
  48. {
  49. long ans;
  50. if (n > 0)
  51. ans = n * rfact(n - 1);
  52. else
  53. ans = 1;
  54. return ans;
  55. }
运行测试
1111.png


示例:递归和倒序计算

  1. //Visual Studio中加上这句才可以使用scanf()
  2. //否则只能使用scanf_s()
  3. #define _CRT_SECURE_NO_WARNINGS
  4. #include <stdio.h>
  5. #include <stdbool.h>
  6.  
  7. void to_binary(unsigned long n);
  8.  
  9. //argc: 参数个数 argv[]: 参数数组
  10. int main(int argc, char *argv[])
  11. {
  12. unsigned long number;
  13. printf("Enter an integer (q to quit):\n");
  14. while (scanf("%lu", &number) == 1)
  15. {
  16. printf("Binary equivalent: ");
  17. to_binary(number);
  18. putchar('\n');
  19. printf("Enter an integer (q to quit):\n");
  20. }
  21. printf("Done.\n");
  22.  
  23. system("pause");
  24. return 0;
  25. }
  26.  
  27. // 使用递归将十进制数用二进制表示
  28. void to_binary(unsigned long n)
  29. {
  30. int r;
  31. r = n % 2;
  32. if (n >= 2)
  33. to_binary(n / 2);
  34. putchar(r == 0 ? '0' : '1');
  35. }
运行测试
1111.png



标签: C语言

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号