Confused: Newbie Function Calls
Dave Angel
davea at ieee.org
Wed Aug 11 13:08:44 EDT 2010
fuglyducky wrote:
> I am a complete newbie to Python (and programming in general) and I
> have no idea what I'm missing. Below is a script that I am trying to
> work with and I cannot get it to work. When I call the final print
> function, nothing prints. However, if I print within the individual
> functions, I get the appropriate printout.
>
> Am I missing something??? Thanks in advance!!!!
>
> ################################################
> # Global variable
> sample_string = ""
>
> def gen_header(sample_string):
> HEADER = """
> mymultilinestringhere
> """
>
> sample_string += HEADER
> return sample_string
>
> def gen_nia(sample_string):
> NIA = """
> anothermultilinestringhere
> """
>
> sample_string += NIA
> return sample_string
>
>
> gen_header(sample_string)
> gen_nia(sample_string)
>
> print(sample_string)
>
>
It'd be best if you used different names for global scope than you do
inside your functions. It won't change how this case works, but at
least it'd be clearer what's happening. And sometimes you can get an
unintended side effect when you use the same name for two different
variables.
In function gen_header(), you take an argument, and return a modified
version of it. But the call to it never uses the return value. If you
want to make any changes to the global value, you'd do something like this:
sample_string = gen_header(sample_string)
sample_string = gen_nia(sample_string)
print(sample_string)
HTH
DaveA
More information about the Python-list
mailing list