Programming with Python

  1. Home
  2. Docs
  3. Programming with Python
  4. Functions in python
  5. Defining a function

Defining a function

A function is a block code that takes in some data and, either performs some kind of transformation and returns the transformed data, or performs some task on the data, or both.

Parts of function:–

  • Keyword def- This is the keyword used to say that a function will be defined now.
  • Function name- This is the name that is used to identify the function. The function name comes after the def keyword.
    If the function name has multiple words, they should be in lower case, and they should be separated with an underscore.
  • Parameter list- Parameter list are placeholder that define the parameters that go into the function. In python, parameters are enclosed in parentheses.
  • Function docstring- These are optional constructs that provide a convenient way for associated documentation to the corresponding function. Docstring are enclosed by triple quotes.
  • Function returns- Python function returns a value.

syntax:-

def name_of_function(arguments):
 '''
     docstring
               '''
   body of function
   return

Note that the function block is intended by four spaces.

Was this article helpful to you? Yes No

How can we help?