Thread: Linking a function to another function

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    43

    Linking a function to another function

    So...

    in my

    int main () function I have some code

    is it possible to make another function like

    Code:
    my_name ()
    {
    blah
    }
    and show that aswell? or do I have to call it like in php or something?

  2. #2
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719
    I suggest you read the function tutorial on this site.
    Videogame Memories!
    A site dedicated to keeping videogame memories alive!

    http://www.videogamememories.com/
    Share your experiences with us now!

    "We will game forever!"

  3. #3
    Registered User IdioticCreation's Avatar
    Join Date
    Nov 2006
    Location
    Lurking about
    Posts
    229
    Well typically you declare your functions before int main. Then define them after int main. Then you can use them inside main. Like this:
    Code:
    int add(int num_one, int num_two);
    
    int main() {
        cout<< add (4, 5);
    }
    
    int add (int num_one, int num_two) {
        int result;
        result = num_one + num_two;
        return result;
    }
    Also remember that in your functions you can't call for a variable that was initialized in main, unless you pass it to the function.

    Inline functions work a little bit different....and you know, there are tutorials on this stuff.
    Last edited by IdioticCreation; 01-17-2007 at 08:46 AM.

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Code:
    #include <iostream>
    using namespace std;
    
    int calculate(int a, int b)
    {
    	int result = (a + b) * 2;
    	return result;
    }
    	
    
    int main()
    {
    	
    	int num = calculate(10, 20);
    	cout<<num<<endl;
    	
    	cout<<"Hit return to exit.";
    	cin.get();  //to keep the console window open for your viewing pleasure
    
    	return 0;
    }

  5. #5
    Registered User
    Join Date
    Jan 2007
    Posts
    43

    ok

    Thanks

    Can someone point me to a good tutorial?

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    C++ is a fairly big language, and you need to get a recent book that is no more than a couple of years old. Is that an option for you?

  7. #7
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  8. #8
    Registered User
    Join Date
    Jan 2007
    Posts
    43
    7stud C++ is a fairly big language, and you need to get a recent book that is no more than a couple of years old. Is that an option for you?
    Possibly, if something Im trying to do doesn't work out, (it should though) then I will be doing this, otherwise I wont

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM