Python Functions

The main idea of a function is to promote the reuse of the code. Instead of writing all your code in one big file, you can break them down to small chunks and put them in separate functions. As an example, let’s say you want to convert a number from gallons to liters. One option is multiplying the number with 3.78541. A better option is creating a small function to do the conversion.

def gallon2liter(input_val):
   return input_val * 3.78541

Now, you can simply call gallon2liter function anytime you want to convert a number from gallon to liter.


As in the example, a function is defined by the following:

  • start with def keyword
  • have a name for a function. eg: def gallon2liter()
  • pass 0 or more arguments
  • return something from the function (optional)

Once the function is defined, you can simply call it by using the function name and passing in the arguments.

Define a function using def keyword

You can define a function using the keyword def followed by function name and parentheses enclosing any parameters.

Function name must start with a letter, _ and can contain only letters, numbers or _.

Here is how to define a function that prints “Hello World”, without taking any arguments or returning any values.

def helloWorld():
   print("Hello World")

Passing arguments to a function

You can define function arguments by putting them between the parentheses that follows the function name. You can define any number of arguments of any types.

The following function takes two arguments.

def area(height, width):
   total_area = height * width
   print(f"total area is {total_area}")

The above function takes height and width as arguments. The area is calculated from these arguments and printed. You can call the function by simply passing values for height and width.

area(20, 10)
total area is 200
area(30, 30)
total area is 900

Returning values from a function

A function can return values or choose not to return anything. The return value could be any data type such as string, number, boolean, list or dictionary.

Here is a function that takes list of values and returns square of each item in the list.

def calculateSquare(input_val):
  output = []
  for i in input_val:
    output.append(i ** 2)
  return output

You can call the above function by passing a list of numbers.

result = calculateSquare([1, 2, 3, 4, 5, 6])

[1, 4, 9, 16, 25, 36]

Practice Questions

  • Write a function that takes a sentence as an argument and returns the sentence with the first letter of each word is converted to upper case. For example, if the argument is “it is a beautiful day”, the function should return “It Is A Beautiful Day”.
  • Write a function to encode another function to decode a string. The encoding can use a simple algorithm – just replace the current character in the string with the next letter in the alphabet. For example, if the input is “welcome aboard”, the encrypt function will return “xfmdpnf bcpbse”. The decrypt function should do the reverse and restore the original string.

2 replies
  1. Tim Alphonso
    Tim Alphonso says:

    AWESOME!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! NOW I KNOW EVERYTHING IN FUNCTIONS!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! THIS SITE IS AWESOME!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!


Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *