weird function output value problem

M

Michel Rouzic

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?
 
C

Chris McDonald

Michel Rouzic said:
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?
 
C

Chris McDonald

Chris McDonald said:
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.
 
K

Keith Thompson

Michel Rouzic said:
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;
}
 
M

Michel Rouzic

Chris said:
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.

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?

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
 
M

Michel Rouzic

You assumed that arguments are passed by reference rather than by

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

Martin Ambuhl

Michel said:
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.
 
P

pete

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 */
 
L

Lawrence Kirby

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top