473,715 Members | 6,096 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Function Declaration

How do you define whether the return value of a function is ByRef or ByVal?

I have a utility class that cleans imported records by doing *really heavy*
string manipulation in lots of different methods, and it operates on
DataTables in excess of 100,000 rows of anywhere between 4 and 30 columns.

I'd like to get the functions to return ByRef for greater speed and memory
efficiency, but I can't see how to do that.

--
Peace & happy computing,

Mike Labosh, MCSD

"Mr. McKittrick, after very careful consideration, I have
come to the conclusion that this new system SUCKS."
-- General Barringer, "War Games"
Nov 21 '05 #1
14 2563
I would first suggest that you use a StringBuilder, rather than Strings for
heavy string manipulation.

Second, what is your function returning? If it returns a Reference Type,
then you are only getting a copy of the pointer to the reference type, not a
copy of the reference type itself.

"Mike Labosh" <ml*****@hotmai l.com> wrote in message
news:O4******** ******@TK2MSFTN GP09.phx.gbl...
How do you define whether the return value of a function is ByRef or
ByVal?

I have a utility class that cleans imported records by doing *really
heavy* string manipulation in lots of different methods, and it operates
on DataTables in excess of 100,000 rows of anywhere between 4 and 30
columns.

I'd like to get the functions to return ByRef for greater speed and memory
efficiency, but I can't see how to do that.

--
Peace & happy computing,

Mike Labosh, MCSD

"Mr. McKittrick, after very careful consideration, I have
come to the conclusion that this new system SUCKS."
-- General Barringer, "War Games"

Nov 21 '05 #2
>I would first suggest that you use a StringBuilder, rather than Strings for
heavy string manipulation.
Actually, most of the stuff uses Regex's. But I do use StringBuilder where
I can. StringBuilder let me do one of our export batches in half the time
it used to take :):):)
Second, what is your function returning? If it returns a Reference Type,
then you are only getting a copy of the pointer to the reference type, not
a copy of the reference type itself.


Pretty much all the method prototypes in this class look like this:

Public Function CleanSomething( ByRef value As String) As String

There is one method that returns a Structure, also.

I just want to make sure I'm returning the return value ByRef for greater
speed / efficiency.

--
Peace & happy computing,

Mike Labosh, MCSD

"Mr. McKittrick, after very careful consideration, I have
come to the conclusion that this new system SUCKS."
-- General Barringer, "War Games"
Nov 21 '05 #3

"Mike Labosh" <ml*****@hotmai l.com> wrote in message
news:uH******** *****@TK2MSFTNG P12.phx.gbl...
I would first suggest that you use a StringBuilder, rather than Strings
for heavy string manipulation.


Actually, most of the stuff uses Regex's. But I do use StringBuilder
where I can. StringBuilder let me do one of our export batches in half
the time it used to take :):):)
Second, what is your function returning? If it returns a Reference Type,
then you are only getting a copy of the pointer to the reference type,
not a copy of the reference type itself.


Pretty much all the method prototypes in this class look like this:

Public Function CleanSomething( ByRef value As String) As String

There is one method that returns a Structure, also.

I just want to make sure I'm returning the return value ByRef for greater
speed / efficiency.

--
Peace & happy computing,

Mike Labosh, MCSD

"Mr. McKittrick, after very careful consideration, I have
come to the conclusion that this new system SUCKS."
-- General Barringer, "War Games"


A function's return value can't be ByRef or ByVal. It's just what it
returns. Now, the parameters that go into the function can be ByRef or
ByVal.

HTH,

Mythran

Nov 21 '05 #4
> A function's return value can't be ByRef or ByVal. It's just what it
returns. Now, the parameters that go into the function can be ByRef or
ByVal.


That's what I was afraid of. Oh well, you win some, you lose some. I was
just hoping to squeak a little more performance out of this.

--
Peace & happy computing,

Mike Labosh, MCSD

"Mr. McKittrick, after very careful consideration, I have
come to the conclusion that this new system SUCKS."
-- General Barringer, "War Games"
Nov 21 '05 #5
> A function's return value can't be ByRef or ByVal. It's just what it
returns. Now, the parameters that go into the function can be ByRef or
ByVal.


