[Tutor] More Function Questions (Joseph Q.)

Brian van den Broek bvande at po-box.mcgill.ca
Sun Apr 17 19:52:08 CEST 2005


Joseph Quigley said unto the world upon 2005-04-17 12:29:
> Hi all,
> Another function question.....
> 
> def bar(x, y):
>     return x + y
> 
> bar(4, 5)
> 
> So I can put anything I want in there. What good is a function like that?
> Of course I know about.
> def foo():
>     print "Hello all you who subscribe to the Python Tutor mailing list!"
> 
> So what do you use the
> def bar(x, y):
>     return x + y
> 
> bar(4, 5)
> 
> functions for? (I just need a simple example)
> Thanks,
>     Joe


Hi Joe,

(I see Max and Lee are quicker than I, but I hate to waste the typing! 
So:)

Jacob was, I think, exactly right when in his reply he stressed that 
these were just examples. I don't recall the exact context in the 
thread where such an example came up. But, a safe bet is that whoever 
gave it to you (gosh, hope it wasn't me, or the failure of 
recollection will be a bit of a blusher!) was giving you an example 
where the function definition structure was the complicated thing, not 
the function logic.

Given that Python has a builtin '+' operator, the bar function example 
adds nothing. But, consider this:

 >>> def fancy_bar(x, y):
... 	try:
... 		return x + y
... 	except TypeError:
... 		return str(x) + str(y)

The original bar will barf in cases like bar(42, 'my favourite 
string'). fancy_bar won't. (Of course, it might not have the best 
result either. But it is an example of how you can harness the power 
of function definitions to do something more than do the bare Python 
builtins.)

Perhaps this analogy will hep you grok the point of functions. Have 
you ever defined a macro in a word processor? ('Macro' often has a 
special meaning in some programming languages, but put that aside. I 
mean to focus on the macros of, say, M$ Word.) The point of those is 
to allow you to put a bunch of word processor commands and operations 
into a convenient chunk, ready for easy invocation. You could always 
do the same operations one at a time by hand in the word processor. 
But, if there is a common routine, making a macro for it makes your 
life easier.

Function definitions in a programming language are kind of like macros 
definitions in a word processor. They let you put a bunch of logic 
into one easy to call command. They don't let you do anything you 
couldn't do before. But they save you from having to write the 
explicit step by step instructions each time. (That makes life easier 
and less error-prone.)

Of course, the point of this is more clear where you have more 
complicated logic inside the function body. But the examples people 
have been giving you are trying to keep the body simple, to focus on 
the function structure.

Here's a non-trivial example where a function does useful work. (It 
probably can be done more prettily -- never mind that, it is a 2 
minute rough example :-) :

 >>> def mode_finder(a_sequence):
	'''returns a tuple of the modes of a_sequence.'''
  	count_dict = {}
  	for item in a_sequence:
  		count_dict[item] = count_dict.setdefault(item, 0) + 1
  	max_count = max(count_dict.values())
  	modes = [x for x in count_dict if count_dict[x] == max_count]
  	return tuple(modes)

 >>> mode_finder([1,2,3,1,1,4,4,5,5,3,3,7,8,7])
(1, 3)

The "mode" of a sequence is the most commonly occurring element (or 
elements). That is something one often want to know. The mode_finder 
function wraps the logic for finding it up into a single, nice, easy 
to invoke collection of commands. It is much better than rewriting 
those commands every time you want to find the mode.

Does that help explain why you'd care about functions?

Best,

Brian vdB



More information about the Tutor mailing list