Beginnger Question
Curtis Jensen
cjensen at bioeng.ucsd.edu
Tue Apr 9 13:30:34 EDT 2002
Dennis wrote:
> If there's a better forum for asking newb questions, please point me
> there!
>
> I'm new to programming. My only experience up to this point is some
> aborted attempts to learn C and Java. I'm finding Python much easier
> to learn than those. Currently, I'm pushing my way through
> "Non-Programmers Tutorial for Python" by Josh Cogliati.
>
> My current problem is this:
>
> x = 2
>
> def wrong(num):
> return num + 1
>
> print wrong(x)
> print wrong(x)
>
> I cannot figure out why this (simplified) program won't increment x
> and produce 3 and 4. This is probably indicative of a bigger problem
> with understanding functions (and their limitations). Are there any
> sources out there that explain functions more in-depth and clearly?!
> Thanks for any and all help.
>
> Dennis
> dennis at horsethieves.com
In this example, the problem is that you are not actualy modifying x.
You probably want to do something like:
x = 2
x = wrong(x)
print x #should print 3
x = wrong(x)
print x # should print 4
In general you might want to look into "pass by reference" and "pass by
value" function arguments. This will help in understanding how function
arguments are handled in general. Though, Python has it's own argument
passing mechanism generaly called "pass by object" I suggest doing a
groups.google search for these terms in the Python news group.
--
Curtis Jensen
cjensen at bioeng.ucsd.edu
http://www-bioeng.ucsd.edu/~cjensen/
FAX (425) 740-1451
More information about the Python-list
mailing list