functions

This is a discussion on functions within the C++ Programming forums, part of the General Programming Boards category; I'm just starting to get the hang on functions exept I don't know what the arguments of a function are ...

  1. #1
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052

    Thumbs up functions

    I'm just starting to get the hang on functions exept I don't know what the arguments of a function are for and why and how returning is done. Can anyone help?

    Thanks
    -Chris

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    403

    ok

    hey a supernewbie (haha, jk i'll help you out)

    so if you wanted a function to add two ints:

    int add_ints(int a, int b)
    {
    return a + b;
    }

    here is the basic format:
    [return-type] func_name([argtype] arg, [argtype] arg,...)
    {
    //other code
    return [return-type];
    }


    and btw using add_ints is this easy:

    int add_ints(int a, int b) //make sure this is declared or prototyped first
    {
    return a + b;
    }

    int main(void)
    {
    int p, q, r;

    cout << "enter 2 numbers" << endl;
    cin >> p;
    cin >> q;

    r = add_ints(p,q);

    cout << "sum is " << r;
    return 0;
    }

    a function can take in any number of args (well for your purposes, but there is a limit) but only return one item.

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    22
    Functions don't have a clue about anything except for whats in the function... The arguments are in case the function needs something outside of the funcion. i.e.

    if you have a function

    void outputname()

    that asks the user for their name, then outputs it, your function would look like...

    void outputname()
    {
    apstring name;

    cout<<"Enter your name : ";
    cin>>name;

    cout<<name<<endl;
    }

    if you wanted the function to output a name that is given to it from outside the function, it would look like...

    void outputname(apstring name) // to make a copy of name
    or
    void outputname(apstring &name) // to make a reference to name

    {
    cout<<name<<endl;
    }
    <^>( * ; * )<^>

  4. #4
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    Well, I'm not that much of a super newbie, its just functions and classes which I can't seem to get the hang of


    Anywayz, i've written programs that use and call functions without the use of arguments or returning. What is the purpose of arguments and returning?

  5. #5
    Unregistered
    Guest
    Arguments allow you to pass information to a function.
    For ex.

    int mult(int x, int y)
    {
    return x*y;
    }

    Now when I want to multiply two numbers i can just say this:

    mult(5, 6);

    What this will do is set x to equal 5 and y to equal 6 then it'll run the function.

    Returning allows you to get a value from a function.
    For ex.

    int a = mult(5, 6);

    What this will do is create an integer called "a" then run the function mult and set a to equal whatever mult returned which in this case would be 5*6.
    Hope this helps.

  6. #6
    Registered User
    Join Date
    Aug 2001
    Posts
    61
    Oops! Forgot to log in when I posted that.

  7. #7
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    oh ok. Thanks. I get it now

  8. #8
    Registered User
    Join Date
    Aug 2001
    Posts
    154
    Arguments can also be passed by value or by reference.
    Default is by value. This makes a copy of the argument, passes it to the called function, does whatever, and ends. The original value is not modified, and the passed value is typically destroyed after the called function is done.
    By reference passes a reference to the original value, which then can be modified. This can be good, or bad, depending on what you're doing.

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