Return Values:

  • A function doesn’t always have to display its output directly. Instead, it can process some data and then return a value or set of values.
    • The value the function returns is called a return value.

How They Work:

  • The return statement takes a value from inside a function and sends it back to the line that called the function.

Example of Returning a Simple Value:

  • Let’s take a first and last name, and return a neatly formatted full name:
python
1def getFormattedUserName(firstName, lastName):
2    fullName = firstName + " " + lastName
3    return fullName
4
5wcuUser01 = getFormattedUserName("Harpua", "Bulldog")
6print(wcuUser01)

Output:

1Harpua Bulldog
  • In line 1, the definition of getFormattedUserName() takes as parameters a first and last name.
  • In line 2, the function combines these two names, adds a space between them, and stores the result in fullName.
  • In line 3, the value of fullName is then returned to the calling line.
  • When you call a function that returns a value, you need to provide a variable where the return value can be stored. In this case the returned value is stored in the variable wcuUser01 at line 6.