Functions Outside Classes
Lesson 22 of 48 in Coddy's Python for Java Developers course.
Define a reusable function with def at module level; no utility class or static modifier is needed. Indent its body and use return to send a value to the caller. A function that reaches its end returns None, unlike a Java void method whose invocation supplies no value. Printing and returning serve different callers: a locked driver can format a returned value without duplicating output. Returning a, b creates one tuple that the caller may unpack, rather than declaring a new Java result class.
Java excerpt: class member declarations
// Class member, outside main
static int twice(int value) { return value * 2; }Python
def twice(value):
return value * 2
print(twice(4))Watch the difference
Python functions do not implicitly return the last expression. Do not write a print where the driver expects a tuple. Repeated def statements with the same name rebind it; they do not create Java-style overloads by signature.
Practice
Apply this comparison in the task below. Editable files: functions.py. Keep the provided imports and the required API names. The locked driver.py supplies execution and checks; do not edit it.
Challenge
BeginnerIn functions.py implement split_minutes(total) for nonnegative integers. Return the tuple (hours, minutes) with minutes in 0..59. The driver reads input and prints the unpacked pair separated by a space; your helper must not read or print. Unlike returning a small Java record, Python can directly return the two-value tuple.
Editable files: functions.py. Keep the provided imports and the required API names. The locked driver.py supplies execution and checks; do not edit it.
Implement the named definitions in the editable modules without input or printing. The driver owns input/output and may call the APIs again on fresh data without producing extra output.
Try it yourself
from functions import split_minutes
hours, minutes = split_minutes(int(input()))
print(hours, minutes)
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Python for Java Developers
1From Java Classes to Python
Run Python Without a ClassPrinting Text and CommentsBindings, Types, and finalDivision and Numeric TypesIndentation Defines BlocksRecap: A Python Receipt4Iteration and Transformations
Enhanced Loops and rangeIndexes and Map EntriesSentinels and Loop ControlFrom Streams to ComprehensionsRecap: Stable Ranking2Values Beyond Java Primitives
None and Boolean ValuesValue Equality and IdentityTruth Tests Beyond booleanBranches and Value SelectionRecap: Missing or Eligible5Functions Beyond Methods
Functions Outside ClassesDefaults Instead of OverloadsVarargs and Keyword OptionsScope and Callable ObjectsRecap: Pricing Functions3Collections and Object Sharing
Lists and ArrayList HabitsTuples Without Record ClassesDictionaries and Java MapsShared References and CopiesRecap: Inventory Collections6Contracts, Errors, and Modules
Text Input and Exact OutputValueError and TypeErrorModules Versus Utility ClassesAnnotations Are Not Java TypesRecap: Validate a Batch9Project: An Order Processor
Project: Model an OrderProject: Pricing PoliciesPractice on your own: Online Python compiler