473,725 Members | 1,956 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

what is pass by ref function?

arunmib
104 New Member
hi all,
I just got this freaky kind of doubt....I have the following piece of code,

Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.      int Val= 10, *ptr;
  4.      ptr = &Val;
  5.  
  6.     TestFn(&Val);
  7.  
  8.     TestPtr(ptr);
  9.  
  10.     return 0;
  11. }
  12.  
  13. void TestFn(int *i)
  14. {
  15.     ....does some operations
  16. }
  17.  
  18. void TestPtr(int *j)
  19. {
  20.    ....does some other operations
  21. }
Technically speaking for the first function (TestFn) I am passing the address of the variable Val. So I can call this function as pass by reference function.

But when it comes to the second function (TestPtr), what i am doing is the following, the address of the Val variable is assigned to Ptr. So, for second function we are just sending the address directly as input parameter. You people get my point right, the function's input parameter is a pointer and the input is also a pointer (with the address of another variable). So this just an equivalent to pass by value (not with values, but with addresses in pointer variables)

Can I call this as pass by value then ?

Am I clear or do I need to be more descriptive....
May 9 '07 #1
10 2223
JosAH
11,448 Recognized Expert MVP
C uses pass by value only; even when you pass a pointer as a parameter, the
value of that pointer is passed to the function. C++ also has a pass by reference
mechanism but that's not what you're using in your example code.

kind regards,

Jos
May 9 '07 #2
arunmib
104 New Member
Thanks for the reply Jos.

So you mean here that if I use the same code in C++, then it can be used as pass by reference also. If yes, can you just brief me about the mechanism how it is handled or some location where I can look for answers....
May 9 '07 #3
pradeep kaltari
102 Recognized Expert New Member
Thanks for the reply Jos.

So you mean here that if I use the same code in C++, then it can be used as pass by reference also. If yes, can you just brief me about the mechanism how it is handled or some location where I can look for answers....
Hi,
In C, pointers also occupy some place in the memory to store the address.
In C++ there is something called reference type. This is just like giving an alias/ new name to the variable.
Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3. ...
  4. int i=5;
  5. ...
  6. int &j=i;
  7. ...
  8. }
  9.  
Here j does is not allocated with memory. Instead it refers to the same location of i. Consider the following pass-by-reference function call in C++:

Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3. ..
  4. int i=5;
  5. ...
  6. call function(i);
  7. ...
  8. }
  9. void function(int &j)
  10. {
  11. ...
  12. }
  13.  
I hope this was helpful.

Regards,
Pradeep
May 9 '07 #4
AdrianH
1,251 Recognized Expert Top Contributor
Pradeep response is extremely close. The only thing that wasn’t mentioned is that a reference can take up memory. However, the compiler's optimiser may realise it is not necessary and not bother.


Adrian
May 9 '07 #5
pradeep kaltari
102 Recognized Expert New Member
Pradeep response is extremely close. The only thing that wasn’t mentioned is that a reference can take up memory. However, the compiler's optimiser may realise it is not necessary and not bother.


Adrian
Hi,
I read it somewhere that its unspecified whether references require memory or not. Could anybody throw some light on this?

Thanks,
Pradeep
May 10 '07 #6
AdrianH
1,251 Recognized Expert Top Contributor
Hi,
I read it somewhere that its unspecified whether references require memory or not. Could anybody throw some light on this?

Thanks,
Pradeep
I don't know what the standard says, but if the compiler can remove the need for allocating memory, it will try to. But there are times that this may not be possible.

It is probably unspecified so that the compiler manufacturers can do as much or as little as possible.


Adrian
May 10 '07 #7
pradeep kaltari
102 Recognized Expert New Member
I don't know what the standard says, but if the compiler can remove the need for allocating memory, it will try to. But there are times that this may not be possible.

It is probably unspecified so that the compiler manufacturers can do as much or as little as possible.


Adrian
Ok. Thank You Adrain.

Regards,
Pradeep
May 10 '07 #8
AdrianH
1,251 Recognized Expert Top Contributor
Ok. Thank You Adrain.

Regards,
Pradeep
No prob. Glad to help.


Adrian
May 10 '07 #9
arunmib
104 New Member
Hey thanks to adrian and pradeep for making thing clear.....
May 10 '07 #10

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

Similar topics

6
9276
by: Randell D. | last post by:
Folks, I've asked this before but never got any response but its important and I thought I'd pitch it again (hopefully a bit clearer)... One can pass data from one function to another either by passing a copy, or passing by reference. My understanding of passing by reference (putting the & before a variable during the function declaration) means that the original data is used.
20
2562
by: Sam | last post by:
Hi I'm learning to code with C++ and wrote some very simple code. I think it's consistent with every rule but always got compiling errors that I don't understand. The code include 5 files as following, delimited by //////: ////////////////pose.h #ifndef pose_h #define pose_h #include "point.h"
0
3322
by: Zlatko Matiæ | last post by:
Hi everybody! Recently I was struggling with client/server issues in MS Access/PostgreSQL combination. Although Access is intuitive and easy to use desktop database solution, many problems appear when someone is trying to use it as front-end for real server database systems such as PostgreSQL or MySQL. One of these problems is regarding pass-through queries and parameters. I wanted to have all the code on client, while executing it on...
8
3625
by: QQ | last post by:
SpreadsheetCell(const string& initialValue); I don't understand what & means here? Thank you very much! A C++ beginner
16
2314
by: Martin Jørgensen | last post by:
Hi, Problem: ======== Some of my output functions are beginning to take pretty many arguments... I mean.... We're talking about 10-15 arguments :-) So I thought to myself, perhaps this is beginning to get out of hands if I continue to put in extra functionality in the (file-writing) output-functions....
50
3309
by: LaundroMat | last post by:
Suppose I have this function: def f(var=1): return var*2 What value do I have to pass to f() if I want it to evaluate var to 1? I know that f() will return 2, but what if I absolutely want to pass a value to f()? "None" doesn't seem to work.. Thanks in advance.
36
2585
by: Pat | last post by:
Hi, I've run into a strange problem, but one that seems like it might be fairly common. I have a single base class, from which several other classes are derived. To keep the example simple, the base class is "Animal" and the derived classes are "Cat," "Dog," and "Horse." The base class has a pure virtual method:
9
9560
by: Trent | last post by:
Here is the error while using Visual Studio 2005 Error 1 error LNK2019: unresolved external symbol "void __cdecl print(int,int,int,int,int,int,int,int)" (?print@@YAXHHHHHHHH@Z) referenced in function _main assign2.obj Thanks a lot ! Here is the code:
11
3358
by: venkatagmail | last post by:
I have problem understanding pass by value and pass by reference and want to how how they are or appear in the memory: I had to get my basics right again. I create an array and try all possible ways of passing an array. In the following code, fun1(int a1) - same as fun1(int* a1) - where both are of the type passed by reference. Inside this function, another pointer a1 is created whose address &a1 is different from that of the passed...
0
8872
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8747
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9392
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9246
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9162
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9091
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6694
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4505
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4773
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.