Function in Python


 Function

         A function is a block of organised, reusable code that is used to perform a single, related action.

A functions is a set of statements that take input, do some specific computation and produce output. The idea is to put some commonly or repeatedly done task together and make a function. so that instead of writing the same code again and again for different inputs we can call the function.

Python allows us to divide a large program into the basic building blocks known as a function. 

Python provide us various inbuilt functions like range() or print(). Although, the user can create its functions, which can be called user-defined functions.

There are mainly two types of functions.

  • User-define functions - The user-defined functions are those define by the user to perform the specific task.
  • Built-in functions - The built-in functions are those functions that are pre-defined in Python.


Advantages of Function


There are the following advantages of Python functions.
  • Using functions, we can avoid rewriting the same logic/code again and again in a program.
  • We can call Python functions multiple times in a program and anywhere in a program.
  • They break the large complex problem into small parts.
  • Reusability is the main achievement of Python functions.
  • They reduce duplication of code in a program.

Creating a function 

    Python provides a def keyword to define the function.

Here are simple rules to define a function in Python.

  • Function block 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 optional statement. This is called Documentation string of the function or Docstring.
  • The code block within every function starts with a colon ( : ) and is indented.
  • The return statement is used to return the value. A function can have only one return.

Syntax for Function Definition

def functionname(parameters):
            
            "function Docstring"               # optional

             statement(s)

             return(expression)

Function calling

  Defining a function only gives it a name, specifies the parameters that are to be included in the function and structure the block of code. Once the basic structure of a function is finalised, then you can execute it by calling it from another function or directly.
A function must be defined before the function call; otherwise, the Python interpreter gives an error. To call the function, use the function name followed by the parentheses.

Example

  1. #function definition  
  2. def hello_world():    
  3.     print("hello world")    
  4. # function calling  
  5. hello_world()      

Output

hello world

Example

def printme(str):

        "this prints a passed string into this function"

         print(str)

         return

printme("i am first called to user defined function")

printme("again second call to the same function")

Output

i am first called to user defined function

again second call to the same function

Example 

  1. # Defining function  
  2. def sum():  
  3.     a = 10  
  4.     b = 20  
  5.     c = a+b  
  6.     return c  
  7. # calling sum() function in print statement  
  8. print("The sum is:",sum())  

Output

The sum is: 30

Example : Creating function without return statement


  1. # Defining function  
  2. def sum():  
  3.     a = 10  
  4.     b = 20  
  5.     c = a+b  
  6. # calling sum() function in print statement  
  7. print(sum())  

Output

None

No comments:

Post a Comment