Introduction to Functions

2.1. Introduction to Functions#

A function is a self-contained sequence of statements or instructions that possesses a name. For example, consider a function designed to convert Celsius to Fahrenheit. Since the formula for this conversion remains consistent, with the only variable being the temperature in Celsius, you can create a function that accepts the temperature as an argument, performs the conversion, and subsequently returns the temperature in Fahrenheit.

What are the advantages of using functions?

  • Many programs require a specific sequence of statements or instructions to be executed repeatedly. By consolidating these repetitive statements into a single function, which can be accessed whenever necessary, functions help reduce code duplication.

  • Functions aid in breaking down larger programs into logical subprograms, enhancing ease of writing and debugging.

  • Functions can be invoked at any point using their names.

  • Functions can call other functions.

  • Functions can OPTIONALLY accept argument(s) that they can utilize within the function.

  • Functions can OPTIONALLY return value(s).

The general form of a function call is as follows:

def <<function_name>>(<<parameters>>):
    body

Note

Note the distinction between parameters and arguments: Function parameters are the names listed in the function’s definition. Function arguments are the actual values provided to the function. Parameters are initialized with the values of the supplied arguments. Reference

In numerous programming languages, braces ({}) are employed to enclose a function’s contents. In contrast, in Python, a function body is differentiated from the surrounding code by indenting all of the function content by four spaces

def convert_celsius_to_fahrenheit(degrees_in_celsius):
    return 9 / 5 * degrees_celsius + 32

2.1.1. IndentationError#

An IndentationError is a type of SyntaxError that occurs when the indentation is incorrect.

def convert_celsius_to_fahrenheit(degrees_in_celsius):
    f = 9 / 5 * degrees_in_celsius + 32
  return 

indent_error