Thread: Function Problems....

  1. #1
    The N00b That Owns You!
    Join Date
    Jul 2005
    Location
    Canada!
    Posts
    178

    Function Problems....

    i get a bunch of errors with my mult func. being at the top and i forgot how to make functions properly how do i fix this?

    Code:
    #include <iostream>
    #include <cstdlib>
    
    using namespace std;
     
    int a;
    int b;
    int c;
    
    void voided() {
    cout << "doesnt return anythin so im void" << endl;
    }
    
    void mult(a, b);
    
    int main() 
    {
        cout << "4 + 6 IS "<< mult(4, 6) <<"!" << endl;
        cout << "void function "<< voided <<"" << endl;
        system("PAUSE");
        return 0;
    }
    mult(a, b) {
    a + b = c; //a plus the value of be assigns the answer to c
    return c; // gives the answer ex if a is 2 and b is 3 it would return 5
    }
    New Function!!!!

    glAddIdol(C+noob);

    The feeling of rusty spoons against my salad fingers is almost ORGASMIC

  2. #2
    FOX
    Join Date
    May 2005
    Posts
    188
    Well for one you've declared mult to return nothing (void), but then proceed to return c. And you forgot to include the return type in the second declaration.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >void mult(a, b);
    If you want your function to return a value then declare it so:
    Code:
    int mult(int a, int b);
    And your assignment statement is backwards.

  4. #4
    The N00b That Owns You!
    Join Date
    Jul 2005
    Location
    Canada!
    Posts
    178
    ?????

    i tried to make it not bein void but says it must be declared nd that mult is not declared/defined
    New Function!!!!

    glAddIdol(C+noob);

    The feeling of rusty spoons against my salad fingers is almost ORGASMIC

  5. #5
    The N00b That Owns You!
    Join Date
    Jul 2005
    Location
    Canada!
    Posts
    178
    and i thought that was how u made a prototype function
    New Function!!!!

    glAddIdol(C+noob);

    The feeling of rusty spoons against my salad fingers is almost ORGASMIC

  6. #6
    FOX
    Join Date
    May 2005
    Posts
    188
    Quote Originally Posted by swoopy
    And your assignment statement is backwards.
    Haha I didn't even notice that one.

    Seriously, get yourself a good book. You seem to lack the understanding of even the simplest concepts like functions, so it's going to be really hard to learn anything from the seemingly random tutorials on the web.

  7. #7
    The N00b That Owns You!
    Join Date
    Jul 2005
    Location
    Canada!
    Posts
    178
    lol no! i used to know em really well then i started API and forgot it lol
    New Function!!!!

    glAddIdol(C+noob);

    The feeling of rusty spoons against my salad fingers is almost ORGASMIC

  8. #8
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Eh... That last statement makes no sense at all.

    Anyway, an example:
    Code:
    // Good...
    int foo(int x);
    
    int main { ... }
    
    int foo(int x)
    {
       return 1;
    }
    
    // Bad...
    int foo(int x);
    
    int main { ... }
    
    int foo(/* nothing here */ x)
    {
       return 1;
    }
    
    // Also bad...
    int foo( );
    
    int main { ... }
    
    /* nothing here */ foo( )
    {
       return 1;
    }
    The signature of the function for both declaration and definition must match exactly*. And, C++ is strongly typed; all variables must have a type.

    * This excludes the parameter names (and only the names). So, the following are fine:
    Code:
    void foo(int x);
    void foo(int fred)
    {
       cout << fred; // **not** cout << x;
    }
    
    void bar(int);
    void bar(int y)
    {
       cout << y;
    }
    Cheers
    Last edited by Zach L.; 07-11-2005 at 03:53 PM.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  9. #9
    Weak. dra's Avatar
    Join Date
    Apr 2005
    Posts
    166
    you didn't assign a type for your function such as int mult() .for your function, you don't need to declare the variables first.

    Code:
    //you can write it like this
    
    double mult( double a, double b ){
    
    return a*b;
    
    }
    
    
    //then in your main function you can simply write 
    
    int main(){
    
    cout << "4 x 5 IS " << mult(4,5);
    
    return 0;
    
    }
    in my example, all you need to do is pass the values into the function, and instead of creating a variable to hold the product of 4 and 5, you simply return it.

  10. #10
    The N00b That Owns You!
    Join Date
    Jul 2005
    Location
    Canada!
    Posts
    178
    i remembered while doin my classes lol ty all
    New Function!!!!

    glAddIdol(C+noob);

    The feeling of rusty spoons against my salad fingers is almost ORGASMIC

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  2. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. Problems with str.replace function
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 11-07-2001, 03:35 AM