473,698 Members | 2,440 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

weird function output value problem

I'm sure that it's a dumb problem where i surely did something dumb,
but, I got a problem, I got a variable that we'll call var1. in the
main() function, this variable var1 (of type unsigned) of a value of
lets say 1000. now i got a function that we'll call func1, declared by
void func1(unsigned var1) and called by func1(var1);. in this function
at some point there is let's say var1=var1*2;. everything works fine
until the end of that function, the problem is that as soon as i come
back to the main function (after the execution of the func1 function)
the value of var1 is not 2000 as expected but 1000

i already encountered the same problem in other functions in the same
program, so what did i do wrong?

Nov 14 '05 #1
8 1570
"Michel Rouzic" <Mi********@yah oo.fr> writes:
I'm sure that it's a dumb problem where i surely did something dumb,
but, I got a problem, I got a variable that we'll call var1. in the
main() function, this variable var1 (of type unsigned) of a value of
lets say 1000. now i got a function that we'll call func1, declared by
void func1(unsigned var1) and called by func1(var1);. in this function
at some point there is let's say var1=var1*2;. everything works fine
until the end of that function, the problem is that as soon as i come
back to the main function (after the execution of the func1 function)
the value of var1 is not 2000 as expected but 1000 i already encountered the same problem in other functions in the same
program, so what did i do wrong?

Assuming that func1 appears *after* main,
at your line: void func1(unsigned var1)
rename 'var1' to 'fred' and notice that main's var1 now changes.

Does that help you understand why?

--
Chris.
Nov 14 '05 #2
Chris McDonald <ch***@csse.uwa .edu.au> writes:
"Michel Rouzic" <Mi********@yah oo.fr> writes:
I'm sure that it's a dumb problem where i surely did something dumb,
but, I got a problem, I got a variable that we'll call var1. in the
main() function, this variable var1 (of type unsigned) of a value of
lets say 1000. now i got a function that we'll call func1, declared by
void func1(unsigned var1) and called by func1(var1);. in this function
at some point there is let's say var1=var1*2;. everything works fine
until the end of that function, the problem is that as soon as i come
back to the main function (after the execution of the func1 function)
the value of var1 is not 2000 as expected but 1000 i already encountered the same problem in other functions in the same
program, so what did i do wrong?


Assuming that func1 appears *after* main,
at your line: void func1(unsigned var1)
rename 'var1' to 'fred' and notice that main's var1 now changes.


Doh! Forget that suggestion, sorry.

Rename all instances of 'var1' inside func1() to 'fred',
and now notice that main's 'var1' still does not change.

--
Chris.
Nov 14 '05 #3
"Michel Rouzic" <Mi********@yah oo.fr> writes:
I'm sure that it's a dumb problem where i surely did something dumb,
but, I got a problem, I got a variable that we'll call var1. in the
main() function, this variable var1 (of type unsigned) of a value of
lets say 1000. now i got a function that we'll call func1, declared by
void func1(unsigned var1) and called by func1(var1);. in this function
at some point there is let's say var1=var1*2;. everything works fine
until the end of that function, the problem is that as soon as i come
back to the main function (after the execution of the func1 function)
the value of var1 is not 2000 as expected but 1000

i already encountered the same problem in other functions in the same
program, so what did i do wrong?


You assumed that arguments are passed by reference rather than by
value.

For example:

void func(unsigned int param)
{
param = 2000;
}

int main(void)
{
unsigned int var = 1000;
func(var);
return 0;
}

The call to func() passes a copy of the value of var, not a reference
to the variable itself. Inside func, "param" is just a local
variable; any changes in its value won't affect var.

If you want to change the value, you'll need to pass a pointer:

void func(unsigned int *param)
{
*param = 2000;
}

int main(void)
unsigned int var = 1000;
func(&var);
return 0;
}

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #4


Chris McDonald wrote:
Chris McDonald <ch***@csse.uwa .edu.au> writes:

Assuming that func1 appears *after* main,
at your line: void func1(unsigned var1)
rename 'var1' to 'fred' and notice that main's var1 now changes.


Doh! Forget that suggestion, sorry.

Rename all instances of 'var1' inside func1() to 'fred',
and now notice that main's 'var1' still does not change.

--
Chris.


um... yeah i know that inside my function i could change it to
anything, i just like to keep the same names for the same variables in
and outside of a function, but um.. the point is to return a changed
value for var1 (or fred if i declare void func1(unsigned fred) and do
fred=fred*2; and call func1(var1);) and well do you know how to fix
that?
Assuming that func1 appears *after* main,


um.... i write my func1 function before main... like

void func1(unsigned var1) //or fred if you prefer
{
var1=var1*2;
}
int main()
{
unsigned var1;
var1=1000;
func1(var1);
} //and there var1 is still 1000

Nov 14 '05 #5
> You assumed that arguments are passed by reference rather than by
value.


oooh, i didnt know that. so... the only way to output of a function
something that was at its input is a pointer?

Nov 14 '05 #6
Michel Rouzic wrote:
I'm sure that it's a dumb problem where i surely did something dumb,
but, I got a problem, I got a variable that we'll call var1. in the
main() function, this variable var1 (of type unsigned) of a value of
lets say 1000. now i got a function that we'll call func1, declared by
void func1(unsigned var1) and called by func1(var1);. in this function
at some point there is let's say var1=var1*2;. everything works fine
until the end of that function, the problem is that as soon as i come
back to the main function (after the execution of the func1 function)
the value of var1 is not 2000 as expected but 1000


