void function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Majeti pradee
    New Member
    • Oct 2006
    • 1

    #1

    void function

    is it mandatory to use void function?and why should we use it
  • apusateri
    New Member
    • Oct 2006
    • 27

    #2
    Not sure exactly what you're referring to, the question is a little vague, but if you are referencing something like the following:

    Code:
    void someFunction(void)
    The first void is the return type of the function, i.e. what will be returned when the function is called. Personally, I don't use void too often, I prefer to use int for error-checking. Void simply indicates that the function does not return a value.

    There are cases, however, when you do need to return a value, such as the following:

    Code:
    int addNumbers(int i, int j, int k)
    {
         return (i + j + k);
    }
    Useless function, yes, but hopefully it shows why you would use return types on your functions. In this case, when you used addNumbers(1, 2, 3) it would return 6, which could be used for later calculations.

    Hope this helps.

    Comment

    • iknc4miles
      New Member
      • Oct 2006
      • 32

      #3
      No. In fact, for a main function, it is not recommended because it uses excess memory.

      void is a variable type just like int, char, double, float, and bool. However, unlike the other variables, void is usually used to hold an unknown (or unwanted) value.

      Rule of thumb for using void: If your function returns a value, you must not use void.


      Acceptable:
      int main()
      {
      return 1; // use int because returning an integer
      }

      void main()
      {
      return; // use void because not returning a value
      }

      void add( int a, int b) {return;};
      add(a, b) // this add does not return anything

      Unacceptable:
      void main()
      {
      return 1; // Will generate "Looking for variable type void not found" error.
      }

      void add( int a, int b) {return;};
      c = add( a, b) // Will generate "Looking for variable type void not found" error.

      - Miles

      Comment

      • vninja
        New Member
        • Oct 2006
        • 40

        #4
        Hey,
        here's my 2 cents.
        nothing is manditory and yet everything is. your question is a little vague but if we extend it to a more complex functions void functions become a little more practicle to use.
        ex
        simple:
        void intro(void)
        {
        cout<< "Welcome to my program";
        }
        //this will make int main() func easier to read and fix if you have a program
        //not doing what it should

        more complex
        void distance(string /*in*/ num, double /*out*/ &thrw)
        {
        cout << "Please enter the distance for throw # "
        << num << ": ";
        cin >> thrw;
        cin.ignore(100, '\n');
        }
        //This void function is great for a get value and it could be rewritten with an int
        // function that would return the var thrw. but if we need more variables to change
        // then we would need more returned values

        //also the ampersand(&) tells the program to replace whatever was in value thrw
        //with whatever is put in (kinda like a value returning func's return command)

        Comment

        • vninja
          New Member
          • Oct 2006
          • 40

          #5
          and if we go furthur
          void distance(string /*in*/ num, double /*out*/ &thrwx, double /*out*/ &thrwy)
          {
          cout << "Please enter the x distance for throw # "
          << num << ": ";
          cin >> thrw;
          cout << "Please enter the y distance for throw # "
          << num << ": ";
          cin >> thrwy;
          cin.ignore(100, '\n');
          }

          //this way you can get two values from one func than 1 value that you could get
          //from a value returning func()

          now we could also change the void to an int like so:
          int distance(string /*in*/ num, double /*out*/ &thrwx)
          {
          cout << "Please enter the x distance for throw # "
          << num << ": ";
          cin >> thrw;
          cout << "Please enter the y distance for throw # "
          << num << ": ";
          cin >> thrwy;
          cin.ignore(100, '\n');
          return thrwy;
          }
          //and this would get the same effect as the code before
          the only difference would be in the main where for the 1st (void) func the call would be
          distance(num, thrwx, thrwy);
          and 2nd(value returning func) call would be
          thrwy = distance(num, thrwx);


          so what it all boils down to is preference
          if you prefer to use value returning func its ok
          if you prefer to use void func its ok too

          hope this helps you n srry for splittin it into 2 posts
          V

          Comment

          • nshrestha
            New Member
            • Oct 2006
            • 2

            #6
            In this execrise, you create a simple payroll program using a main() fuction and four void function.
            A. Fingure 10.25 shows the partially completed IPO charts for the payroll program.complet e the Input and out put colums of the IPO charts for the four void functions named getInput( ),CalcFedTaxes( ),CalNetpay( ),and displayInfo( ).The FWT(Federal Withholding Tax) rate is 20% of the weekly salary, and the
            FICA (Federal Insurance contributions Act) rate is 8% of the weekly salary.

            figure 10-25

            Input processing output

            name processing item:none name
            weekly salary 1.getlnput(name ,weeklysalary) FWI
            FWT rate (.2) 2.CalcFedTaxes( weekly salary) FICA
            FICA rate (.08) FWT rate,FICA rate,FWT,FICA) weekly net pay
            3.calcNetpay(we elky salary,FWT,FICA ,weekly net pay)
            4.displayinfo(n ame,FWT,FICA,we ekly net pay)


            getInput ( ) fuction

            Input processing output
            processing item: none
            Algorithm:
            1.enter the name and weekly salary

            calcFedTaxes ( )fuction

            Input Processing output
            Processing item:none
            Algorithm:
            1.calculate the FWT by multiplying the weekly salary by the FWT rate
            2. calculate the FICA by multiplying the weekly salary by the FICA rate


            calcNetpay( )function

            Input processing output
            Processing item:none
            Algorithm:
            1.calculate the weekly net pay by subtracting the FWT and FICA from
            the weelky salary


            displayInfo( )function

            input Processing output
            Processing item:none
            Algorithm:
            1.display the name,FWT,FICA and weekly net pay

            B.Display the taxes and net pay with two decimal places.

            Comment

            Working...