In Python, function is a group of related statements that perform a specific task. Functions help break our program into smaller and modular chunks. As our program grows larger and larger, functions make it more organized and manageable. Furthermore, it avoids repetition and makes code reusable.
In Python each function definition is of the form
def name of function (list of formal
body of function
For example, we could define the function max by the code
def max(x, y):
if x > y:
def is a reserved word that tells Python that a function is about to be defined.
A function definition which consists of following components.
1. Keyword def marks the start of function header.
2. A function name to uniquely identify it. Function naming follows the same rules of writing identifiers in Python.
3. Parameters (arguments) through which we pass values to a function. They are optional.
4. A colon (:) to mark the end of function header.
5. Optional documentation string (docstring) to describe what the function does.
6. One or more valid python statements that make up the function body. Statements must have same indentation level (usually 4 spaces).
7. An optional return statement to return a value from the function.
A function is a named sequence of statements that performs a computation. When you define a function, you specify the name and the sequence of statements. Later, you can “call” the function by name.
The name of the function is type. The expression in parentheses is called the argument of the function. The resultis called the return value.
The return statement is used to exit a function and go back to the place from where it was called. The Syntax of return statement:
This statement can contain expression which gets evaluated and the value is returned. If there is no expression in the statement or the return statement itself is not present inside a function, then the function will return the None object.
Hello, May. Good morning!
Here, None is the returned value.
Basically, we can divide functions into the following two types:
1. Built-in functions - Functions that are built into Python.
2. User-defined functions - Functions defined by the users themselves.
Python provides built-in functions that convert values from one type to another. The int function takes any value and converts it to an integer, if it can, or complains otherwise:
ValueError: invalid literal for int(): Hello
int can convert floating-point values to integers, but it doesn’t round off; it chops off the fraction part:
float converts integers and strings to floating-point numbers:
Finally, str converts its argument to a string:
Python has a math module that provides mathematical functions.
>>> import math
This statement creates a module object named math. If you print the module object, you get some information about it:
>>> print math
<module 'math' (built-in)>
The module object contains the functions and variables defined in the module. To access one of the functions, you have to specify the name of the module and the name of the function, separated by a dot (also known as a period). This format is called dot notation.
>>> math.sqrt(2) / 2.0