函数定义(续) ·函数定义中,函数名后面括号中的内容即为函数参数列表 ·每个具体的参数名前面都是该参数的类型,多个参数之间采 用逗号分隔 ●如果函数不需参数,需要在括号中放置Void表示没有参数 ●函数体部分可以既包括声明又包括语句 double average(double a,double b) double sum; /declaration sum a b; /statement * return sum 2; /statement * 11/47
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 函数定义 (续) 函数定义中,函数名后面括号中的内容即为函数参数列表 每个具体的参数名前面都是该参数的类型,多个参数之间采 用逗号分隔 如果函数不需参数,需要在括号中放置 void 表示没有参数 函数体部分可以既包括声明又包括语句 double average(double a, double b) { double sum; / ∗ declaration ∗ / sum = a + b; / ∗ statement ∗ / return sum / 2; / ∗ statement ∗ / } 11 / 47
函数定义(续2) ·定义在函数体内的变量对其它函数是不可见的,也不可由其 它函数修改 ●C89里面,变量声明必须处在所有语句之前;C99里面则可 以延迟到真正需要使用变量时才声明 ●返回类型为void的void function,其函数体可以为空。可 以在程序开发时有意将函数体暂时留置为空 void foo(void) { } 12/47
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 函数定义 (续 2) 定义在函数体内的变量对其它函数是不可见的,也不可由其 它函数修改 C89 里面,变量声明必须处在所有语句之前;C99 里面则可 以延迟到真正需要使用变量时才声明 返回类型为 void 的 void function,其函数体可以为空。可 以在程序开发时有意将函数体暂时留置为空 void foo(void) { } 12 / 47
函数调用(Function Calls) 函数调用由函数名字后跟括在括号中的参数列表构成,参数 列表的多个参数之间由逗号分隔 average(x,y) print_count(i) foo() 。即使参数为空,调用时的一对括号也不可省略 foo;/**legal but has no effect *** ·函数调用的后面一般需要一个分号将函数调用转为语句 print_count(i); fo0(); 13/47
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 函数调用 (Function Calls) 函数调用由函数名字后跟括在括号中的参数列表构成,参数 列表的多个参数之间由逗号分隔 average(x, y) print_count(i) foo() 即使参数为空,调用时的一对括号也不可省略 foo; / ∗ ∗∗ legal but has no e f fec t ∗∗ ∗ / 函数调用的后面一般需要一个分号将函数调用转为语句 print_count(i); foo(); 13 / 47
函数调用(续) non-void function会返回值,该值可以存储在变量中、被测试、打 印或以其它方式使用 avg average(x,y); if (average(x,y)>0) printf("Average is positiveln"); printf("The average is %gIn",average(x,y)); ●当然non-void function的返回值如果不需要的话也总是可以忽略 并抛弃 average(x,y);/discards return value * ●是否需要函数的返回值视情况而定,比如printf会返回所打印的字 符数,但一般会抛弃printf的返回值 num_chars printf("Hi,Mom!In");/num_chars ==9*/ 14/47
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 函数调用 (续) non-void function 会返回值,该值可以存储在变量中、被测试、打 印或以其它方式使用 avg = average(x, y); if (average(x, y) > 0) printf("Average i s posi tive \n"); printf("The average i s %g\n", average(x, y)); 当然 non-void function 的返回值如果不需要的话也总是可以忽略 并抛弃 average(x, y); /∗ discards return value ∗/ 是否需要函数的返回值视情况而定,比如 printf 会返回所打印的字 符数,但一般会抛弃 printf 的返回值 num_chars = printf("Hi , Mom! \n"); /∗ num_chars ==9 ∗/ 可以在函数调用前加上 (void) 表示确实要抛弃返回值,但没必要一 定这样做 (void) printf("Hi , Mom! \n"); 14 / 47
示例一素数测试 ●测试标准:用从2到该数平方根间的任何整数作为除数去除输入的整数 1 #include <stdbool.h> /*C990ny*/ 2 #include <stdio.h> 3 bool is_prime(int n){ 4 int divisor; 5 if (n <1)return false; 6 for (divisor 2;divisordivisor <n;divisor++) 7 if (n divisor ==0) 8 return false; 9 return true; 10 } 11 int main(void){ 12 int n; 13 printf("Enter a number:") 14 scanf ("%d",&n) 15 if (is_prime(n))printf("Primeln"); 16 else printf("Not primeln"); 17 return 0; 18} 15/47
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 示例 —— 素数测试 测试标准:用从 2 到该数平方根间的任何整数作为除数去除输入的整数 1 #include <stdbool.h> /∗ C99 only ∗/ 2 #include <stdio.h> 3 bool is_prime(int n) { 4 int divisor; 5 if (n <= 1) return false; 6 for (divisor = 2; divisor * divisor <= n; divisor++) 7 if (n % divisor == 0) 8 return false; 9 return true; 10 } 11 int main(void) { 12 int n; 13 printf("Enter a number: "); 14 scanf("%d", &n); 15 if (is_prime(n)) printf("Prime\n"); 16 else printf("Not prime\n"); 17 return 0; 18 } 15 / 47