
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Defining a Function in Python
You can define functions to provide the required functionality. Here are simple rules to define a function in Python.
- Function blocks begin with the keyword def followed by the function name and parentheses ( ( ) ).
- Any input parameters or arguments should be placed within these parentheses. You can also define parameters inside these parentheses.
- The first statement of a function can be an optional statement - the documentation string of the function or docstring.
- The code block within every function starts with a colon (:) and is indented.
- 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 return None.
Syntax
def functionname( parameters ): "function_docstring" function_suite return [expression]
By default, parameters have a positional behavior and you need to inform them in the same order that they were defined.
Example
The following function takes a string as input parameter and prints it on standard screen.
def printme( str ): "This prints a passed string into this function" print str return
- Related Articles
- Defining Clean Up Actions in Python
- C program to find maximum of four integers by defining function
- Defining a variable reference in SAP ABAP
- Defining Colors in CSS3
- Defining Keyframes in CSS3
- Defining the midpoint of a colormap in Matplotlib
- Defining a discrete colormap for imshow in Matplotlib
- Defining static members in C++
- Defining Class Methods in Perl
- Defining new functions in Arduino
- PHP Defining namespace
- Calling a Function in Python
- Defining a primary key in an Attribute view in SAP HANA
- Defining user lock settings in SAP HANA
- PHP Defining Multiple Namespaces in same file

Advertisements