Robotics & Artificial Intelligence

Discuss the structure of a Python function with the help of an example.

Python Functions

1 Like

Answer

The structure of a Python function consists of two main parts: Function Header and Function Body.

The function header includes the def keyword, followed by the function name, a list of parameters enclosed within parentheses, and ends with a colon. It defines the name of the function and the parameters that the function can accept.

The function body consists of an indented block of statements that specify what the function does. It may include one or more statements and can optionally contain a return statement to return a value to the calling statement.

Example:

def square(n):
    return n * n
result = square(6)
print(result)

In this example, def square(n): is the function header, where square is the function name and n is the parameter. The indented statement return n * n forms the function body, which calculates and returns the square of the given number when the function is called.

Answered By

1 Like


Related Questions