functions in python

By | March 12, 2019

Functions in Python-: Functions are the most important aspect of an application. A function can be defined as the organised block of reusable code which can be called whenever required. A function is a block of organised, reusable code that is used to perform a single related action.

In other words we can say that the collection of functions creates a program. The function is also known as procedure or subroutine in other programming languages.

Procedure or function- : In Other languages a function that doesn’t return a value so it is called a procedure. We  write functions that do not have a return statement these are not  really procedures. All Python procedures are functions means  if no explicit return is executed in the procedure body the special Python value None is returned and if return arg is executed the value arg is immediately returned. Nothing else in the function body is executed after a return has been executed. Because Python does not have true procedures.

Defining functions  -:  Most of the functions need parameters and each language has its own specifications for how function parameters are defined. Python is flexible and provides three options for defining function parameters. These options are here

  1. Positional parameters
  2. Passing arguments by parameter name
  3. Variable numbers of arguments
  4. Mixing argument-passing techniques

Positional parameters-: The simplest way to pass parameters to a function in Python is by position. In the first line of the function we can  specify variable names for each parameter, when the function is called  the parameters used in the calling code are matched to the function’s parameter variables based on their order. For example

>>> def power(x, y):
...     r = 1
...     while y > 0:
...         r = r * x
...         y = y - 1
...     return r
...
>>> power(3, 3)
27

Passing arguments by parameter name-: We can pass arguments into a function by using the name of the corresponding function parameter rather than its position. Continuing with the previous interactive example we can type

>>> power(2, 3)
8
>>> power(3, 2)
9
>>> power(y=2, x=3)
9

In this arguments to power in the final invocation are named their order is irrelevant. In the arguments are associated with the parameters of the same name in the definition of power and we get back 3^2. This type of argument passing is called keyword passing.

Variable numbers of arguments-: Python functions can be defined to handle variable numbers of arguments which we can do in two ways. One way handles the relatively familiar case in which we want to collect an unknown number of arguments at the end of the argument list into a list. The other method can collect an arbitrary number of keyword-passed arguments which have no correspondingly named parameter in the function parameter list into a dictionary.

Prefixing the final parameter name of the function with a * causes all excess non-keyword arguments in a call of a function to be collected together and assigned as a tuple to the given parameter. Here is a simple way to implement a function to find the maximum in a list of numbers

>>> def maximum(*numbers):
...     if len(numbers) == 0:
...         return None
...     else:
...         maxnum = numbers[0]
...         for n in numbers[1:]:
...             if n > maxnum:
...                 maxnum = n
...         return maxnum
...

Mixing argument-passing techniques-: It’s possible to use all of the argument-passing features of Python functions at the same time although it can be confusing if we do not done with care. The general rule for using mixed argument-passing is that positional arguments come first, then named arguments, followed by the indefinite positional argument with a single *, and last of all the indefinite keyword argument with **.

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *