KnowledgeBoat Logo
|

Robotics & Artificial Intelligence

How will you define a function?

Python Functions

1 Like

Answer

The rules to be followed for defining a function in Python are as mentioned below:

(a) The first line of the function called header must begin with the keyword def followed by the function name and parentheses ( ).

(b) Any input parameters should be defined within the parentheses.

(c) The function header must end with a colon (:). The function block (i.e., a set of statements) should be indented under function header.

(d) A docstring (resembles as a comment statement) can be used to state about the action taken in the function block. The docstring is written within triple quotes (''' or """ """). It is written just below the function header.

(e) The return statement signifies an exit of control from the function. It may or may not pass the value back to the caller function.

Syntax:

def <function name>(<parameters>):
    <"function documentation (docstring)">
    <function statements>
    <return [expression]>

# Main Program
.....................................
    Statements
.....................................

Answered By

1 Like


Related Questions