Thread: C functions

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    3

    Smile C functions

    Hi , if we take a simple C program that contains a main and a function,is it possible to write the main program in a file and the function in a new file and when the main program executes it calls the function and compile it too ? and how?assuming that the compiler is turbo C++ for DOS ?or i wish if there is a General method to do this......

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Sure. Include the header file which prototypes the functions you've got in another file, in your main file. Then link them all together when you compile. You really should get a new compiler. TC++ is ancient.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Dragon Rider jas_atwal's Avatar
    Join Date
    Nov 2007
    Location
    India
    Posts
    54
    All you need to do is put the function in one file and include it in the main file using "include" keyword. An example is below where we have two files mainfile.c and func.h. func.h will have the function and it will be called from mainfile.c. Compile both of these files and run.


    mainfile.c
    ---------------------------------
    Code:
    #include<stdio.h>
    #include "func.h"
    
    int power(int m, int n);
    
    main(){
        int i;
    
        for(i=0; i<=10; i++)
            printf("%d %d\n", i, power(2,i));
        return 0;
    }
    file 2: func.h
    ----------------------------
    Code:
    int power(int base, int n){
        int i, p;
    
        p = 1;
        for(i = 1; i <= n; i++)
            p = p * base;
        return p;
    }
    -------------------------------------------------------------------
    There are 10 kinds of people in this world....Those who understand binary and those who don't

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You don't put the function in the .h file. You prototype it in the .h file, and put it in its own .c file.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Void Functions Help
    By bethanne41 in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2005, 05:30 PM
  2. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 11:50 AM
  3. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 03:35 AM
  4. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 01:41 PM
  5. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 05:39 AM