Array: Call by value and Call by reference

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • m best
    New Member
    • Jan 2012
    • 7

    Array: Call by value and Call by reference

    What is the difference between Array of call by value and call by reference?
    Any simple example?
    Last edited by Niheel; Jan 17 '12, 04:10 AM.
  • C CSR
    New Member
    • Jan 2012
    • 144

    #2
    One (by reference) Changes the value back in the "sub of orig" and the other (by value) only changes the value within the scope of the second subprocedure.
    EXAMPLE:

    Code:
    'ByRef set1
    Sub firstval()
    	x = 3
    	Call secondsub(x)
    	[ x is now 9 here after return from secondsub]
    End Sub
    
    Sub secondsub(x)
    	x = x + 6
    End Sub
    
    '-----------------------
    'ByVal set 2
    Sub firstval2()
    	x = 3
    	Call secondsub2(x)
    	[ x is still 3 here after return from secondsub2]
    End Sub
    
    Sub secondsub2(ByVal x)
    x = x + 6
    End Sub
    Note that I didn't use a function or a return =.

    Comment

    • C CSR
      New Member
      • Jan 2012
      • 144

      #3
      Excuse me. I missed the "Array" part of the question. Note that passing an array "by value" does not protect the data from change in the original subprocedure (something to do with the fact that the array itself is already a reference to the data it points to).

      Comment

      • donbock
        Recognized Expert Top Contributor
        • Mar 2008
        • 2427

        #4
        C CSR provided a concise definition of the terms pass by value and pass by reference. Do you have any questions about these definitions?

        If not, then the only issue remaining is the mechanics of passing an array by value versus passing it by reference. Passing an array by reference is no problem. Assuming you are asking about C, I can only think of one way to pass an array by value:
        Code:
        struct s { int array[10] };
        void func(struct s parameter);
        ...
        struct s value;
        ...
        func(value);
        I advise you not to do this. Passing and returning structures/unions by value is inefficient and some compiler implementations accomplish this in nonreentrant ways.

        Much better is to use the const keyword to simulate call-by-value:
        Code:
        int array[10];
        void func(const int *parameter);
        ...
        func(array);

        Comment

        • m best
          New Member
          • Jan 2012
          • 7

          #5
          Thanks for reply.......... ............... ............... ............... ............... ......

          Comment

          • kiara
            New Member
            • Jan 2012
            • 2

            #6
            example for call by value:(with return and argument type)
            #include<stdio. h>
            main()
            {
            int add(int,int);
            int n1,n2,sum;
            printf("enter two no's");
            scanf("%d%d",&n 1,&n2);
            sum=add(n1,n2);
            printf("%d",sum );
            }
            int add(int x,int y)
            {
            int sum;
            sum=x+y;
            return sum;
            }

            Comment

            • kiara
              New Member
              • Jan 2012
              • 2

              #7
              example for call by reference:
              #include<stdio. h>
              main()
              {
              int add(int*,int*);
              int sum,n1,n2;
              printf("enter the no.s");
              scanf("%d%d",&n 1,&n2);
              sum=add(&n1,&n2 );
              printf("%d",sum );
              }
              int add(int *x,int *y)
              {
              int sum;
              sum=*x+*y;
              return sum;
              }


              the difference: in call by value we r passing the values to a new variable/same variable but it has different storage location.so it takes more memory to store.
              but in the later one locations r copied.

              Comment

              • hemant goyal
                New Member
                • Jan 2012
                • 1

                #8
                the difference: in call by value we r passing the values to a new variable/same variable but it has different storage location.so it takes more memory to store.
                but in the later one locations r copied

                Comment

                Working...