473,791 Members | 2,827 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

passing an argument by reference

I have an api that uses reference as arguments.

void function(double z, short &x, short &y){}
some calculations are done on z and then the results are being passed back
out in x and y.

The values the i need to pass to this function are pointer to shorts.

short * x1;
short * y1;

If I call the function like

function(z1, *x1, *y1);

The compiler doesn't complain and I get correct results with the small
number of tests that I have performed on it. But I am not sure that this is
correct.

Thanks, Mike

-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
Jul 22 '05 #1
16 1648
On Sun, 2 May 2004 22:50:03 -0600, "Michael G" <mi****@montana .com>
wrote in comp.lang.c++:
I have an api that uses reference as arguments.

void function(double z, short &x, short &y){}
some calculations are done on z and then the results are being passed back
out in x and y.

The values the i need to pass to this function are pointer to shorts.

short * x1;
short * y1;

If I call the function like

function(z1, *x1, *y1);

The compiler doesn't complain and I get correct results with the small
number of tests that I have performed on it. But I am not sure that this is
correct.

Thanks, Mike


Assuming that x1 and y1 are properly initialized and actually point to
short ints, this is just fine.

What are passed are references to the shorts that the pointers point
to.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 22 '05 #2

"Michael G" <mi****@montana .com> wrote in message
news:40******** **@corp.newsgro ups.com...
I have an api that uses reference as arguments.

void function(double z, short &x, short &y){}
some calculations are done on z and then the results are being passed back out in x and y.

The values the i need to pass to this function are pointer to shorts.

short * x1;
short * y1;

If I call the function like

function(z1, *x1, *y1);

The compiler doesn't complain and I get correct results with the small
number of tests that I have performed on it. But I am not sure that this is correct.

Thanks, Mike


The function call is correct provided x1 and y1 actually point to
shorts, i.e.

short a;
short b;

short * x1 = &a;
short * x2 = &b;
Jul 22 '05 #3
Michael G wrote:
I have an api that uses reference as arguments.

void function(double z, short &x, short &y);
This is a very bad idea.
some calculations are done on z
and then the results are being passed back out in x and y.
*Real* C++ programmers do it like this:

cat main.cc

#include <iostream>

std::pair<short , short> f(double z);

int main(int argc, char* argv[]) {
std::pair<short , short> p = f(33.0);
std::cout << p.first << ", " << p.second << std::endl;
return 0;
}

Jul 22 '05 #4
std::pair<short , short> f(double z);


Why should we do simple when we can do complicated ?
Jul 22 '05 #5

"Boris Sargos" <bs*****@wanado o.fr> wrote in message
news:c7******** **@news-reader2.wanadoo .fr...
std::pair<short , short> f(double z);


Why should we do simple when we can do complicated ?


Which one do you consider to be more complicated?

john
Jul 22 '05 #6
"E. Robert Tisdale" <E.************ **@jpl.nasa.gov > wrote in message news:<40******* *******@jpl.nas a.gov>...
Michael G wrote:
I have an api that uses reference as arguments.

void function(double z, short &x, short &y);


This is a very bad idea.
some calculations are done on z
and then the results are being passed back out in x and y.


*Real* C++ programmers do it like this:

> cat main.cc

#include <iostream>

std::pair<short , short> f(double z);

int main(int argc, char* argv[]) {
std::pair<short , short> p = f(33.0);
std::cout << p.first << ", " << p.second << std::endl;
return 0;
}

Well - those two functions are different. Your function has no access
to the initial values of x and y.

/Peter
Jul 22 '05 #7

"Michael G" <mi****@montana .com> wrote in message
news:40******** **@corp.newsgro ups.com...
I have an api that uses reference as arguments.

void function(double z, short &x, short &y){}
some calculations are done on z and then the results are being passed back
out in x and y.

The values the i need to pass to this function are pointer to shorts.
Well, not exactly. You need to *get* the values from pointers to shorts.

short * x1;
short * y1;

