473,386 Members | 1,815 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,386 software developers and data experts.

argument passing

What is meant by 'an argumentis passed by value'
Dec 1 '06 #1
7 1988
r035198x
13,262 8TB
What is meant by 'an argumentis passed by value'
Only a copy of the object is passed not the object itself

www.cs.rutgers.edu/~borgida/314/4param.pdf.gz
Dec 1 '06 #2
horace1
1,510 Expert 1GB
What is meant by 'an argumentis passed by value'
C passes parameters into a function using pass by value:

1 copies of the actual parameters are made in temporary variables and these are passed;
2 the called function can access the copies of the values of the actual parameters via the names of the corresponding formal parameters.

e.g. consider a function to raise a number to an exponent
Expand|Select|Wrap|Line Numbers
  1. float power(float number, int exponent)                    /* function header */
  2. {                                                                               
  3.     float result;                              /* local variable holds result */
  4.  
  5.     for (result = 1; exponent >= 1; exponent--)    /* loop exponent down to 1 */
  6.         result = result * number;                   /* number to the exponent */
  7.     return result;                                  /* return function result */
  8. }
  9.  
if it is called
Expand|Select|Wrap|Line Numbers
  1.      float x=4.0;
  2.      int exp = 3;
  3.      y = power(x, exp);                             /* call function */
  4.  
copies of x and exp are made and passesed into the function when the values are used in the calculation. Changes made (e.g. to exponent in the for() loop) only effect the copies.
Dec 1 '06 #3
r035198x
13,262 8TB
C passes parameters into a function using pass by value:

1 copies of the actual parameters are made in temporary variables and these are passed;
2 the called function can access the copies of the values of the actual parameters via the names of the corresponding formal parameters.

e.g. consider a function to raise a number to an exponent
Expand|Select|Wrap|Line Numbers
  1. float power(float number, int exponent) /* function header */
  2. float result; /* local variable holds result */
  3.  
  4. for (result = 1; exponent >= 1; exponent--) /* loop exponent down to 1 */
  5. result = result * number; /* number to the exponent */
  6. return result; /* return function result */
  7. }
  8.  
if it is called
Expand|Select|Wrap|Line Numbers
  1. float x=4.0;
  2. int exp = 3;
  3. y = power(x, exp); /* call function */
  4.  
copies of x and exp are made and passesed into the function when the values are used in the calculation. Changes made (e.g. to exponent in the for() loop) only effect the copies.
Are you trying to say that you cannot pass by reference in C?
Dec 1 '06 #4
horace1
1,510 Expert 1GB
Are you trying to say that you cannot pass by reference in C?
To pass an object into a function by reference the address of the object is passed as the actual parameter (using the address operator &) and the corresponding formal parameter is declared as a pointer. Inside the function the name of the formal parameter is prefixed with the indirection operator * which will then access the memory allocated to the original object.

e.g. to swap the values of two ints
Expand|Select|Wrap|Line Numbers
  1. void swap_int(int *p_int1, int *p_int2)                                 
  2. {                                                                               
  3.     int temporary = *p_int1;                     /* copy first variable into temporary */  
  4.     *p_int1 = *p_int2;                          /* copy second variable into first */  
  5.     *p_int2 = temporary;                     /* copy temporary variable into first */  
  6. }                                                                               
  7.  
called so
Expand|Select|Wrap|Line Numbers
  1.     int x=2, y=6;
  2.     swap_int(&x, &y);
  3.  
In C++ we can also use reference parameters
Dec 1 '06 #5
r035198x
13,262 8TB
To pass an object into a function by reference the address of the object is passed as the actual parameter (using the address operator &) and the corresponding formal parameter is declared as a pointer. Inside the function the name of the formal parameter is prefixed with the indirection operator * which will then access the memory allocated to the original object.

e.g. to swap the values of two ints
Expand|Select|Wrap|Line Numbers
  1. void swap_int(int *p_int1, int *p_int2) 
  2. int temporary = *p_int1; /* copy first variable into temporary */ 
  3. *p_int1 = *p_int2; /* copy second variable into first */ 
  4. *p_int2 = temporary; /* copy temporary variable into first */ 
  5.  
called so
Expand|Select|Wrap|Line Numbers
  1. int x=2, y=6;
  2. swap_int(&x, &y);
  3.  
In C++ we can also use reference parameters

Your first statement

C passes parameters into a function using pass by value:
Seemed misleading
Dec 1 '06 #6
horace1
1,510 Expert 1GB
Your first statement

C passes parameters into a function using pass by value:

Seemed misleading
In C the actual parameters passed into the function when it is called are 'passed by value', consider
Expand|Select|Wrap|Line Numbers
  1.     int x=2, y=6;
  2.     swap_int(&x, &y);
  3.  
the parameters &x and &y are 'passed by value', i.e.
1. temporary variables are created which contain addresses of x and y
2. inside swap() these are used to access the memory locations allocated to x and y to swap the values

hence we effectively achieve 'pass by reference' by using the values of the addresses which were 'passed by value'

consider
Expand|Select|Wrap|Line Numbers
  1. void swap_int(int *p_int1, int *p_int2)                                 
  2. {                                                                               
  3.     int *temporary = p_int1;                      
  4.     p_int1=p_int2;
  5.     p_int2 = temporary;                      
  6.     printf("swaped x %d y %d ???\n", *p_int1, *p_int2);
  7. }      
  8.  
we swap the pointers which appear to have swaped the values of x and y but this has no efecct on x and y in the main()
Dec 1 '06 #7
r035198x
13,262 8TB
In C the actual parameters passed into the function when it is called are 'passed by value', consider
Expand|Select|Wrap|Line Numbers
  1. int x=2, y=6;
  2. swap_int(&x, &y);
  3.  
the parameters &x and &y are 'passed by value', i.e.
1. temporary variables are created which contain addresses of x and y
2. inside swap() these are used to access the memory locations allocated to x and y to swap the values

hence we effectively achieve 'pass by reference' by using the values of the addresses which were 'passed by value'

consider
Expand|Select|Wrap|Line Numbers
  1. void swap_int(int *p_int1, int *p_int2) 
  2. int *temporary = p_int1; 
  3. p_int1=p_int2;
  4. p_int2 = temporary; 
  5. printf("swaped x %d y %d ???\n", *p_int1, *p_int2);
  6.  
we swap the pointers which appear to have swaped the values of x and y but this has no efecct on x and y in the main()
I suppose you've made your point
Dec 1 '06 #8

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

Similar topics

3
by: Frank Bechmann | last post by:
Eventually most of you will not learn much from this because it's just another event in the 'default argument value gotcha' series, but because it cost me some hours yesterday to spot this 'error'...
8
by: Alex Vinokur | last post by:
Various forms of argument passing ================================= C/C++ Performance Tests ======================= Using C/C++ Program Perfometer...
14
by: Bart Samwel | last post by:
Hi everybody, I would really like some help explaining this apparent discrepancy, because I really don't get it. Here is the snippet: void foo(int&); void foo(int const&); ...
17
by: Charles Sullivan | last post by:
The library function 'qsort' is declared thus: void qsort(void *base, size_t nmemb, size_t size, int(*compar)(const void *, const void *)); If in my code I write: int cmp_fcn(...); int...
2
by: Alexander Fedin | last post by:
Guys, I 've met some strange behaviour of the C# compiler. To reproduce it please try to compile the code below and you 'll be really amazed. The problem is that you can not use an interface...
1
by: User1014 | last post by:
Since you can pass a function to a ... erm...... function.... how to you use the result of a function as the argument for another function instead of passing the actual function to it. i.e. ...
3
by: matko | last post by:
This is a long one, so I'll summarize: 1. What are your opinions on raising an exception within the constructor of a (custom) exception? 2. How do -you- validate arguments in your own...
12
by: dave_dp | last post by:
Hi, I have just started learning C++ language.. I've read much even tried to understand the way standard says but still can't get the grasp of that concept. When parameters are passed/returned...
17
by: Summercool | last post by:
I wonder which language allows you to change an argument's value? like: foo(&a) { a = 3 } n = 1 print n
18
by: sanjay | last post by:
Hi, I have a doubt about passing values to a function accepting string. ====================================== #include <iostream> using namespace std; int main() {
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.