Thread: Function definitions

  1. #1
    Registered User
    Join Date
    Sep 2011
    Location
    Athens , Greece
    Posts
    357

    Function definitions

    One way to avoid the problem of call-before-definition is to arrange the programm so that the definition of each call precedes all its calls.Unfortunately such an arrangement doesn't always exist and even when it does it may the program harder to understand by putting its functions definitions in an unnatural order

    If I have understood well it means something like :

    Code:
    void f2(void) {
    //...
    }
    
    void f1(void) {
    
    f2();
    }
    So the f2 comes first so we must define it before we call it ... Hence the order is unnatural... I think it means something like that. My reflection is right?

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    yes, I believe you are correct. this is the reason we have declarations and header files. you can keep your definitions in the order you like, and put all the declarations in the header file, so that they're all visible before the call.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  3. #3
    Registered User
    Join Date
    Jun 2013
    Location
    Portugal
    Posts
    11
    Code:
    int f1(){
         f2();
        //....
    }
     
    int f2(){
         //...
    }
    This wouldn't work because f1 was compiled first and at some point it called function f2 and the compiler had no previous knowledge of f2.
    Thats why you need to declare it first after you include the libraries:
    Code:
    #include <stdio.h>
    
    int f2();
    
    int f1(){
         f2();
         //....
    }
    
    int f2(){
         //....
    }
    This way you can write your functions in any logical order that suits you better when coding.
    That happens because when the compiler is reading your code it first sees that there actually exists a function f2 although it doesn't execute its code yet. Then it starts reading your first function, f1, and has no problem with your call to function f2. (You can also, like Elkvis said, declare your functions in an header file and later include it in your .c)

    The example you gave and your reflection are correct, you can avoid previously declaring your functions like I did up there by writing them in a order you know the compiler will execute them, but that can be troublesome when it comes to bigger programs and you need to write your functions in a particular logical way.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 23
    Last Post: 03-07-2011, 05:28 PM
  2. Function Definitions with Iterators
    By pianorain in forum C++ Programming
    Replies: 5
    Last Post: 05-17-2010, 02:58 PM
  3. Function definitions
    By lifeis2evil in forum C++ Programming
    Replies: 21
    Last Post: 11-03-2007, 06:57 PM
  4. help writing function definitions
    By jlmac2001 in forum C++ Programming
    Replies: 2
    Last Post: 04-10-2003, 09:44 PM
  5. Function Definitions
    By PutoAmo in forum C Programming
    Replies: 4
    Last Post: 03-03-2002, 06:24 PM