Thread: Functions

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    100

    Functions

    Okay I really hate making multiple threads, but I was wondering how to create functions. Like my own functions that are like

    fry()
    beat()
    distort()


    You get the idea. Just examples of verbs that I don't think are real standard functions.

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Function:
    Code:
    #include <iostream>
    #include <string>
    
    void fry(std::string &food)
    {
         std::cout<<"You fried "<<food;
    }
    
    int main()
    {
        std::string chicken = "Chicken";
        fry(chicken);
        return 0;
    }
    Woop?

  3. #3
    Registered User
    Join Date
    Jul 2005
    Posts
    100
    What does this mean? (std::string &food)

    And that void fry() at the beginning... were you declaring the function perhaps?
    I guess I just don't understand the whole "std::string" thing. Sorry I'm a noob (as if you couldn't tell already .

  4. #4
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    std::string is the string class provided by C++. I was defining the function if I wanted to prototype it I would have wrote just void fry(std::string &food) and defined it after main(). As for what std::string &food does it is passing the string object itself not a copy so it is "faster" if you wanted you could just write it as void fry(std::string food) and for this function it would produce the same results.
    Woop?

  5. #5
    Registered User mrafcho001's Avatar
    Join Date
    Jan 2005
    Posts
    483
    also you can replace the return type (void for the example above) at the begining to something else
    Code:
    int add(int int1, int int2)  //the int can be replayed with any type
    {
    return int1 + int2;
    }
    
    int main()
    {
       cout << add(2,2) << endl;  //this will display 4
       int integer = add(1, 3);  //integer is now 4
    
    return 0;
    }
    My Website
    010000110010101100101011
    Add Color To Your Code!

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Also you can include <iostream>.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    C++ Beginner
    Join Date
    Jun 2005
    Posts
    39
    Here is a common way to use functions..

    Code:
    #include <iostream>
    #include <string>
    
    int mult(int x, int y)
    {
    return x * y;
    }
    I'm a beginner C++ programmer, but I have studied HTML and Java. So if you need to help me I should catch on fast =)

  8. #8
    *this
    Join Date
    Mar 2005
    Posts
    498
    Code:
    void fry () 
    {
       //put code here
    }
    
    void beat ()
    {
       //put code here
    }
    
    void distort ()
    {
       //put code here
    }
    
    // Here is where your programs "heart" is... When you run the 
    // .exe it starts with the first line from main as you probably know...
    // so we can call your functions right here
    int main ()
    {
       fry ();
       beat ();
       distort ();
    
       return 0;
    }
    The void means it has no return type.
    A return type is when a function can return a value like lets say your function does 5 + 5 and returns 10 because its the sum. Then you can have a variable like...

    "int sum = function(); "
    function returns an integer that is assigned to sum.

    This will give you a good explanation:
    http://www.mvhs.fuhsd.org/bob_vanhoy/pdfs/lesson06.pdf
    http://www.mvhs.fuhsd.org/bob_vanhoy/pdfs/lesson07.pdf
    Last edited by JoshR; 07-22-2005 at 02:37 PM.

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You can call your functions multiple times, too.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

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