1. Computing

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

From , former About.com Guide

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'.

  1. About.com
  2. Computing
  3. Python
  4. Beginning Python
  5. Python "Hello World" Tutorial - Defining Python Functions

©2013 About.com. All rights reserved.