Python Programming – Defining A Function

You can learn about Functions in Python Programs with Outputs helped you to understand the language better.

Python Programming – Defining A Function

You can define functions to provide the required functionality. Ferre are simple rules to define a function in Python

(a) Function blocks begin with the keyword def followed by an identifier, i.e., the name of the function followed by a parenthesized list of parameters and a colon (:) to mark the end of the function header. Next follows the block of the statement(s) that are part of the function.

(b) A name used inside a function to refer to the value passed as an argument is called a parameter. Any input parameters or arguments should be placed within these parentheses. You can also define parameters inside these parentheses.

(c) Optional documentation string (docstring) is used to describe what the function does.

(d) One or more valid Python statements that make up the function body. The function body consists of indented statements and statements must have the same indentation level (usually 4 spaces). The function body gets executed every time the function is called.

(e) An optional return statement to return a value from the function. It can be anywhere in the functional body. This statement ends the execution of the function call and “returns” the result,
i. e., the value of the expression following the return keyword, to the caller. If there is no return statement in the function code, the function ends, when the control flow reaches the end of the function body.

  • The statement return [expression] exits a function, optionally passing back an expression to the caller. A return statement with no arguments is the same as a return None.

The syntax for defining function is as follows:
def function-name( parameters ): n n n function_docstring””” function suite i.e. the function body return [expression]
You create a function with a def statement. This provides the name, parameters, and the suite of statements that creates the function’s result. By default, parameters have a positional behavior, and you need to inform them in the same order that they were defined.
The function name is the name by which the func¬tion is known. The parameter is a list of variable names; these names are the local variables to which actual argument values will be assigned when the function is applied. The suite (which must be indented) is a block of statements that computes the value for the function.

There is a convention in Python to insert a documentation string right after the def line of the function definition. The documentation string, known as a docstring, should contain a short description of the purpose of the function and explain what the different arguments and return values are. Interactive sessions from a Python shell are also common to illustrate how the code is used. Docstrings are usu¬ally enclosed in triple-double quotes ” ” “, which allow the string to span several lines or summary section of exactly one line.
The return statement specifies the result value of the function. This value will become the result of apply¬ing the function to the argument values. This value is sometimes called the effect of the function.

Function Header

It begins with the keyword def and ends with a colon (:), and contains the function identification details. The first line of a function definition is called the function header.

Function Body

Consisting of a sequence of indented (4 space) Python statement(s), to perform a task. The body can con¬tain any number of statements. The body of the function gets executed only when the function is called/invoked. The first statement of the function body can optionally be a string constant, docstring, enclosed in triple quotes. It contains the essential information that someone might need about the function. The last statement of the function, i.e., return statement returns a value from the function. A return statement may contain a constant/literal, variable, expression or function, if the return is used without anything, it will return None.

  • The sequence of statements inside a function definition is called a function body.

Here is the simplest form of a Python function. This function takes a string as an input parameter and prints it on a standard screen.
In the program given in Figure 10.5, def print( str ): # Line 1 The first line of a function definition, i.e., Line 1 is called header and the rest, of the lines in our example, is known as the body.

Example
Demo of python function.

def printme(str):
” ” ” This prints a passed string into this function ” ” ”
print(str)
return

Calling/Invoking a Function

Defining a function only gives it a name, specifies the parameters that are to be included in the function, and structures the blocks of code. Once the basic structure of a function is finalized, you can execute it by calling it from another function or directly from the Python prompt. Following is the example to call printme ( ) function.

Example
Demo of function definition.

# Function definition
def printme( str ): # Function definition
” ” ” This prints a passed string into this function ” ” ”
print(str)
return
# Now you can call printme function
printme(“Call to user-defined function”) # Function calling /invokingRUN
Call to user-defined function

 

Leave a Reply

Your email address will not be published. Required fields are marked *