Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
10327 views
ubuntu2004
Kernel: Python 3 (system-wide)

Functions IV - functions can call functions

As we've seen a function can be called form inside another function. The following code shows an example we've already seen.

def say_hello(name): """Print Hello name""" print( f'Hello {name}') say_hello('Bob')

In this example we call the function say_hello() which we have written. Our function calls the inbuilt function print().

But our function can also call another function that we have written.

Look at the following code then run it.

def add_full_stop(sentence): """Add a full stop to sentence""" return sentence + '.' def say_hello(name): """Print Hello name.""" # Add a full stop to the string "Hello name" and assign to the variable full_sentence full_sentence = add_full_stop( f'Hello {name}' ) print( full_sentence ) say_hello('Bob')

Here we have defined two functions.

The first, add_full_stop(), takes a string as an argument and returns that string with a full stop added.

The second, say_hello(), takes a string as an argument, it calls add_full_stop() with that string included in the sentence "Hello name" (which adds a full stop) and assigns the returned string to the variable sentence.

Exercise Notebook

Next Notebook