473,406 Members | 2,220 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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 1547
"Michel Rouzic" <Mi********@yahoo.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********@yahoo.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********@yahoo.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_Keith) 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
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 << "...
5
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...
0
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
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...
5
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...
4
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...
2
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...
7
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
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...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...
0
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...
0
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...
0
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,...
0
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...

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.