Skip to content

Why Functions?

Why Functions?

Functions are an essential part of most programming languages. Functions are reusable pieces of code that can be called using the function's name. Functions can be called anywhere in a Python program, including calling functions within other functions.

Functions provide a couple of benefits:

  • Functions allow the same piece of code to run multiple times

  • Functions break long programs up into smaller components

  • Functions can be shared and used by other programmers

Every function has a name. The function name is used when the function is called in a program. Calling a function means running a function.

Functions can receive input from the program. The input provided to a function is called input arguments or just arguments. Arguments are the code passed to a function as input.

Functions can produce output. We say a function returns output to the program. The output of a function can be assigned to a variable for use in a program.

Below is an example calling Python's pow() a function:

In [2]:
out = pow(3,2)

In the function call above, the function name is pow. pow is the power function. The pow function raises a number to a power. The input arguments are the numbers 3 and 2. The function output is assigned to the variable out. In this example, the function returns the value 9 (3 raised to the 2 power, 3^2 = 9).