4.2. Return Values

A function doesn't always have to display its output directly. It can process some data and then return it in a variable or something else. The return statement takes a value from inside a function and sends it back to the line that called the function. Let's see some examples.

Returning a Simple Value

Let's look at a function that takes a first and last name, and returns a neatly formatted full name:

  • In line ❶, the function get_formatted_name() takes two parameters.
  • Then, in line âť·, we assign full_name.title() to return
  • The converted name is then saved in a variable called celebrity, to be printed in the following line.

Making an Argument Optional

Sometimes it makes sense to make an argument optional so people can provide extra info when necessary. To make an argument optional, you can use default values empty string like this ''.

Let start by expanding the previous function to include middle name:

Remember that parameter with default value has to go last. Even though there's no error, the actress name is not in the right order now and there's an extra space in the middle of the actor name. To fix this problem, we can use if statement as in the example.


> Calling this function with a first and last name is straightforward. If we're using a middle name, however, we have to make sure the middle name is the last argument.

Returning a Dictionary

A function can return lists and even dictionaries. The following function takes in parts of a name and returns a dictionary containing that person's info.

This function takes in simple info and puts it into a more easier to understand structure. The next change makes it possible to store someone's name. See the example.

We add a new optional parameter age, with the default value None. In conditional test, None is considered as False.

Like we did in previous sections, sometimes we want to do the action in the functions to numerous objects. Instead of writing one by one, function can accept lists and also dictionaries.

Say we have a list of names and want to print "Hi!" to each. We can do the following.

Let's put the greeting into a list of greeting so we can call them one by one.

Lambda function

A lambda function is a small anonymous function. A lambda function can take any number of arguments, but can only have one expression.

As an example, here is a lambda function that adds 10 to the number passed in as an argument, and print the result.

> This function is the short hand of:def plus_ten(a):
return a + 10

print(plus_ten(7))

Lambda functions can take any number of arguments. For example, these two examples show

  1. a lambda function that multiplies argument a with argument b and print the result.

  2. a lambda function that sums argument a, b, and c and print the result.

Exercise 4.2

  1. Count Character
    Write a function called count_character() that accept two strings and return the following string:
    "{string_1}" has size {length of string_1} and "{string_2}" has size {length of string_2}.
    To check if its working, uncomment the print part in the code.

  2. Less, More, or Equal
    Write a function called compare_character() that accept two strings and return the following string:
    The length of the string "{string_1}" is {less than, more than, equal to} the length of the string "{string_2}".
    To check if its working, write the following:

  3. Comparing Two Strings
    Write a function called comparing_char() that accept three arguments: two strings and one Boolean value (default True). If the boolean value is True, print the final string of no.1 and if False, print the final string of no.2.

  4. Pythagorean triple
    Three numbers a, b, c, are called Pythagorean triple if a2 + b2 = c2 . Make a lambda function called is_pythagorean_triple that accept three arguments. It returns the value True if it is a pythagorean triple. Otherwise, it is False. By using that function and the provided list, print the following:

(20, 99, 101) is a triple Pythagoras.
(60, 91, 109) is a triple Pythagoras.
(16, 112, 113) is not a triple Pythagoras.
(44, 117, 125) is a triple Pythagoras.

Hint: To get the first element in the first triple, you can write num_list[0][0]