Home arrow Python arrow Page 7 - Python 101 (part 6): Hedgehogs, Pythons And Funky Chameleons

Arguing Your Case - Python

This week, Python 101 discusses how to abstract out parts of yourPython code into reusable functions, add flexibility to them by allowingthem to accept different arguments, and make them return specific values.Also included: a discussion of variable scope and functions to help youwrite your own functions. Confused? All is explained within...

TABLE OF CONTENTS:
  1. Python 101 (part 6): Hedgehogs, Pythons And Funky Chameleons
  2. Cheating The Taxman
  3. Talking Movies
  4. Call Me Sometime
  5. Return To Me
  6. Tall, Dark And Handsome
  7. Arguing Your Case
  8. Enter The Funky Chameleon
  9. Flavour Of The Month
  10. Hip To Be Square
By: Vikram Vaswani, (c) Melonfire
Rating: starstarstarstarstar / 4
July 16, 2001

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
Now, if you've been paying attention, you've seen how functions can help you segregate blocks of code, and use the same piece of code over and over again, thereby eliminating unnecessary duplication. But this is just the tip of the iceberg...

The functions you've seen thus far are largely static, in that the variables they use are already defined. But it's also possible to pass variables to a function from the main program - these variables are called "arguments", and they add a whole new level of power and flexibility to your code.

To illustrate this, let's go back a couple of pages and revisit the tempConv() function.

def tempConv(): celsius = 35 fahrenheit = (celsius * 1.8) + 32 return fahrenheit
As is, this function will always calculate the Fahrenheit equivalent of 35 degrees Celsius, no matter how many times you run it or how many roses you buy it. However, it would be so much more useful if it could be modified to accept *any* value, and return the Fahrenheit equivalent of that value to the calling program.

This is where arguments come in - they allow you to define placeholders, if you will, for certain variables; these variables are provided at run-time by the main program.

Let's now modify the tempConv() example to accept an argument.

#!/usr/bin/python # set a variable - this could be obtained via input() alpha = 40 # define a function def tempConv(temperature): fahrenheit = (temperature * 1.8) + 32 return fahrenheit # call function with argument result = tempConv(alpha) # print print alpha, "Celsius is", result, "Fahrenheit"
And now, when the tempConv() function is called with an argument, the argument is assigned to the placeholder variable "temperature" within the function, and then acted upon by the code within the function definition.

It's also possible to pass more than one argument to a function - as the following example demonstrates.

>>> def addIt(item1, item2, item3): ... result = item1 + item2 + item3 ... return result ... >>> addIt("hello", "-", "john") 'hello-john' >>>
Note that there is no requirement to explicitly specify the type of argument(s) being passed to a function - since Python is a dynamically-typed language, it can automatically identify the variable type and act on it appropriately.

 
 
>>> More Python Articles          >>> More By Vikram Vaswani, (c) Melonfire
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PYTHON ARTICLES

- Python Big Data Company Gets DARPA Funding
- Python 32 Now Available
- Final Alpha for Python 3.2 is Released
- Python 3.1: String Formatting
- Python 3.1: Strings and Quotes
- Python 3.1: Programming Basics and Strings
- Tuples and Other Python Object Types
- The Dictionary Python Object Type
- String and List Python Object Types
- Introducing Python Object Types
- Mobile Programming using PyS60: Advanced UI ...
- Nested Functions in Python
- Python Parameters, Functions and Arguments
- Python Statements and Functions
- Statements and Iterators in Python

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: