C Tutorial/Function/Function Prototype

Материал из C\C++ эксперт
Перейти к: навигация, поиск

Function is defined above the function main: it is not necessary to prototype the function

#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);
}
The value of i before call 0
       The value of i after call 10

Function is defined after main: prototype the function

#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;
}
The value of i before call 0
 The value of i after call 10

Function prototype

#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;
}
In function change, number = 20
     
     In main, result = 20    number = 10