Not necessarily true for situations when a function returns an object
instance.

If string objects are being created in the function and then the string
object variable is returned, a copy of the pointer gets created if the
return value is captured like this:

Dim X As String = function()

In any event, you don't have to worry about copies of the actual data being
created and passed back.
Nov 21 '05 #6

"Mike Labosh" <ml*****@hotmai l.com> wrote in message
news:eB******** ******@TK2MSFTN GP15.phx.gbl...
A function's return value can't be ByRef or ByVal. It's just what it
returns. Now, the parameters that go into the function can be ByRef or
ByVal.


That's what I was afraid of. Oh well, you win some, you lose some. I was
just hoping to squeak a little more performance out of this.

--
Peace & happy computing,

Mike Labosh, MCSD

"Mr. McKittrick, after very careful consideration, I have
come to the conclusion that this new system SUCKS."
-- General Barringer, "War Games"


One way to squeak a little more performance is to read/write only what you
need. Instead of loading all data into a data table or data set, you can
load the strings you need into strings, manipulate what you need, write to
the store. If you are using files, read only what you need into a string,
manipulate it, and write it back out. It sounds as though you are loading
from a database, in this case, do you really need to do all of the
manipulation using client code? Just for kicks, and to see if we can speed
this up a lot, post a little of the logic you are using (code) for
manipulating the data. It might be possible to do all this using SQL
Server, which turns out to be a lot faster for manipulating data in the
database (go figure, a database is faster at manipulating data?!? :P )

Mythran

Nov 21 '05 #7
Mike,
In addition to the other comments.

I would define the function as:

| Public Function CleanSomething( ByVal value As String) As String

To help ensure the "fastest" possible function! Remember that ByVal & ByRef
refer to how parameters are passed, while Reference Type & Value Type refer
to how values are stored.

ByVal passes a copy of the variable as the parameter.
ByRef passes a reference to the variable as the parameter.

Reference Types exist on the heap, a variable holds a reference to the
actual object on the heap.
Value Types exist on the stack or nested inside another object, a variable
holds the actual value.
String is a Reference type, which means that a String variable or parameter
holds a reference to the actual string object on the Heap. If you pass a
String ByRef to a routine, you are passing a reference to the variable that
holds a reference to the actual string object on the heap. In other words a
reference to a reference to an object. If you pass a String ByVal you
passing the reference itself. In other words a reference to an object.

I would expect ByRef String to be slightly slower as you are dereferencing a
reference each time you want to access the String's value.

When you define your function "As String" you are receiving a copy of the
reference to the actual string object on the heap. There is only one
instance of the String on the heap.

Hope this helps
Jay

"Mike Labosh" <ml*****@hotmai l.com> wrote in message
news:uH******** *****@TK2MSFTNG P12.phx.gbl...
| >I would first suggest that you use a StringBuilder, rather than Strings
for
| >heavy string manipulation.
|
| Actually, most of the stuff uses Regex's. But I do use StringBuilder
where
| I can. StringBuilder let me do one of our export batches in half the time
| it used to take :):):)
|
| > Second, what is your function returning? If it returns a Reference Type,
| > then you are only getting a copy of the pointer to the reference type,
not
| > a copy of the reference type itself.
|
| Pretty much all the method prototypes in this class look like this:
|
| Public Function CleanSomething( ByRef value As String) As String
|
| There is one method that returns a Structure, also.
|
| I just want to make sure I'm returning the return value ByRef for greater
| speed / efficiency.
|
| --
| Peace & happy computing,
|
| Mike Labosh, MCSD
|
| "Mr. McKittrick, after very careful consideration, I have
| come to the conclusion that this new system SUCKS."
| -- General Barringer, "War Games"
|
|
Nov 21 '05 #8
>"If you pass a String ByVal you
passing the reference itself. In other words a reference to an object."
Just a little clarification here, you are actually passing a COPY of the
original reference to the object, not the original reference itself.

Sub One()
Dim y As String = "Test"
Two(y)
End Sub

