Void function returns value of 1000

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Deanm007
    New Member
    • Apr 2010
    • 37

    Void function returns value of 1000

    It says all over that void f() means that it returns no value. But this function below does return a value to the main function which is 1000. Am I correct?

    Basically void is taking place of a return type. Maybe it returns a value but no return type(ie. int, char, bool)?


    Code:
    void SetTo1000(int iaArray[], int iIndex) 
    {
    	iaArray[iIndex] = 1000;
    }
    
    int main() 
    {
    	int iMyArray[5] = {-3,2,0,-1,5};
    	SetTo1000(iMyArray,3);
    	for (int iIndex=0; iIndex<5; iIndex++)
    	{
    		cout << iMyArray[iIndex] << endl;
    	}
    	return 0;
    Last edited by Deanm007; Apr 20 '10, 04:27 PM. Reason: Figured out how to wrap code tags..
  • Dheeraj Joshi
    Recognized Expert Top Contributor
    • Jul 2009
    • 1129

    #2
    Your code(function) do not return any value to main function.

    Regards
    Dheeraj Joshi

    Comment

    • Deanm007
      New Member
      • Apr 2010
      • 37

      #3
      Hi Dheeraj

      i see 1000 as part of the output. That 1000 gets returned from the function to main, no?

      Comment

      • sridhard2406
        New Member
        • Dec 2007
        • 52

        #4
        Hi Deanm007,
        Your funtion will store the 1000 into iaArray[iIndex]. please find the below funtion for returning the int.



        int funtion(int a)
        {
        a++;
        return a ;
        }

        Comment

        • Dheeraj Joshi
          Recognized Expert Top Contributor
          • Jul 2009
          • 1129

          #5
          Please read pass by reference and pass by value to understand why you get the value 1000 in your output.

          Regards
          Dheeraj Joshi

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            No 1000 is not returned to main in the strictest sense of the word.

            The return mechanism for any function is quite different to writing data to a memory location.

            Your function does not return a value it returns void and so it does not return a value to main. It writes a value to a memory location that is accessible by main. The net result is that the value stored by the function is available in main but it has not been returned to main, it has been communicated to main by an alternate mechanism.

            The actual return process often involves data being transferred on the stack as part of the stack unwinding that happens when a function returns.

            Comment

            • Dheeraj Joshi
              Recognized Expert Top Contributor
              • Jul 2009
              • 1129

              #7
              Nice answer banfa, In fact i wanted to ans in same way, could not articulate it. Good explanation.

              Regards
              Dheeraj Joshi

              Comment

              • Deanm007
                New Member
                • Apr 2010
                • 37

                #8
                Originally posted by Banfa
                No 1000 is not returned to main in the strictest sense of the word.

                The return mechanism for any function is quite different to writing data to a memory location.

                Your function does not return a value it returns void and so it does not return a value to main. It writes a value to a memory location that is accessible by main. The net result is that the value stored by the function is available in main but it has not been returned to main, it has been communicated to main by an alternate mechanism.

                The actual return process often involves data being transferred on the stack as part of the stack unwinding that happens when a function returns.
                Thanks a lot. that was very clear.

                So you wouldn't need to pass in by reference to store a function's result in the memory, would you? Because in my code, the function would need ampersands like this:

                Code:
                # void SetTo1000(int& iaArray[], int& iIndex) 
                # {
                #     iaArray[iIndex] = 1000;
                # }

                Comment

                • Deanm007
                  New Member
                  • Apr 2010
                  • 37

                  #9
                  sridhar, dheeraj, thanks for responding

                  Comment

                  • Banfa
                    Recognized Expert Expert
                    • Feb 2006
                    • 9067

                    #10
                    No you would need to pass a pointer to an array by reference because it is already a pointer, you would be unnecessarily dereferencing twice assuming you where just changing the contents of the array. Same for the size.

                    This should work just as well

                    Code:
                    void SetTo1000(int iaArray[], int iIndex) 
                    {
                        iaArray[iIndex] = 1000;
                    }
                    Note aiArray is a pointer to int (int *) with this syntax.

                    Comment

                    • Deanm007
                      New Member
                      • Apr 2010
                      • 37

                      #11
                      Originally posted by Banfa
                      No you would need to pass a pointer to an array by reference because it is already a pointer, you would be unnecessarily dereferencing twice assuming you where just changing the contents of the array. Same for the size.

                      This should work just as well

                      Code:
                      void SetTo1000(int iaArray[], int iIndex) 
                      {
                          iaArray[iIndex] = 1000;
                      }
                      Note aiArray is a pointer to int (int *) with this syntax.
                      I got it, thank you.

                      Comment

                      Working...