If I call the function like

function(z1, *x1, *y1);

The compiler doesn't complain and I get correct results with the small
number of tests that I have performed on it. But I am not sure that this is correct.


It is. It might help to look at it like this.

short x2;
short y2;
x2 = *x1;
y2 = *y1;
function(z1, x2, y2);
Make more sense now?
Jul 22 '05 #8

"E. Robert Tisdale" <E.************ **@jpl.nasa.gov > wrote in message
news:40******** ******@jpl.nasa .gov...
Michael G wrote:
I have an api that uses reference as arguments.

void function(double z, short &x, short &y);


This is a very bad idea.
some calculations are done on z
and then the results are being passed back out in x and y.


*Real* C++ programmers do it like this:


Oh boy. That's just what the OP needs......
Jul 22 '05 #9
Peter Koch Larsen wrote:
E. Robert Tisdale wrote:
Michael G wrote:
I have an api that uses reference as arguments.

void function(double z, short &x, short &y);
This is a very bad idea.
some calculations are done on z
and then the results are being passed back out in x and y.


*Real* C++ programmers do it like this:

> cat main.cc

#include <iostream>

std::pair<short , short> f(double z);

int main(int argc, char* argv[]) {
std::pair<short , short> p = f(33.0);
std::cout << p.first << ", " << p.second << std::endl;
return 0;
}


Well - those two functions are different.
Your function has no access to the initial values of x and y.


Mike's original description of the API
describes x and y as outputs -- not inputs.
But here is a version which passes them as inputs:
cat main.cc

#include <iostream>

std::pair<short , short> f(short x, short y, double z);

int main(int argc, char* argv[]) {
short x = 7, y = 13;
std::pair<short , short> p = f(x, y, 33.0);
x = p.first;
y = p.second;
std::cout << x << ", " << y << std::endl;
return 0;
}

Jul 22 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
14953
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) { document.images.src = eval("mt" +menu+ ".src") } alert("imgOff_hidemenu"); hideMenu=setTimeout('Hide(menu,num)',500);
58
10182
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of code... TCHAR myArray; DoStuff(myArray);
25
2946
by: Victor Bazarov | last post by:
In the project I'm maintaining I've seen two distinct techniques used for returning an object from a function. One is AType function(AType const& arg) { AType retval(arg); // or default construction and then.. // some other processing and/or changing 'retval' return retval; }
17
9394
by: LP | last post by:
Hello, Here's the scenario: Object A opens a Sql Db connection to execute number of SqlCommands. Then it needs to pass this connection to a constructor of object B which in turn executes more commands on the same connection. I have an understanding that if SqlConnection is passed as "value" (unboxed), object B will create its own copy of SqlConnection, so when object A closes its connection, it remains open for object B's copy. Is this
6
6002
by: MSDNAndi | last post by:
Hi, I get the following warning: "Possibly incorrect assignment to local 'oLockObject' which is the argument to a using or lock statement. The Dispose call or unlocking will happen on the original value of the local." My code is: using System; using System.Collections.Generic;
12
2689
by: Andrew Bullock | last post by:
Hi, I have two classes, A and B, B takes an A as an argument in its constructor: A a1 = new A(); B b = new B(a1);
12
5405
by: Mike | last post by:
Consider the following code: """ struct person { char *name; int age; }; typedef struct person* StructType;
1
1976
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. function foo2(){} function foo(func){}
12
2595
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 by value temporaries are created?(I'm not touching yet the cases where standard allows optimizations from the side of implementations to avoid copying) If so, please quote part of the standard that says that. Assuming it is true, I can imagine two...
4
2121
by: puzzlecracker | last post by:
How can I pass a reference to a method as constant? I tried the following: Function(const Foo f) or Function(readonly Foo f) Also, How to declare local variable to be constant const Foo foo or readonlyFoo f?
0
9669
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
9515
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,...
1
10154
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
9993
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
7537
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
6776
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4109
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3713
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2913
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.