Menu
CoddyTech

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 icon

Challenge

Beginner

In 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)
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Python for C Developers

Practice on your own: Online Python compiler