Sub Two(ByVal x as String)
'The parameter "x" is a copy of the pointer "y" that points to the
String object on the heap.
'There are now two pointers that point to the one String object
'Only the "x" pointer can be used in this procedure, and only the "y"
pointer can be used in the "One" procedure.
End Sub



"Jay B. Harlow [MVP - Outlook]" <Ja************ @msn.com> wrote in message
news:uu******** ******@TK2MSFTN GP12.phx.gbl... Mike,
In addition to the other comments.

I would define the function as:

| Public Function CleanSomething( ByVal value As String) As String

To help ensure the "fastest" possible function! Remember that ByVal &
ByRef
refer to how parameters are passed, while Reference Type & Value Type
refer
to how values are stored.

ByVal passes a copy of the variable as the parameter.
ByRef passes a reference to the variable as the parameter.

Reference Types exist on the heap, a variable holds a reference to the
actual object on the heap.
Value Types exist on the stack or nested inside another object, a variable
holds the actual value.
String is a Reference type, which means that a String variable or
parameter
holds a reference to the actual string object on the Heap. If you pass a
String ByRef to a routine, you are passing a reference to the variable
that
holds a reference to the actual string object on the heap. In other words
a
reference to a reference to an object. If you pass a String ByVal you
passing the reference itself. In other words a reference to an object.

I would expect ByRef String to be slightly slower as you are dereferencing
a
reference each time you want to access the String's value.

When you define your function "As String" you are receiving a copy of the
reference to the actual string object on the heap. There is only one
instance of the String on the heap.

Hope this helps
Jay

"Mike Labosh" <ml*****@hotmai l.com> wrote in message
news:uH******** *****@TK2MSFTNG P12.phx.gbl...
| >I would first suggest that you use a StringBuilder, rather than Strings
for
| >heavy string manipulation.
|
| Actually, most of the stuff uses Regex's. But I do use StringBuilder
where
| I can. StringBuilder let me do one of our export batches in half the
time
| it used to take :):):)
|
| > Second, what is your function returning? If it returns a Reference
Type,
| > then you are only getting a copy of the pointer to the reference type,
not
| > a copy of the reference type itself.
|
| Pretty much all the method prototypes in this class look like this:
|
| Public Function CleanSomething( ByRef value As String) As String
|
| There is one method that returns a Structure, also.
|
| I just want to make sure I'm returning the return value ByRef for
greater
| speed / efficiency.
|
| --
| Peace & happy computing,
|
| Mike Labosh, MCSD
|
| "Mr. McKittrick, after very careful consideration, I have
| come to the conclusion that this new system SUCKS."
| -- General Barringer, "War Games"
|
|

Nov 21 '05 #9
If I pass a structure byval that contains both reference and value types,
what exactly am I passing and if I change a structure field in my function,
is the structure field in the calling procedure changed also?
--
Dennis in Houston
"Jay B. Harlow [MVP - Outlook]" wrote:
Mike,
In addition to the other comments.

I would define the function as:

| Public Function CleanSomething( ByVal value As String) As String

To help ensure the "fastest" possible function! Remember that ByVal & ByRef
refer to how parameters are passed, while Reference Type & Value Type refer
to how values are stored.

ByVal passes a copy of the variable as the parameter.
ByRef passes a reference to the variable as the parameter.

Reference Types exist on the heap, a variable holds a reference to the
actual object on the heap.
Value Types exist on the stack or nested inside another object, a variable
holds the actual value.
String is a Reference type, which means that a String variable or parameter
holds a reference to the actual string object on the Heap. If you pass a
String ByRef to a routine, you are passing a reference to the variable that
holds a reference to the actual string object on the heap. In other words a
reference to a reference to an object. If you pass a String ByVal you
passing the reference itself. In other words a reference to an object.

I would expect ByRef String to be slightly slower as you are dereferencing a
reference each time you want to access the String's value.

When you define your function "As String" you are receiving a copy of the
reference to the actual string object on the heap. There is only one
instance of the String on the heap.

Hope this helps
Jay

