Function definitions

This is a discussion on Function definitions within the C++ Programming forums, part of the General Programming Boards category; Hey again Thanks for all the help on my project, the teacher hasn't grade it yet but hopefully I did ...

  1. #1
    Registered User lifeis2evil's Avatar
    Join Date
    Oct 2007
    Posts
    76

    Lightbulb Function definitions

    Hey again Thanks for all the help on my project, the teacher hasn't grade it yet but hopefully I did everything correct and I was able to help one of my class mates with there code and got it to work without any code i had

    Now she wants us to do a new project:
    Write a program that ask the user for a number. Then displays the multiplication table for that number 0-10.

    use the following function prototype and code the function definitions

    int getNumber( );
    void displayTable(int);

    use a loop to calculate and display the table


    I have it working but without the void displayTable(int); how could I make it including that?
    Code:
    #include <iostream>
    
    using namespace std;
    //Prototypes
    int getNumber( );
    //void displayTable(int);
    
    
    int main()
    {
        int tableAnswer;
        int firstNumber; 
     
        firstNumber=getNumber();
        
       int Number;
       for (Number=0; Number<=10; Number++)
       {
        tableAnswer=firstNumber*Number;
        cout<<firstNumber<<" x "<<Number<< " = "<<tableAnswer<<'\n';
    }
         
        system ("pause");
    	return 0;
    }
    
    int getNumber()
    {
    int Number;
          cout<<"Please enter a number"<<endl;
          cin>>Number;
         return Number;
    }
    
    /*void displayTable(int)
    {
     int Number;
     for (Number=0; Number<=10; Number++)
     cout<<Number;
     }
     */

  2. #2
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    All of this goes inside your displayTable() function definition:
    Code:
     int Number;
       for (Number=0; Number<=10; Number++)
       {
        tableAnswer=firstNumber*Number;
        cout<<firstNumber<<" x "<<Number<< " = "<<tableAnswer<<'\n';
    Then, you call the displayTable() function from main, the same way you called getNumber() from main.

    Your main function should just call those two functions, and then return... It should hand-off all of the "work" to the other functions.

    And, you are using the variable "Number" for two different things, which you can sometimes get away with, but it's generally bad practice.... It's OK to use "counting" variables names (usually x, y, i, j) in several different places like that, but you shouldn't use the same name for your user-input and for a counting variable. It would be perfectly acceptable to use the name "firstNumber" inside and outside the getNumber() function.

    Of course, you'll have to pass the value of firstNumber into the displayTable() function, and there is no harm in using that same name again inside displayTable().


    ----------------------------------------------------------------
    P.S.
    Your function definiton needs some work. The function prototype and the first line of the function definition should not be exactly the same... they have different requirements. The prototype just needs the variable types (i.e. int) for the variables passed-in. The function definition also needs variable names (names to be used inside the function) for the passed-in values.

    I suggest you start with a simple version of displayTable() that just displays the value of firstNumber. Then, add the 1st calculation and get that working. Then, add the loop. ...It's much easier if you "develop" your program one step at a time.

    You might want to review the differences between a function prototype, the first line of a function definition, and a function call... It can get confusing because they all look similar.

    In a more complicated program, you might have several function calls to the same function, but there will be only one function prototype and one function definition. Each of those calls can pass-in different variable names, as long as the variable types are correct. (The name isn't getting passed-in, only the value assigned to the name.) And, each of those calls might assign the return value to a different variable name, as long as the type is correct. (Again, the function returns a value.) ....Confused yet?

    ...and I was able to help one of my class mates with there code and got it to work...
    It's a very good idea to get into a study group. It's easy to "get stuck" for hours on a simple concept, or on a simple bug, and somebody in the group can usually help you through it. Just make sure you understand what "the group" is doing, and do your own work... or at least use your own variable names.
    Last edited by DougDbug; 11-02-2007 at 05:31 PM.

  3. #3
    Registered User lifeis2evil's Avatar
    Join Date
    Oct 2007
    Posts
    76
    Hehe yes been confused >_<
    mmm do you mean doing this stuff?

    Code:
    #include <iostream>
    
    using namespace std;
    //Prototypes
    int getNumber( );
    void displayTable(int);
    
    
    int main()
    {
        int tableAnswer;
        int firstNumber; 
        
        tableAnswer=displayTable();
        firstNumber=getNumber();
        
       
         
        system ("pause");
    	return 0;
    }
    //function definitions
    int getNumber(numberInput)
    {
    int numberInput;
          cout<<"Please enter a number"<<endl;
          cin>>numberInput;
         return numberInout;
    }
    
    void displayTable(int firstNumber)
    {
      int Number;
       for (Number=0; Number<=10; Number++)
       {
        tableAnswer=firstNumber*Number;
        cout<<firstNumber<<" x "<<Number<< " = "<<tableAnswer<<'\n';
     }
    It tells me i am doing something wrong with this part
    Code:
    //Prototypes
    int getNumber( );
    void displayTable(int);
    
    
    int main()
    {
        int tableAnswer;
        int firstNumber; 
        
        tableAnswer=displayTable();
        firstNumber=getNumber();
    Yeah I was sorta in a group thing with my teacher today but this is due on Sunday and I have classes tomorrow so it doesn't leave me much time to do a study group lol last project I was really stuck took me about 4 days and most of those days were sleepless >_<



    Thank you for explaining everything it aids to my learning lol even though I have o learn backwards cause I am a visual leaner so I look at the code first and then try to figure out how it works with what you wrote hehe Thanks a lot

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    OF course it's wrong!
    Remember your previous lesson: the function takes one argument, but you pass NONE!
    About declarations and definitions... I recommend that you put variable names in declarations AND definitions because usually compilers use your declarations for intellisense (eg to show you what parameters a function takes) and it's very vague sometimes hard to know what type of arguments it takes if there's just two "int." Instead, put names on them in the declaration instead and you'll find it easier. It should work in both C and C++.

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    42
    Also, first you should get the number, and then pass it to function that will display table. At the moment you're doing it in wrong order.

  6. #6
    Registered User lifeis2evil's Avatar
    Join Date
    Oct 2007
    Posts
    76
    Wait which part is wrong??

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    Fix your function call and use the debugger to find out. You can do that, can't you?

  8. #8
    Registered User lifeis2evil's Avatar
    Join Date
    Oct 2007
    Posts
    76
    no I've never really done function calls before. I was only able to make it work the 1st way I did it but I wanted to use the other function too.

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    Then how come you are using functions if you've never learned how to use them?

  10. #10
    Registered User lifeis2evil's Avatar
    Join Date
    Oct 2007
    Posts
    76
    because its a project

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    Regardless. You shouldn't use what you don't know.
    Take some time to read a tutorial.
    Last edited by Elysia; 11-03-2007 at 03:43 PM.

  12. #12
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129

  13. #13
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    Adding to Elysia's statement:
    You can also declare default parameters for functions, so that you do not have to pass arguments.
    such as
    Code:
    int Function(int = 10);
    
    int Function(int x)
    {
       return x;
    }
    if no argument is passed then it will return 10 as the default

    maybe that will help clarify
    Last edited by Raigne; 11-03-2007 at 03:46 PM.

  14. #14
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    Perhaps that's overkill since lifeis2evil is still new to functions.

  15. #15
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    its not overkill. it is a very useful part of the language and it is not like it's that hard.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 05-24-2009, 02:42 AM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  3. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  4. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  5. help writing function definitions
    By jlmac2001 in forum C++ Programming
    Replies: 2
    Last Post: 04-10-2003, 09:44 PM

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