Divide code on parts in C

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Ovserj1994
    New Member
    • Feb 2015
    • 3

    Divide code on parts in C

    Hello,
    I am trying to divide the following code on three modules. I am sure I did everything correctly, however I got an error. I was wondering if there is something specific about this code that I did not take into consideration. Thank you.


    Code:
    #include<stdio.h>
    
    void function1(void);
    
    void function2(char *str);
    
    void function3(void);
    
    void print_string(char *str);
    
    int num;
    
    int main (void)
    
    {
    
    num = 3;
    
    function1();
    
    print_string("DONE\n");
    
    return 0;
    
    }
    
     
    
    void function1()
    
    {
    
    char phrase[] = "The string passed from function 1";
    
    
    print_string("In function 1");
    
    function2(phrase);
    
    }
    
    void function2(char *str)
    
    
    print_string("In function 2");
    printf("Printing -- %s\n", str);
    
    function3();
    }
    
    void function3()
    {
    
    print_string("In function 3\n");
    
    printf("num cubed = %i\n", num*num*num);
    }
    
    void print_string(char *str)
    {
    
    printf("%s\n",str);
    }
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    OK.

    First, main() only calls function1(). Therefore, you only need the function prototype for function1() in the file with main().

    Second, in the file with function1() you need the function prototype for function2().

    Third, in the file with function2() you need the function prototype for function3().

    Fourth, in the file with function3() you need the function prototype for print_string().

    You now compile all the files and let the linker combine the separate object files into your executable. If you are using a tool like Visual Studio this is done by having one project with five source files. You then just build the project. If you try to do this manually, you will need to write a make file to do the five compiles and the final link. I hope this is not the case since writing make files is archaic.

    Always follow the simple rule: You must declare a function before you call it. This declaration is the function prototype which is just the first line of the function followed by a semi-colon.

    Comment

    • Ovserj1994
      New Member
      • Feb 2015
      • 3

      #3
      Okay. So, I created 3 different files:


      file 1:
      Code:
      #include<stdio.h>
      
      void function1(void);
      
      int num;
      
      int main (void)
      
      {
      
      num = 3;
      
      function1();
      print_string("DONE\n");
      
      return 0;
      
      }
      file2:
      Code:
      #include<stdio.h>
      
      
      
      void function2(char *str);
      void function3(void);
      
      
      
      
      void function1(void)
      {
              char phrase[] = "The string passed from function 1";
              print_string("In function 1");
              function2(phrase);
      }
      
      
      void function2(char *str)
      }
              print_string("In function 2");
              printf("Printing -- %s\n", str);
      
              function3();
      }
      file3:
      Code:
      #include<stdio.h>
      
      
      void function3()
      {
      print_string("In function 3\n");
      printf("num cubed = %i\n", num*num*num);
      }
      
      
      void print_string(char *str)
      {
      printf("%s\n",str);
      }
      Get several errors..
      Last edited by Ovserj1994; Mar 11 '15, 11:09 PM. Reason: -_-

      Comment

      • donbock
        Recognized Expert Top Contributor
        • Mar 2008
        • 2427

        #4
        File1 calls print_string but that function hasn't been declared.
        File2 calls print_string but that function hasn't been declared.
        File3 calls print_string on line 6, but that function isn't declared/defined until line 11.
        File3 uses undeclared variable num.
        Last edited by donbock; Mar 12 '15, 11:21 AM. Reason: Changed "define" to "declare".

        Comment

        Working...