Skip to main content

Python 3 Part 7 - Functions in Python 3

Functions in Python 3

Table of content

  1. Function
  2. Default value of argument
  3. Passing arbitrary arguments using *args
  4. Passing arbitrary keyword arguments using **kwargs

Function

Function is a block of code executed when it is called. The syntax for defining a function is as below.

def function_name(<arguments):
    statement
    return statement


Example of a simple function which takes no arguments.


Function which takes arguments and returns addition.


Function passing arguments with key.


Default value of argument 

We can set default value for argument, if value is not passed for that argument it takes default value.


Passing arbitrary arguments using *args

If the number of argument function is not fixed we can use arbitrary argument


Passing arbitrary keyword arguments using **kwargs

We can pass arbitrary number of arguments with some values.

Thank you folks. If you like this Python 3 post, please do checkout my other post on  Django with Python 3 series


Most viewed

Ruby on Rails Part 4 - Exception Handling

  Ruby on Rails Part 4 - Exception Handling  Table of content Exception Handling retry raise ensure else  catch and throw Exception classes Exception Handling Enclose the code that could raise an exception with a begin/end block and use rescue clauses to tell Ruby the types of exceptions that you want to handle. The syntax for exception handling : 
 begin  
      #- statements
 rescue OneTypeOfException       #-
 handle the exception rescue AnotherTypeOfException       #- 
handle the exception else       
# Other exceptions
 ensure 
      # ensure block is always executed
 end 
 Everything from begin to rescue is protected 

in the block. 
If an exception occurs during the execution of this block of code, control is passed to the 

block between rescue and end.
 
For each rescue clause in the begin block, Ruby compares the raised Exception against each 

of the parameters of the...