473,513 Members | 4,001 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Proper Way to Pass Numerical Values to a Function


Most of the C programming I write manipulates strings so I haven't had
much experience with numerical functions and passing variables in and
out which brings me to a laughably simple problem that I am having.
How does one pass two unsigned longs in to a function that
will ultimately do floating point calculations on them?

A C tutorial I have says that one should not declare the
inputs to a function as floats but should use doubles which will
handle other numerical types. An example of what I am trying to do is:

void do_percent (lownum, highnum)
double lownum;
double highnum;
{ /*percentage routine*/
printf ("%.3f\n", (lownum/highnum));
return;
} /*percentage routine*/

There is no percentage arithmetic in it yet because if I call
it like:

do_percent (smallnumber, largenumber); with both the input arguments
being unsigned long numbers, the two values become corrupt when doing a
gdb trace.

If I declare them as unsigned longs in the function, they do
keep their values, but printf outputs garbage values so it obviously
doesn't know what I tried to do.

I simply want to learn good practices for passing numerical
values in to functions so any good ideas are appreciated. I have
obviously misunderstood what I read. Many thanks.
--

Martin McCormick WB5AGZ Stillwater, OK
Information Technology Division Network Operations Group
Nov 13 '05 #1
2 2003
Martin McCormick wrote:

Most of the C programming I write manipulates strings so I haven't had
much experience with numerical functions and passing variables in and
out which brings me to a laughably simple problem that I am having.
How does one pass two unsigned longs in to a function that
will ultimately do floating point calculations on them?

A C tutorial I have says that one should not declare the
inputs to a function as floats but should use doubles which will
handle other numerical types. An example of what I am trying to do is:

void do_percent (lownum, highnum)
double lownum;
double highnum;
{ /*percentage routine*/
printf ("%.3f\n", (lownum/highnum));
return;
} /*percentage routine*/

There is no percentage arithmetic in it yet because if I call
it like:

do_percent (smallnumber, largenumber); with both the input arguments
being unsigned long numbers, the two values become corrupt when doing a
gdb trace.


That's because do_percent() expects to receive two
`double' values, but you've passed it two `unsigned long'
values instead. The traffic cop asked you for your license
and you handed him a codfish; is it any surprise that you're
in trouble? Write

do_percent( (double)smallnumber, (double)largenumber );

A far better solution is to get a more recent tutorial,
one that's been written less than a decade and a half ago.
Then you'd learn that the way you've written the function is
old-hat, and that a newer and better way has been available
since 1989:

void do_percent(double lownum, double highnum)
{
printf ("%.3f", lownum / highnum);
}

This do_percent() function, like the original, requires two
`double' arguments. However, this manner of defining the
function also "advertises" the number and nature of its
arguments so the compiler can generate whatever conversions
are necessary between what you supply and what do_percent()
expects (or can complain if what you supply can't be converted
to `double').

Since your tutorial apparently knows about `void' (which
came into the language at the same time as the newer function
style), it should tell you that the old style shown above is
kept around only for compatibility with pre-1989 C and that
you should use the new style for practically all new code.
Look up the word "prototype" in the index -- and if it's not
there, get yourself a newer tutorial.

--
Er*********@sun.com
Nov 13 '05 #2
In article <3F***************@sun.com>,
Eric Sosman <Er*********@Sun.COM> wrote:
A far better solution is to get a more recent tutorial,
one that's been written less than a decade and a half ago.
Then you'd learn that the way you've written the function is
old-hat, and that a newer and better way has been available
since 1989:


You are absolutely right. I got to thinking and I have had
that book for eleven years. My thanks to you and to another person
who emailed me off-list with similar information.

I new this might be one of those teachable moments and it
turns out to be. My old book does have prototyping in it and I have
certainly used it before, but I never understood nearly as clearly as
I do now why you have to do it. I have written a lot of code that
works but doesn't have prototyping in it. The only reason that is
true is because I was lucky in that whatever I was doing involved
integers or other variable types that defaulted to the correct type.

It is kind of like having a bare wire in an electrical cord.
Kids, don't try this at home, but one of the two wires in a power cord
is neutral and will probably not shock you if you touch it while in a
tub of bath water. You've got a 50% chance of getting that one or the
hot wire. Good luck.

In all seriousness, my knowledge of C has many holes in it,
but at least now I know what prototyping actually does for one. It
lays down the rules for how data are handled at any given time.

Again, many thanks to both of you.
--

Martin McCormick WB5AGZ Stillwater, OK
Information Technology Division Network Operations Group
Nov 13 '05 #3

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

Similar topics

7
13718
by: BCC | last post by:
Hi, I have a class with several member variables that should be initialized according to user input before an object is instantiated. Using a static variable is required here. But, I would like to have each object of my class have unique values of these variables, because the values may be different depending on some other factor in the...
11
4934
by: red floyd | last post by:
I have a copy of the C++ standard. Unfortunately, I don't have a copy of the C89 standard, and the question I have is related to something incorporated by reference. Specifically, what does the Standard say about the behavior of gmtime() when the argument is negative (i.e. before the epoch)? Is it UB? Should gmtime() return NULL/0? ...
16
3232
by: Martin Jørgensen | last post by:
Hi, I've made a program from numerical recipes. Looks like I'm not allowed to distribute the source code from numerical recipes but it shouldn't even be necessary to do that. My problem is that I'm not very experienced with pointers, pointers to pointers and the like and I got 4 compiler warnings + I don't completely understand how to...
4
6369
by: alexandre.brisebois | last post by:
Hi, I am using access 2003, I would like to know if there is an option to reorganize the tables in a maner that is readable, as we can do in sql sever 2000 or 2005. I have been given a database to look a and I am loosing tremendious amounts of time trying to organize it so that I could view it. Regards, Alexandre Brisebois
2
4760
by: Parachute | last post by:
Using Borland Builder 6, I get numerical overflow when running a small programme without CodeGuard ("exp: OVERFLOW error"). The programme does not give any error messages when CodeGuard is activated during the run. The equation that causes overflow uses the exp-function, but given and resulting values are not very large. So why does it seem as...
171
4722
by: Raman | last post by:
Hi All, Here is a small Code, int main(void) { char *p=(char *) malloc(100); strcpy(p,"Test1234567890"); p=p+10; free(p);
14
20370
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 passed also. I understand that this way of passing the array is by value and if the prototype is declared as foo(int *), it is by reference in which case...
10
13610
by: Robert Dailey | last post by:
Hi, I noticed in Python all function parameters seem to be passed by reference. This means that when I modify the value of a variable of a function, the value of the variable externally from the function is also modified. Sometimes I wish to work with "copies", in that when I pass in an integer variable into a function, I want the...
6
2914
by: Academia | last post by:
I want to declare in vb: FSCTL_GET_COMPRESSION FSCTL_SET_COMPRESSION which are declared in Winioctl.h.
0
7270
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...
0
7178
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...
0
7397
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. ...
0
7563
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...
1
7125
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...
0
5703
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
4757
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...
0
1612
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
1
813
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.