Calling functions from different files.

This is a discussion on Calling functions from different files. within the C++ Programming forums, part of the General Programming Boards category; Hi all, I'm not very sure about linking but I managed to create a project (using VC++ 6) and all ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    6

    Question Calling functions from different files.

    Hi all,

    I'm not very sure about linking but I managed to create a project (using VC++ 6) and all my source are in the specific folder which was created for that project.

    Can I know how can I do a function call of another .cpp file that will be compiled and put into the same build (.exe)?

    Example:

    Code:
    void accessories(void)
    {
    	int choice;
    
    	do
    	{
    		system("cls");
    
    		cout << "==================" << endl;
    		cout << "1.) Converter" << endl;
    		cout << "2.) Back to Main Menu" << endl;
    		cout << "==================" << endl;
    		
    		cout << "Please select: ";
    		cin >> choice;
    		
    		switch (choice)
    		{				
    			case 1 : cout << "Converter" << endl;
    				// Goes to another file which stores other stuff
    				break;
    		}
    
    	} while (choice != 3 );
    
    }
    The comment in bold is what i meant by function call of another file. So, when it does a function call, it'll read from a function in the converter.cpp file.

    Thanks in advance

  2. #2
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    Ok, lets say you have a file named "myfunctions.cpp" that contains
    Code:
    #include <iostream.h>
    void SayHello()
    {
       cout << "Hello";
    }
    Then in your other file you might have
    Code:
    #include "myfunctions.cpp"
    void accessories(void)
    {
    	int choice;
    
    	do
    	{
    		system("cls");
    
    		cout << "==================" << endl;
    		cout << "1.) Converter" << endl;
    		cout << "2.) Back to Main Menu" << endl;
    		cout << "==================" << endl;
    		
    		cout << "Please select: ";
    		cin >> choice;
    		
    		switch (choice)
    		{				
    			case '1': sayhello(); // Goes to another file which stores other stuff
    				break;
    		}
    
    	} while (choice != 3 );
    
    }
    You'll notice its the exact same as calling a function inside the file, except you need to "#include """ the file at the start.

    ~ Paul

  3. #3
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    Yeh same thing, I myself like to use HPP files and keep my function declarations there, but he said he wanted to use a cpp file.

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    6
    Hi stovellp,

    It didn't work though I did #include filename.cpp

    However, I added the files into the same project workspace, and it automatically reference it when I did a function call which is stored in another file.

    I'm using Visual C++ 6. However, I'm wondering, why doesn't it work for #include filename.cpp

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 10-23-2006, 07:45 AM
  2. Calling functions within functions
    By saahmed in forum C Programming
    Replies: 1
    Last Post: 03-09-2006, 01:03 AM
  3. Calling functions via variable
    By SoundFX in forum C++ Programming
    Replies: 3
    Last Post: 08-30-2005, 10:26 PM
  4. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 02:35 AM
  5. calling functions in parallel
    By zeus in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 02-01-2002, 10:48 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21