第五阶段—函数—函数的传参—地址传递:从键盘输入一个值位n,分别求出1 + 3 + 5 +..+ n的奇数和。与0 + 2 + 4 + ...+ n -1的偶数和。
作者:互联网
1 #include<stdio.h> 2 void sum(int num, int *x, int *y) 3 { 4 int flag = 0; 5 int i = 0; 6 *x = 0; 7 for(i = 0;i <= num; i = i+2)//变量的递增条件时i= i+2 8 { 9 *x+=i; 10 } 11 12 *y = 0; 13 for(i = 1;i <= num; i = i+2) 14 { 15 *y+=i; 16 } 17 return; 18 } 19 20 int main(int argc, const char *argv[]) 21 { 22 int n = 0;//定义需要传给形参,给形参赋值的变量 23 int even_sum = 0; 24 int old_sum = 0; 25 printf("plese input number:"); 26 scanf("%d",&n); 27 28 sum(n, &even_sum, &old_sum); 29 30 printf("even_sum = %d,old_sum = %d\n",even_sum , old_sum); 31 return 0; 32 }
标签:传参,函数,+..+,int,...+,void 来源: https://www.cnblogs.com/thismajor/p/15369108.html