4. 函数的调用
4.1 传值调用
函数的形参和实参分别占有不同内存块,对形参的修改不会影响实参
4.2 传址调用
传址调用是把函数外部创建变量的内存地址传递给函数参数的一种调用函数的方式
这种传参方式可以让函数和函数外边的变量建立起真正的联系,也就是函数内部可以直接操作函数外部的变量
4.3 练习
- 写一个函数可以判断一个数是不是素数
方法一
#include <stdio.h>
int main()
{int i = 0;int count = 0;for (i = 100; i <= 200; i++){int flag = 1;int j = 0;for (j = 2; j <= i - 1; j++){if (i % j == 0){flag = 0;break;}}if (flag == 1){count++;printf("%d ", i);}}printf("\ncount=%d\n", count);return 0;
}
方法二
#include <stdio.h>
#include <math.h>
int main()
{int i = 0;int count = 0;for (i = 100; i <= 200; i++){int j = 0;int flag = 1;for (j = 2; j <= sqrt(i); j++){if (i % j == 0){flag = 0;break;}}if (flag == 1){count++;printf("%d ", i);}}printf("\ncount=%d\n", count);return 0;
}
方法三
#include <stdio.h>
#include <math.h>
int is_prime(int a)
{int j = 0;for (j = 2; j <= sqrt(a); j++){if (a % j == 0){return 0;}}return 1;
}
int main()
{int i = 0;int count = 0;for (i = 101; i <= 200; i+=2){if (is_prime(i)){printf("%d ", i);count++;}}printf("\ncount=%d\n", count);return 0;
}
方法四
#include <stdio.h>
#include <stdbool.h>
//bool布尔类型
bool is_prime(int a)
{int j = 0;for (j = 2; j <= sqrt(a); j++){if (a % j == 0){return false;}}return true;
}
int main()
{int i = 0;int count = 0;for (i = 101; i <= 200; i+=2){if (is_prime(i)){printf("%d ", i);count++;}}printf("\ncount=%d\n", count);return 0;
}
- 写一个函数判断一年是不是闰年
方法一
#include <stdio.h>
int main()
{int year = 0;for (year = 1000; year <= 2000; year++){if (year % 4 == 0){if (year % 100 != 0){printf("%d ", year);}}if (year % 400 == 0){printf("%d ",year);}}return 0;
}
方法二
#include <stdio.h>
int main()
{int year = 0;for (year = 1000; year <= 2000; year++){if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)){printf("%d ", year);}}return 0;
}
方法三
#include <stdio.h>
int is_leap_year(int i)
{if (((i % 4 == 0) && (i % 100 != 0)) || (i % 400 == 0))return 1;elsereturn 0;
}
int main()
{int year = 0;for (year = 1000; year <= 2000; year++){if (is_leap_year(year)){printf("%d ", year);}}return 0;
}
- 写一个函数,实现一个整形有序数组的二分查找
#include <stdio.h>
int binary_search(int arr[], int k, int sz)
{int left = 0;int right = sz - 1;while (left <= right){int mid = left + (right - left) / 2;if (arr[mid] < k){left = mid + 1;}else if (arr[mid] > k){right = mid - 1;}else{return mid;}}return -1;
}
int main()
{int arr[] = { 1,2,3,4,5,6,7,8,9,10 };int k = 7;int sz = sizeof(arr) / sizeof(arr[0]);int ret = binary_search(arr, k, sz);if (ret == -1){printf("没找到\n");}else{printf("找到了,下标是:%d\n", ret);}
}
//形参和实参的名字可以相同,也可以不同
//下面是一个错误的示范
//数组传参实际上传递的是数组首元素的地址,而不是整个数组
//所以在函数内部计算一个函数传参部分的数组的元素个数是不可靠的
#include <stdio.h>
int binary_search(int arr[], int k)
{int sz = sizeof(arr) / sizeof(arr[0]);//形参arr看上去是数组,本质是指针变量int left = 0;int right = sz - 1;while (left <= right){int mid = left + (right - left) / 2;if (arr[mid] < k){left = mid + 1;}else if (arr[mid] > k){right = mid - 1;}else{return mid;}}return -1;
}
int main()
{int arr[] = { 1,2,3,4,5,6,7,8,9,10 };int k = 7;int ret = binary_search(arr, k);if (ret == -1){printf("没找到\n");}else{printf("找到了,下标是:%d\n", ret);}
}
- 写一个函数,每调用一次这个函数,就会将 num 的值增加1
方法一
#include <stdio.h>
void Add(int* p)
{(*p)++;
}
int main()
{int num = 0;Add(&num);printf("%d\n", num);Add(&num);printf("%d\n", num);Add(&num);printf("%d\n", num);Add(&num);printf("%d\n", num);Add(&num);printf("%d\n", num);return 0;
}
方法二
#include <stdio.h>
int Add(int n)
{return ++n;
}
int main()
{int num = 0;num = Add(num);printf("%d\n", num);num = Add(num);printf("%d\n", num);num = Add(num);printf("%d\n", num);num = Add(num);printf("%d\n", num);return 0;
}