1. The call-by-
function parameter arguments and occupy different memory blocks, respectively, of the modification of the parameter does not affect the argument.
2. call pass-
pass-calling is the way to pass the memory address of the function of external variables to create a way to call the function arguments.
This approach allows parameter passing variables outside of functions and function is really to establish a positive relationship, that is, the internal function can be directly manipulated variables outside the function.
Exercise:
1. Call function to achieve two-digit exchange
#include <stdio.h>
int Swap(int *pa, int *pb)
{
int temp = 0;
temp = *pa; //等价于temp=a;
*pa = *pb;
*pb = temp;
return 0;
}
int main()
{
int a = 10;
int b = 20;
printf("a=%d b=%d\n", a, b);
Swap(&a, &b);
printf("a=%d b=%d\n", a, b);
return 0;
}.
2. Call function to identify a leap year between 1000-2000
int is_leap_year(int y) //调用函数找出润年
{
if ((y % 4 == 0 && y % 100 != 0) || (y % 400 == 0))
{
return 1;
}
else
{
return 0;
}
}
int main()
{
int count = 0;
int year = 0;
for (year = 1000; year < 2001; year++)
{
if (is_leap_year(year) == 1)
{
count++;
printf("%d ", year);
}
}
printf("count=%d\n", count);
return 0;
}
3. Call function to find prime numbers between 100-200
#include <math.h> //sqrt引用的头文件
int is_prime(int n)
{
//判断n是否为素数
int i = 0;
for (i = 2; i < sqrt(n); i++)
{
if (n%i == 0)
{
return 0;
}
}
return 1;
}
int main()
{
int count = 0;
int i = 0;
for (i = 100; i <= 200; i++)
{
if (is_prime(i) == 1)
{
printf("%d\n", i);
count++;
}
}
printf("%d\n", count);
return 0;
}
4. Call function using a binary search to find the specified element subscript
int binary_search(int arr[], int k,int sz)
{
int left = 0;
int right = sz - 1;
int mid = (left + right) / 2;
while (left <= right)
{
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 = 0;
int sz = sizeof(arr) / sizeof(arr[0]);
scanf("%d", &k);
//数组传参的时候,传过去一个数组名,本质上传过去的是数组首元素的地址
int ret=binary_search(arr,k,sz); //二分查找 找到返回下标,找不到返回-1
if (ret == -1)
{
printf("找不到\n");
}
else
{
printf("找到了,下标是%d\n", ret);
}
return 0;
}