Defining and Using Functions
In Python, functions are defined with the syntax:
def functionName([arguments]) :
function-body
Function names follow the same naming convention as variable names specified in the section "Data Types". The arguments are specified as a comma-delimited list of variable names that will be used within the function-body. These variables will be positionally assigned the values used in the function call. More complex methods of specifying function arguments are specified in Python tutorials and references listed at the official Python website: www.python.org.
A function must be defined in a Python program before it can be called. Therefore, function definitions must occur earlier in the program than calls to those functions.
A function may optionally return a value or sequence of values.
|
Example: Defining and Using Functions
“Prints a tag plus the supplied string” tag = “function printString : “ print tag + stringToPrint
def addString(string_1, string_2) : “Concatenates 2 strings and returns the result” concatenatedString = string_1 + string_2 return concatenatedString
testString = “this is a test” printString(testString) wholeString = addString(“part1:”, “part2”) printString(wholeString) printString(addString(“this is “, “another test”)) |