Probably a noobish question concerning functions()
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Probably a noobish question concerning functions()

  1. #1
    Join Date
    Jan 2010
    Posts
    1

    Resolved Probably a noobish question concerning functions()

    Hello! Im very new to C++ but find it exciting and fun (so far, heh). I've run into a couple of problems that I've solved with either tutorials or forum threads. But to this latest problem I can't find a straight answer anywhere - perhaps because its simplicity? :P

    Anyway, I need to know how to store two (or more) values in a function, and how I should manage to extract them from there, one by one. One value, no problems, but observe the very simple code example below;



    #include <cstdio>
    #include <cstdlib>
    #include <iostream>
    using namespace std;


    int function1(int value1, int value2);
    int main(int nNumberofArgs, char* pszArgs[])
    {

    int sword = 15;
    int armor = 10;

    sword = sword *function1( ,sword); //Im guessing that I should put
    armor = armor *function1(armor, ); //something in front of ,sword and something
    //behind armor, here


    cout <<"The sword value is: "<<sword<<endl;
    cout <<"The armor value is: "<<armor<<endl;

    system ("PAUSE");
    return 0;
    }

    int function1(int value1, int value2)

    {
    value1 = 15; //this value is to be multiplied with "armor"
    value2 = 25; //this value is to be multiplied with "sword"

    return value1;
    return value2;

    }


    What I want to do is to get value1 (=15) from function1() and multiply it with the "armor value", and the value2 (=25) from the same function multiplied with the "sword value". But I also need to be able to only extract one of the values from function1() in other parts of the code.

    To keep it short, the function() is to act as a database for a few variables.


    I'm probably doing it all wrong? Anyways, some help with the above suggestions or re-organisation of the code would be very helpful. Maybe functions isnt the way to go?

    Thanks!

  2. #2
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Probably a noobish question concerning functions()

    Well, the problem is a function can only return one value. It is possible though to return multiple values by passing parameters as references or through pointers.

    Now, as you said, function1 works as a database or rather repository of data. You want to extract different values from it with different calls. The point is that you need to specify the function what kind of values you need for it.

    You have 2 values/coefficients: one for armor and one for swords. You need to tell the function which of the two you need. So, you can do something like this:
    Code:
    enum Item 
    {
      Armor,
      Sword
    };
    
    int GetCoefficient(Item item)
    {
      int value = 0;
      switch(item)
      {
         case Armor: value = 25; break;
         case Sword: value = 15; break;
      }
    
      return value;
    }
    Then you can use it like this:
    Code:
    int sword = 15;
    int armor = 10;
    
    sword = sword *GetCoefficient(Sword);
    armor = armor *GetCoefficient(Armor);
    Of course, you can simplify all that by using a std::map. This is a class that associates a value to a key. The key (used to identify the value) is the item type, and the value is the coefficient.
    Code:
    #include <map>
    #include <cstdio>
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    enum Item 
    {
      Armor,
      Sword
    };
    
    int main(int nNumberofArgs, char* pszArgs[])
    {
      int sword = 15;
      int armor = 10;
    
      std::map<Item, int> coefficients;
      coefficients[Armor] = 25;
      coefficients[Sword] = 15;
    
      sword = sword * coefficients[Armor];
      armor = armor * coefficients[Sword];
      
      //behind armor, here
    
      cout <<"The sword value is: "<<sword<<endl;
      cout <<"The armor value is: "<<armor<<endl;
    
      system ("PAUSE");
      return 0;
    }
    Of course, you can make the coefficients variable global, or local to a function or member of a class (though that is probably too advance for you now).
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  3. #3
    Join Date
    Jan 2010
    Posts
    1

    Re: Probably a noobish question concerning functions()

    Thanks for the reply!

    So the error was that I thought a function could return multiple values because it can hold multiple arguments I.e. = function1(int value1, int value2). But I see now that this was not the case...

    I've been experimenting with std:maps a lot now and find it much easier to use. A nice "breakthrough", heh.

    Novice coder is thankful /bow

    /Daniel

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    Re: Probably a noobish question concerning functions()

    A function can certainly output multiple values----either by returning a class or struct type, or by using reference parameters.

    However, from your description, I think you should be designing a class, not a function. Functions in general should not be thought of as having persistent state.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center