Your expectations are wrong
This code:
void f(unsigned v1) { v1 *= 2; }
int main(void) {
unsigned v1;
f(v1);
return 0;
}
means exactly the same thing as
void f(unsigned crap) { crap *= 2; }
int main(void) {
unsigned v1;
f(v1);
return 0;
}

There are two places that you could be confused.
1) The name of the formal parameter in the definition of f() has *no*
relationship to any instance of that variable name outside the
function.
2) The function's formal parameter is initialized with a copy of
the value of the argument with which it is called. It does not
operate on the argument in the calling function.
If you find that this is unclear and neither your text nor your teacher
can make it clearer, someone here -- perhaps even I -- will surely be
glad to help if you can tell us what confusion remains.
Nov 14 '05 #7
On Fri, 17 Jun 2005 01:37:29 -0700, Michel Rouzic wrote:
You assumed that arguments are passed by reference rather than by
value.


oooh, i didnt know that. so... the only way to output of a function
something that was at its input is a pointer?


Functions have return values but if you need to return more then one thing
then, yes, you would typically use pointer arguments.

Function arguments are ALWAYS passed by value in C. Something that may
appear to break this rule at first sight but in fact doesn't is arrays. C
doesn't have an array "value" as such, when you try to take the value of
an arry you get a pointer to its first element which is why things like

char array[10];
char *ptr = array;

work. The same happens when you try to pass an array as an argument.
given

void foo(char *ptr)
{
strcpy(ptr, "FOO");
}

I can write

foo(array);

and the string FOO will be copied into my local array. That's possible
because a pointer is being passed (by value) which allows the function
foo() to access what it points at, i.e. the array in the caller. As the
example shows this is important for library function like strcpy() too.

Lawrence
Nov 14 '05 #8
Michel Rouzic wrote:
void func1(unsigned var1) //or fred if you prefer
{
var1=var1*2;
}
int main()
{
unsigned var1;
var1=1000;
func1(var1);
} //and there var1 is still 1000


/* BEGIN new.c */

#include <stdio.h>

void func1(unsigned var1)
{
var1 *= 2;
}

void func2(unsigned *var1)
{
*var1 *= 2;
}

int main(void)
{
unsigned var1;

var1 = 1000;
func1(var1);
printf("var1 is %u\n", var1);
func2(&var1);
printf("var1 is %u\n", var1);
return 0;
}

/* END new.c */

--
pete
Nov 14 '05 #9

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

Similar topics

10
2055
by: Chris Mantoulidis | last post by:
I see some really weird output from this program (compiled with GCC 3.3.2 under Linux). #include <iostream> using namespace std; int main() { char *s; s = "test1"; cout << "s = " << s << " and &s = " << &s << "\n";
5
1665
by: Tommytrojan | last post by:
Hi, I have been using Python for a while but today I came across a really strange behavior: While poking around in Queue.py due to problems with importing this module from a thread I got an error that a module that I imported on top of the file could not be accessed. I reduced the problem to this small script:
0
1442
by: Zwyatt | last post by:
having a really weird little bug w/ time_t...check it out: I have the following code (simplified here): #include <time.h> class A { public: char *aString; int aNum;
10
1845
by: Bonj | last post by:
Hello. I hope somebody can help me on this, because I'm running out of options to turn to. I have almost solved my regular expression function. Basically it works OK if unicode is defined. It doesn't work OK in ANSI mode however, as it has to use MultiByteToWideChar and WideCharToMultiByte. I've discovered that the regular expression part is working fine. As far as I can tell the regular expression code is correctly parsing what it...
5
2366
by: Jim Strathmeyer | last post by:
So I'm having some weird problems with file output. If I try to boil this problem down to a small, simple program to just show what problems I'm having, I can't get the same problematic behavior. I have a function: bool Tourist::CreateMaps() { debug.Out() << "Tourist::CreateMaps" << std::endl; std::ofstream out((player_name + ".sav").c_str(),std::ios::out); std::string ms = Random::MapStart();
4
1385
by: Peter Afonin | last post by:
Hello, I have a weirdest issue I've ever had. I have a function that enters some data into the Oracle table and returns the sequential row number for the new record (autonumber): Private Function AddSystem(ByVal txt As TextBox, ByVal cn As OracleConnection) As Integer Try
2
1480
by: sieg1974 | last post by:
Hi, I have a linked list with 705 nodes, and the functions getContact and listContact to deal with it. listContact works properly, and prints all some debug information about each node. On the other hand, getContact breaks when it reaches the 580th node although it's almost identical to listContact . Does anyone have any idea about what is wrong? Thanks,
7
1391
by: Vmrincon | last post by:
Hi everybody! I am trying to program a function in javascript and a funny behaviour occurs. If I execute this code: aux=parseInt(year-birthyear); return (aux)
14
1823
by: leptone | last post by:
Dear all, I am programming a PLC with an 80188 processor, hence I am using an old version of Borland C++ (3.10). While doing the job, I've encountered strange behaviours and I've isolated the problem that seems to be related to a malloc function. I wrote a little piece of code to reproduce the situation: #include <stdlib.h> #include <stdio.h>
0
8676
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
8608
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
9161
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
8867
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...
0
7732
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3050
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
2332
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2006
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.