--------------------------------------------------- | Date: 2002-03-14 03:09:45 | | Filename: functions.py | | Author: mind@metalshell.com | | | | http://www.metalshell.com/ | --------------------------------------------------- #!/usr/local/bin/python # # function example by mind@metalshell.com # # here is an example on creating and using custom functions. # # 03/12/2002 # # http://www.metalshell.com # # example of making a custom function using integers # def func_name(string1, string2, ...) # def func_name(integer1, integer2, ...) # def func_name(string1, integer1, ...) # # bad example: # def func_name(string1, integer1, integer2): # if len(string1) == 0: # return 1 # # if integer1 < integer2: # return 2 # # # if func_name(some_string, 3, some_string2) == 2: # do_whatever.. # # obviously you cant pass a string to a function that thinks its an integer # unless you change the string to an integer or the other way around in the # function. # define our integers test1 = 10 test2 = 12 def add_two(num): num = num + 2 return num def sub_two(num): num = num - 2 return num def is_equal(str1, str2): if str1 == str2: return 1 else: return 0 if is_equal(test1, test2) == 1: print test1, "equals", test2 else: print test1, "does not equal", test2 if add_two(test1) == test2: print test1, "+ 2 equals", test2 else: print test1, "+ 2 does not equal", test2 if sub_two(test1) == test2: print test1, "- 2 equals", test2 else: print test1, "- 2 does not equal", test2 if sub_two(test2) == test1: print test2, "- 2 equals", test1 else: print test2, "- 2 does not equal", test1 # ok you get the idea... you can also do the same thing with strings: test3 = "This is a test string" test4 = "This is a test string" test5 = "string test a is This" def match(str1, str2): if str1 == str2: return 1 else: return 0 if match(test3, test4) == 1: print "\"", test3, "\"", "and", "\"", test4, "\"", "both match" else: print "\"", test3, "\"", "and", "\"", test4, "\"", "do not match" if match(test4, test5) == 1: print "\"", test4, "\"", "and", "\"", test5, "\"", "both match" else: print "\"", test4, "\"", "and", "\"", test5, "\"", "do not match" print "Example finished!"