[Tutor] Input parameters for functions

Alan Gauld alan.gauld at yahoo.co.uk
Fri Jun 19 19:49:20 EDT 2020


On 19/06/2020 15:44, Mary Knauth via Tutor wrote:

> My original py script worked 100% with no parameters in the functions, 
> so what is the advantage of adding the parameters?

To answer that we have to go back to basics and ask why write
functions in the first place?

There are basically two reasons_
a) The save us having to retype code every time we need it,
   we can just call the function.
b) We can reuse code in more than one program. Think about
   the standard library modules that you have probably used.
   eg. We can import sys and then use the functions (like exit)
   that are inside sys in every program.
c) They make our code easier to read by hiding the details
   of an operation. That doesn't really depend so  much
   on parameters, although it can help, but we will ignore
   it as an issue for now.

For case (a) it doesn't matter so much whether we use
parameters or not. It's our code, we know what it is doing.
We can use global variables and we know what those variables
are called. No problem.(Although if you need to use it on
different values within your program things change, as
you'll see below)

But in case (b) if we don't have parameters we must still
rely on global variables. And that means we must know the names
of those variables. But if the code that is calling our
function is in a different program how can we be sure it
has the same variable names? Or worse, that if it does use
the same names that it uses them the same way we did when
we wrote the function?

Consider a simple function that calculates the square
of a number:

def square()
   return x*x

Now that will work fine if we have a variable called x.

But if I try to import the code and use the square function
in another program I need to create a variable in my program
called x otherwise the function will fail. But the only
way I know to do that is if I read your code.
Easy in this case but imagine if the function had several
dozen (or hundred) lines of code inside! (Which is common
in professional size programs)

Now see what changes if we rewrite square using a parameter:

def square(x):
   return x*x

Now I can import your code and pass it any variable name
I like:

import mary_code

z = 42
print(mary_code.square(z))
y = 27
print(mary_code.square(y))


Notice I called square twice. The first time I passed
a variable called z, the second time I passed y.

Without the parameter I can't do that, I have to use
x so the equivalent code becomes:

import mary_code

z = 42
y = 22
x = z
print(mary_code.square())
x = y
print(mary_code.square())

Notice how I have to introduce an x value then copy the
values I want to use (z and y) into x before I can call
the function.

Now imagine I already have a variable called x that I'm
using for something else. Now it gets really messy. I have
to copy x to a temporary variable. Then copy my real
variable (z or y) to x. then call the function. Then
copy the temporary variable back into x.

And I have to do that every time I try to use the function.
Pretty soon the function is causing more work than it saves!

That's why its a good idea to make your functions like standalone
pieces of code with no dependencies on the external code
that calls them. Then you can use them across many projects,
or with many values in your own project.

> def balance(account_balance)
> def deposit(account_balance, deposit_amount) ———----——is ‘account_balance’ necessary?
> def withdrawal(account_balance, withdrawal_amount) —— is ‘account_balance’ necessary?

Yes that looks right. The deposit and withdrawal functions need to know
the starting balance and the amount. They can then return the ending
balance.

The first function may not be necessary if it just returns the value
given to it! But if it prints a nice message or checks it is not
negative or something then in that case it would be sensible to
tell it what the balance was. (Although we might rename it to
print_balance() or check_balance() perhaps)

Note: There are other problems associated with using global variables
in functions but they require much more complex examples and
only really apply to large programs so we can ignore them for now.

HTH
-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list