# LargestReturned.py (c) Kari Laitinen # http://www.naturalprogramming.com # 2006-04-12 File created. # 2022-12-16 Converted to Python 3. # The corresponding C++ program has the name largest_with_return.cpp. # When compared to the corresponding Java/C#/C++ programs, this # program has some extra lines at the end. Those extra statements # show how some standard features of Python can be used to # make the search_largest_integer() function superfluous. def search_largest_integer( list_of_integers, number_of_integers_in_list ) : largest_integer = list_of_integers[ 0 ] integer_index = 1 while integer_index < number_of_integers_in_list : if list_of_integers[ integer_index ] > largest_integer : largest_integer = list_of_integers[ integer_index ] integer_index += 1 return largest_integer # The main program. first_list = [ 44, 2, 66, 33, 9 ] second_list = [ 888, 777, 66, 999, 998, 997 ] found_largest_integer = search_largest_integer( first_list, 5 ) print( "\n The largest integer in first_list is %d." % \ found_largest_integer ) print( "\n The largest integer in second_list is %d." % \ search_largest_integer( second_list, 6 ) ) # Python provides the standard max() function which can be used # to search for the maximum value in the following way. print( "\n The largest integer in first_list is %d." % \ max( first_list ) ) print( "\n The largest integer in second_list is %d." % \ max( second_list ) ) print( "\n The largest of three first values in second_list is %d." % \ max( second_list[ 0 : 3 ] ) )