1. Home
  2. Computing & Technology
  3. Python

"Hello, World!": A Quick Tutorial on Python

By Al Lukaszewski, About.com

4 of 6

Defining Functions

Now, we can make a function which calls the last method of the class:

def prints(string):
     string.printme()
     return

Next, we define two more functions. These illustrate how to pass arguments to and how to receive output from functions. The strings in parentheses are arguments on which the function depends. The value returned is signified in the 'return' statement at the end.

def hello(i):
     string = "hell" + i
     return string

def caps(word):
     value = string.capitalize(word)
     return value

The first of these functions takes an argument 'i' which is later concatentated on to the base 'hell' and returned as a variable named 'string'. As we shall see in the main() function, this variable is hardwired in the program as 'o', but you could easily make it user-defined by using sys.argv[3] or similar.

The second function is used to capitalize the parts of the output. It takes one argument, the phrase to be capitalized, and returns it as a value 'value'.

4 of 6

Explore Python

More from About.com

  1. Home
  2. Computing & Technology
  3. Python
  4. Beginning Python
  5. Python "Hello World" Tutorial - Defining Python Functions

©2008 About.com, a part of The New York Times Company.

All rights reserved.