473,378 Members | 1,620 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Array: Call by value and Call by reference

m best
7
What is the difference between Array of call by value and call by reference?
Any simple example?
Jan 15 '12 #1
7 4860
C CSR
144 100+
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:

Expand|Select|Wrap|Line Numbers
  1. 'ByRef set1
  2. Sub firstval()
  3.     x = 3
  4.     Call secondsub(x)
  5.     [ x is now 9 here after return from secondsub]
  6. End Sub
  7.  
  8. Sub secondsub(x)
  9.     x = x + 6
  10. End Sub
  11.  
  12. '-----------------------
  13. 'ByVal set 2
  14. Sub firstval2()
  15.     x = 3
  16.     Call secondsub2(x)
  17.     [ x is still 3 here after return from secondsub2]
  18. End Sub
  19.  
  20. Sub secondsub2(ByVal x)
  21. x = x + 6
  22. End Sub
  23.  
Note that I didn't use a function or a return =.
Jan 15 '12 #2
C CSR
144 100+
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).
Jan 15 '12 #3
donbock
2,426 Expert 2GB
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:
Expand|Select|Wrap|Line Numbers
  1. struct s { int array[10] };
  2. void func(struct s parameter);
  3. ...
  4. struct s value;
  5. ...
  6. 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:
Expand|Select|Wrap|Line Numbers
  1. int array[10];
  2. void func(const int *parameter);
  3. ...
  4. func(array);
Jan 17 '12 #4
m best
7
Thanks for reply............................................. ...............................
Jan 17 '12 #5
kiara
2
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",&n1,&n2);
sum=add(n1,n2);
printf("%d",sum);
}
int add(int x,int y)
{
int sum;
sum=x+y;
return sum;
}
Jan 22 '12 #6
kiara
2
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",&n1,&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.
Jan 22 '12 #7
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
Jan 23 '12 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Bell | last post by:
Is there a way to copy an array items' value (like clone) to another array? Currently the array.clone() and System.arraycopy() are only copy the iten reference. Jun
3
by: sam pal | last post by:
I have the following programs and please clear my doubts. Passing the value by reference is same as pointer by reference. Is this true? What is the difference between display and display2? How...
41
by: Berk Birand | last post by:
Hi, I am just learning about the array/pointer duality in C/C++. I couldn't help wondering, is there a way to pass an array by value? It seems like the only way to do is to pass it by...
4
by: wg | last post by:
I am attempting to create an array of value types but seem to be haveing a problem referencing it. I have created a class Public Class Tags Private _Name As String Private _Address As String ...
12
by: huangshan | last post by:
hi all In what condition i need( or mast) a (templates)function return value by reference? can you give me a example thanks
14
by: Abhi | last post by:
I wrote a function foo(int arr) and its prototype is declared as foo(int arr); I modify the values of the array in the function and the values are getting modified in the main array which is...
68
by: Jim Langston | last post by:
I remember there was a thread a while back that was talking about using the return value of a function as a reference where I had thought the reference would become invalidated because it was a...
13
by: asm23 | last post by:
Hi,I need some help to clarify the warning "initial value of reference to non-const must be an lvalue". I'm searching in this groups to find someone has the same situation like me. I found in...
1
by: Drummin | last post by:
How do I get user id from radio button name in an array. The value is a variable. So this section repeats in an array and when submitting the ID and value is passed for each user in the array. echo...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.