"Mike Labosh" <ml*****@hotmai l.com> wrote in message
news:uH******** *****@TK2MSFTNG P12.phx.gbl...
| >I would first suggest that you use a StringBuilder, rather than Strings
for
| >heavy string manipulation.
|
| Actually, most of the stuff uses Regex's. But I do use StringBuilder
where
| I can. StringBuilder let me do one of our export batches in half the time
| it used to take :):):)
|
| > Second, what is your function returning? If it returns a Reference Type,
| > then you are only getting a copy of the pointer to the reference type,
not
| > a copy of the reference type itself.
|
| Pretty much all the method prototypes in this class look like this:
|
| Public Function CleanSomething( ByRef value As String) As String
|
| There is one method that returns a Structure, also.
|
| I just want to make sure I'm returning the return value ByRef for greater
| speed / efficiency.
|
| --
| Peace & happy computing,
|
| Mike Labosh, MCSD
|
| "Mr. McKittrick, after very careful consideration, I have
| come to the conclusion that this new system SUCKS."
| -- General Barringer, "War Games"
|
|

Nov 21 '05 #10

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

Similar topics

2
8831
by: Thomas Matthews | last post by:
Hi, I'm getting linking errors when I declare a variable in the global scope, but not inside a function. The declarations are the same (only the names have been changed...). class Book { public: Book()
21
3850
by: Rob Somers | last post by:
Hey people, I read a good thread on here regarding the reason why we use function prototypes, and it answered most of my questions, but I wanted to double check on a couple of things, as I am writing something up on functions, and I don't like writing about things I am not sure about. Ok, then, here we go: I initially thought that one would only really need to use a function
3
2305
by: Dennis Chang | last post by:
Hi all, I was reading about function pointers and came across something which intrigued me. K&R2 calls qsort (pg.119) within main as so: qsort( (void **) lineptr, 0, nlines-1, (int (*) (void *, void*))(numeric ? numcmp : strcmp) ); I guess what interests me is the nameless function pointer and then the
6
7993
by: Daniel Nichols | last post by:
I've noticed that in a C module (.c, .h file combination) that if you create a function's definition before it is used in other functions than a declaration is not necessary. I believe if the compiler can find the definition of the function prior to encountering the use of the function it will generate the prototype itself. I don't currently use this feature, I explicitly create declarations for all functions in a header file. However, I...
20
2370
by: Christian Christmann | last post by:
Hi, in a benchmark I've found an uncommon use of a function. This is the simplified form: 1 int foo( int f ) 2 { 3 return f; 4 } 5
20
1802
by: svata | last post by:
Hello there, after some time of pondering I come to some solution which would suit me best. Please correct, if I am wrong. Function has two parameters. A string array, better said a pointer to it, char *p_buf and int size. int read_name( char *p_buf, int size) { char *p_item_name_1;
4
2514
by: Paulo Matos | last post by:
Hi all, I'm trying to work out a parser for function declarations but it turns out that it is harder than I initially thought. I'm looking at 3rd Ed of Stroustrup, page 808. I'm trying to parse something like: int foo(int, int); const double *xpto(mytype *, mytype &) const; But I'm not being able to find my way around the grammar.
2
5328
by: f rom | last post by:
----- Forwarded Message ---- From: Josiah Carlson <jcarlson@uci.edu> To: f rom <etaoinbe@yahoo.com>; wxpython-users@lists.wxwidgets.org Sent: Monday, December 4, 2006 10:03:28 PM Subject: Re: 1>make_buildinfo.obj : error LNK2019: unresolved external symbol __imp__RegQueryValueExA@24 referenced in function _make_buildinfo2 Ask on python-list@python.org . - Josiah
28
4702
by: Bill | last post by:
Hello All, I am trying to pass a struct to a function. How would that best be accomplished? Thanks, Bill
26
4869
by: aruna.mysore | last post by:
Hi all, I have a specific problem passing a function pointer array as a parameter to a function. I am trying to use a function which takes a function pointer array as an argument. I am too sure about the syntax of calling the same. #include <stdio.h> void fp1()
0
8823
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
9343
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...
1
9104
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,...
1
6646
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
5967
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();...
0
4477
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
4738
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2541
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2119
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.