C++ void methods
Results 1 to 6 of 6

Thread: C++ void methods

  1. #1
    Senior Member
    Join Date
    Oct 2005
    Posts
    106

    Talking C++ void methods

    I was discussing with a friend about making a calculator for my pain-in-the-ass math courses, and he recommended that: 1. If I was going to make a calculator, I should make a damn good one; and 2. I should use C++.

    Being as bored as I am, I agreed. He quickly recommended that I should use void methods. Then he had to run off.

    And left me completely befuddled on implementing C++ voids.

    So should it be
    Code:
    #include <iostream>
    
    void ????
    {
         ????
    };
    
    int main()
    { 
         //program stuff
    }
    Or am I way off? I have no clue; I tried googling, and come upon several fascinating sites comparing C to C++, but other than that I came empty handed. And yes, I have read the manual.

  2. #2
    Elite Hacker
    Join Date
    Mar 2003
    Posts
    1,407
    A void method is a method that generally doesn't return anything, although I think it can. A good example of a void method is one in which you just want to print a message.

    Code:
    void printmessage(const char * message){
       printf("%s\n", message);
    }
    Of course that's C, but it still works in C++. Printf itself is a void function, but I think it returns something. I'm not sure why your friend told you to implement it that way. To me a calculator would call functions which return the result of the calculation. If you did it as void functions then you would have a pass a pointer to get the result of the calculation. Also, I don't think you need that semicolon after your void ???? { } in your example, and it may even be an error. Good luck on your calculator.

  3. #3
    Jaded Network Admin nebulus200's Avatar
    Join Date
    Jun 2002
    Posts
    1,356
    Generically in programming, regardless of the language, the language will provide a way to 'call' a method outside of the main body of code and most languages will also allow you to return a value, if you chose, from that external method (external in the sense that it is not part of the main body). Generally speaking, when you return something from the external method, its known as a function, versus not returning something, which is a procedure.

    Whether or not something is returned by the function depends on its purpose. It can be as simple as returning the success or failure of the function to as complicated as returning a pointer (memory address) of something (could be as simple as a pointer to the beginning of a string or as complicated as a pointer to an object), but it all goes back to the purpose of the external method. H3r3tic provided a common use of procedures (output), but the key there is it doesn't terribly matter whether or not something is returned or not.

    In C/C++, you denote what is returned by typecasting the external method with the typecast reflective of the type of information returned by the function, with void being applicable if nothing is returned.

    If you notice from your own example, main() is typecast is int...why is that ? Most operating systems will track values returned by the program after it exits (mostly used for error tracking), so for example, in Unix if a program exits with a 0 the program ran successfully (0 errors) and if abnormally it returns a non-zero number...the int typecast facilitates this exchange between the program and the OS...

    Quicky example (psuedo-C, ie it looks like C but may generate syntax errors since I hardly use it anymore...)
    Alright, the program is rather pointless since the values are static (non-changing), but it does illustrate a few different types and reasons for returns...The first function just adds two numbers and returns the sum (function used to compute a value and return it)...the second function checks if the addition is successful (again rather pointless, but illustrates a function being used to check the logical success of something else, but still typecast as int), the third function just prints out whatever it is told (probably bad form, but I am in a hurry) and it illustrates a return value not meaning anything, not being necessary, so it returns nothing (void). Lastly I'll point out that the program itself will return whether or not the addition was successful or not to the OS, but note a negation of the value is necessary since a successful program (1) must return 0.

    So what's the point? Everything must be typecast in C/C++ and void is just one of the types that means 'nothing' or 'no value'. Very important distinction to understand, especially if you migrate into more advanced C++ topics like templates...

    Code:
    int add(int a, int b) {
    // return sum of a and b
        return (a+b);
    }
    
    int success(int a, int b, int c) {
    // return 1 if c > a+b, 0 if not
        return (c > (a+b) );
    }
    
    void printIt(char *c)  {
    // print out string c 
          printf("%s\n", c); return;
    }
    
    int main(int argc, char **argv)
    {
    int a = 2;
    int b = 5;
    int c =0;
            c = add(a, b);
           if(success(a, b, c) ) {
                printIt("Add function successfull\n");
           } else {
               printIt("Add function failed\n");
         }
         return (! success(a, b, c) );
    }
    There is only one constant, one universal, it is the only real truth: causality. Action. Reaction. Cause and effect...There is no escape from it, we are forever slaves to it. Our only hope, our only peace is to understand it, to understand the 'why'. 'Why' is what separates us from them, you from me. 'Why' is the only real social power, without it you are powerless.

    (Merovingian - Matrix Reloaded)

  4. #4
    Elite Hacker
    Join Date
    Mar 2003
    Posts
    1,407
    Your program syntactically looks fine, I think your logic if flawed though. c > (a+b) will be true (1) if c is greater than (a+b). c will always be equal to (a+b) as long as c is the result of the add function passing in a and b. So the success function will always return 0. You could change c > (a+b) to c == (a+b) or c >= (a+b) and it will make a little bit more sense . I would have let this go, but the person we're trying to help might be testing this program as I type this and saying to himself "wtflip". Peace.

  5. #5
    Jaded Network Admin nebulus200's Avatar
    Join Date
    Jun 2002
    Posts
    1,356
    Lol, yup, that's what I get for trying to quickly pound something out, was too focused on trying to get a logical function in there and not whether or not it made sense Oh well, more suprised I didn't make a syntax error, that's probably the first time I have done something in C in about 8 years ... not counting fixing the various POC codes for various exploits every now and then

    EDIT: Was gonna spread a little green for the eagle eye, but:

    You must spread your AntiPoints around before giving it to h3r3tic again.

    There is only one constant, one universal, it is the only real truth: causality. Action. Reaction. Cause and effect...There is no escape from it, we are forever slaves to it. Our only hope, our only peace is to understand it, to understand the 'why'. 'Why' is what separates us from them, you from me. 'Why' is the only real social power, without it you are powerless.

    (Merovingian - Matrix Reloaded)

  6. #6
    Elite Hacker
    Join Date
    Mar 2003
    Posts
    1,407
    A little off topic. I think one of my teachers is going to make me hate C++. I'm in a programming languages course this semester, and we've been doing some ML, which none of us had done until that class. Now we're translating really weird ML code into C++, and most of us have also never done C++. I have my doubts about finishing the assignment. I just find it hard to believe that I will be able to translate code that I don't understand in a language that I barely understand, into code in a language similar to a language I understand, but using aspects of the language that I don't understand and have never used. Either I suck or my professor is crazy. Either way I just needed a place to say that. Now, back to the grind .

Posting Permissions

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