Functions and Object Arguments
Lesson 22 of 48 in Coddy's Python for C Developers course.
C functions receive parameter values; copying a pointer still allows access to the pointed-to object. Python calls bind parameter names to the supplied objects. Rebinding an integer parameter does not rebind the caller name, but mutating a supplied list is visible to the caller. Define free functions with def; no class is required. return sends a value back instead of printing it. A tuple can return several results without an output-pointer parameter or a custom result struct.
C: complete example.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
int twice(int value) { value *= 2; return value; }
int main(void) {
int x = 3; int y = twice(x);
printf("%d %d\n", x, y);
return 0;
}Python: complete example.py
def twice(value):
value *= 2
return value
x = 3
print(x, twice(x))Watch the difference
A Python function that reaches its end returns None. It is not an uninitialized C return value. Keep reusable helpers separate from the driver that owns input and output.
Your code goes in functions.py. The locked driver.py is the entry file, supplies execution and checks, and cannot be edited.
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.
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.
Return a tuple instead of a C result struct or output-pointer pair.
Your code goes in functions.py. The locked driver.py is the entry file, supplies execution and checks, and cannot be edited.
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 C Developers
1From C Programs to Python
Run Python Without mainPrinting Without Format CodesNames Versus Typed StorageDivision and Integer LimitsSuites Instead of BracesRecap: A Python Receipt4Iteration and Transformations
Iteration Without PointersIndexes Without Pointer MathSentinels and Loop ControlComprehensions and SortingRecap: Stable Ranking2Values and Truth Tests
Absence Is Not a Null PointerContent Versus Object IdentityTruth Tests and OperandsConditional Value SelectionRecap: Missing or Eligible5Functions and Callbacks
Functions and Object ArgumentsDefaults and Named ArgumentsVarargs Without va_listCallbacks and Captured NamesRecap: Pricing Functions3Containers and Aliasing
Lists Versus C ArraysTuples and Struct ValuesMappings and Lookup LoopsPointers, Copies, and AliasesRecap: Inventory Collections6Contracts, Errors, and Modules
Text Input and Exact OutputError Codes Versus ExceptionsImports Are Not IncludesAnnotations Versus C TypesRecap: Validate a Batch9Project: An Order Processor
Project: Model an OrderProject: Pricing PoliciesPractice on your own: Online Python compiler