Functions

This is a discussion on Functions within the C++ Programming forums, part of the General Programming Boards category; I was wondering how to create my own functions i read tutorials and i read it in my book but ...

  1. #1
    Registered User Joe100's Avatar
    Join Date
    Jul 2003
    Posts
    63

    Functions

    I was wondering how to create my own functions i read tutorials and i read it in my book but neither of them make sence to me if anyone on cprogramming could help please please do
    "Be Quick Or Be Dead"

    [img]
    http://www.danasoft.com/sig/GU.jpg
    [/img]

  2. #2
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    The following is what I wrote in a thread a few days ago:

    Here is an example of a function that happens to return nothing (void). I'd suggest learning each concept with a very simple program before applying it to your larger, more complicated program.
    Code:
    #include <iostream>
    
    // Function prototype goes here above main().
    void Display(int numToDisplay);
    
    int main()
    {
        int myNum = 0;
    
        // Pass any data that is needed by the function.
        Display(myNum);  // Pass a local variable.
        Display(myNum+1);  // Pass the result of an expression.
        Display(2);  // Pass a constant value.
        Display(-1);  // Pass a constant value.
        myNum = 3;  // Change the local variable.
        Display(myNum);  // Pass the modified variable.
        return 0;
    }
    
    // Function definition.  Note it is the same as it was prototyped earlier.
    void Display(int numToDisplay)
    {
        // We now have a copy of the passed in data to use in the function.
        if (numToDisplay < 0)
        {
            std::cout << "Error, " << numToDisplay << " is invalid input." << std::endl;
        }
        else
        {
            std::cout << "Number is " << numToDisplay << std::endl;
    
            // Modifying numToDisplay does not change main()'s version.
            numToDisplay = numToDisplay * numToDisplay;
            std::cout << "Number squared is " << numToDisplay << std::endl;
        }
    }
    Notice how the function is declared (see the prototype) before main(). Then it is called inside main(). And finally it is defined after main(). Make sure you are declaring and calling your text function correctly in addition to defining it.

    Also notice how you have to pass the data to the function so that the function can use it. The function cannot use a variable that is declared inside a different function. For example, Display cannot use myNum because myNum was declared in main(). Instead, a copy of myNum's value must be passed to the Display function so that it can be used.

    Does that help?

  3. #3
    Registered User Joe100's Avatar
    Join Date
    Jul 2003
    Posts
    63
    cool thanks
    "Be Quick Or Be Dead"

    [img]
    http://www.danasoft.com/sig/GU.jpg
    [/img]

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, 02: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

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