书籍:C Primer Plus第六版英文版
A function is a self-contained unit of program code designed to accomplish a particular task.
A function can both produce actions and provide values.
Why should you use functions?
For one, they save you from repetitious programming.
The program can then use that function wherever needed, or you can use the same function in different programs, just as you have used putchar() in many programs.
Also, even if you do a task just once in just one program, using a function is worthwhile because it makes a program more modular, hence easier to read and easier to change or fix.
Formal parameters are local variables, private to the function.
void show_n_char(char, int);
show_n_char(SPACE, 12);
The actual arguments are the space character and 12. These values are assigned to the corresponding formal parameters in show_n_char()—the variables ch and num.
In short, the formal parameter is a variable in the called function, and the actual argument is the particular value assigned to the function variable by the calling function.
The actual argument can be a constant, a variable, or an even more elaborate expression. Regardless of which it is, the actual argument is evaluated, and its value is copied to the corresponding formal parameter for the function.
Because the called function works with data copied from the calling function, the original data in the calling function is protected from whatever manipulations the called function applies to the copies.
Using return has one other effect. It terminates the function and returns control to the next statement in the calling function. This occurs even if the return statement is not the last in the function.
One of the most important C concepts (and sometimes one of the most perplexing) is the pointer, which is a variable used to store an address.
The unary & operator gives you the address where a variable is stored.
/* swap1.c -- first attempt at a swapping function */
#include
void interchange(int u, int v); /* declare function */
int main(void)
{
int x = 5, y = 10;
printf("Originally x = %d and y = %d.\n", x , y);
interchange(x, y);
printf("Now x = %d and y = %d.\n", x, y);
return 0;
}
void interchange(int u, int v) /* define function */
{
int temp;
temp = u;
u = v;
v = temp;
}
Running the program gives these results:
Originally x = 5 and y = 10.
Now x = 5 and y = 10.
Nothing is wrong with interchange(); it does swap the values of u and v. The problem is in communicating the results to main( ). As we pointed out, interchange() uses different variables from main( ), so interchanging the values of u and v has no effect on x and y!
这里可以参考C语言:使用函数实现两个数的交换。
Basically, a pointer is a variable (or, more generally, a data object)
whose value is a memory address.
ptr = &pooh; // assigns pooh's address to ptr
We say that ptr “points to” pooh. The difference between ptr and &pooh is that ptr is a variable, and &pooh is a constant.
int * pi; // pi is a pointer to an integer variable
char * pc; // pc is a pointer to a character variable
float * pf, * pg; // pf, pg are pointers to float variables
The value (*pc) of what pc points to is of type char. What of pc itself?
We describe it as being of type “pointer to char.” The value of pc is an address, and it is represented internally as an unsigned integer on most systems. However, you shouldn’t think of a pointer as an integer type.
There are things you can do with integers that you can’t do with pointers, and vice versa.
For example, you can multiply one integer by another, but you can’t multiply one pointer by another.
So a pointer really is a new type, not an integer type. Therefore, as mentioned before, ANSI C provides the %p form specifically for pointers.
/* swap3.c -- using pointers to make swapping work */
#include
void interchange(int * u, int * v);
int main(void)
{
int x = 5, y = 10;
printf("Originally x = %d and y = %d.\n", x, y);
interchange(&x, &y); // send addresses to function
printf("Now x = %d and y = %d.\n", x, y);
return 0;
}
void interchange(int * u, int * v)
{
int temp;
temp = *u; // temp gets value that u points to
*u = *v;
*v = temp;
}
Answer:
Originally x = 5 and y = 10.
Now x = 10 and y = 5.
Let’s summarize what this example does. We want a function that alters the values x and y. By passing the function the addresses of x and y, we give interchange() access to those variables.
Using pointers and the * operator, the function can examine the values stored at those locations and change them.
When you want to read in a value for a variable (num, for example), you use scanf("%d", &num). That function reads a value and then uses the address you give it to store the value.