C Tutorial/Function/Function Prototype — различия между версиями
Admin (обсуждение | вклад) м (1 версия: Импорт контента...) |
|
(нет различий)
|
Версия 17:21, 25 мая 2010
Function is defined above the function main: it is not necessary to prototype the function
<source lang="cpp">#include <stdio.h> void f1(int *k) {
*k = *k + 10;
} main ( ){
int i; i = 0; printf (" The value of i before call %d \n", i); f1 (&i); printf (" The value of i after call %d \n", i);
}</source>
The value of i before call 0 The value of i after call 10
Function is defined after main: prototype the function
<source lang="cpp">#include <stdio.h> f1(int *k); main ( ){
int i; i = 0; printf (" The value of i before call %d \n", i); f1 (&i); printf (" The value of i after call %d \n", i);
} f1(int *k) {
*k = *k + 10;
}</source>
The value of i before call 0 The value of i after call 10
Function prototype
<source lang="cpp">#include <stdio.h> int change(int number); /*Function prototype */ int main(void) {
int number = 10; int result = 0; result = change(number); printf("\nIn main, result = %d\tnumber = %d", result, number); return 0;
} int change(int number) {
number = 2 * number; printf("\nIn function change, number = %d\n", number); return number;
}</source>
In function change, number = 20 In main, result = 20 number = 10