Skip to content
Home » Functions in Python – Calling Functions in Other Functions

Functions in Python – Calling Functions in Other Functions

Spread the love

Here’s another article in the Functions in Python series. Today we’ll be talking about calling functions in other functions. If you haven’t read the previous parts yet, feel free to do so. Here they are:

1) Introduction to Functions

2) Functions with Parameters

3) The return Statement

4) Mandatory Parameters

5) Optional Parameters

6) Mutable Optional Parameters

7) Keyword Arguments

8) Arbitrary Number of Positional Parameters

9) Arbitrary Number of Keyword Parameters

10) Arbitrary Number of Both Positional and Keyword Parameters

11) Nested Functions

We can call a function inside another function. Let’s define 2 simple functions:

def trim_string(text, length):
    if len(text) > length:
        return text[:length]
    else:
        return text

def repeat_trimmed_string(text, length, repetitions):
    trimmed_text = trim_string(text, length) + " "
    return trimmed_text * repetitions

print(repeat_trimmed_string("interesting", 5, 7))

The second function, repeat_trimmed_string, calls the first function in its body. Here’s the result:

inter inter inter inter inter inter inter

Your Panda3D Magazine

Make Awesome Games and Other 3D Apps

with Panda3D and Blender using Python.

Cool stuff, easy to follow articles.

Get the magazine here (PDF).

Python Jumpstart Course

Learn the basics of Python, including OOP.

with lots of exercises, easy to follow

The course is available on Udemy.

Blender Jumpstart Course

Learn the basics of 3D modeling in Blender.

step-by-step, easy to follow, visually rich

The course is available on Udemy and on Skillshare.


Spread the love
Tags:

Leave a Reply