On 10/19/2012 11:40 AM, Daniel Gulko wrote: > Thanks David. This has been helpful in understanding a bit more on how > parameters are passed through.
Please don't top-post. it ruins the sequence of events. Your comments above happened after the parts you quote below. So why are they not after the things they follow? There is a long-standing convention in this forum, and many others, and why let Microsoft ruin it for all of us? >> Date: Thu, 18 Oct 2012 04:44:55 -0400 >> Subject: Re: [Tutor] Help Passing Variables >> From: dwightdhu...@gmail.com >> To: dangu...@hotmail.com >> CC: tutor@python.org >> >> #A little more complex in terms of params: >> >> def SwapCaseAndCenter(*kwargs): By convention kwargs is used for keyword arguments, while the * means positional arguments. The function uses none of them, so it's all bogus. You can't learn anything useful about argument passing from this example code, except what NOT to do. >> if upper_or_lower == "upper": >> print a_string.center(center_num).upper() >> >> if upper_or_lower == "lower": >> print a_string.center(center_num).lower() >> >> To understand the most elementary aspects of function passing, let's write a whole new function, and call it a few times. Paste this into a file, and try various things, and make sure you see how they work. Or reread Alan's email, which was also correct and clear. First point, a simple function may take zero arguments, or one, or two, or fifty. We'll start with positional arguments, which means order matters. Python also supports keyword arguments, default values, and methods, none of which I'll cover here. When you define a function, you specify its formal parameters. You do that by putting the parameter names inside the parentheses. Unlike most languages, you do NOT specify the types of any of these. But the order matters, and the number matters. So if we define a function: def truncate_string(a_string, width): temp = a_string[:width] return temp That function needs to be called with two arguments, which match the two formal parameters. They do NOT have to have the same names, and in fact they might well be literal strings or ints, or variables with string and int values. print truncate_string("this is a long string", 4) should print out "this" (without the quotes, of course) name = raw_input("give me a name") trunc_name = truncate_string(name, 8) print trunc_name truncate_string("aaa") #gives error, because it has the wrong number of arguments Does this help? -- DaveA _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor