Definition and declaration

The definition of a function specifies exactly how a function is constructed, what parameters are passed to it, and what value it returns to the calling function.

With the declaration we name a variable and make it known to the compiler.

Definition and declaration differ in some points. The definition includes a memory allocation for the function, while no memory is allocated in the declaration. The declaration can be done several times, vice versa a function can be defined exactly once in a program.

Syntax

In Python, functions are defined with the keyword def followed by the function name and the input parameters.

def function_name():
	# code to be executed

def function_name(parameter_1 = default_value, parameter_2 = default_value, ...):
	# code to be executed

Example

def add():
	# code to be executed

Another Example

Code Explanation

LineDescription
1Defines the function add() with the input parameters a and b. Variable a is assigned the value 3 in the function header and variable b the value 5
2The function returns the sum of a and b
def add(a = 3, b = 5):
    return a + b