Francais

Francais


Home


Products


Download


Tutorials

    Python Section
  • Python Quick Reference

  • Web Ring/Links


    Contact

    Create a function

    The function definition begins by the reserved word def follow by the function name and its parameters between parentheses, ended by a : (colon)
    The first function's lines of code contain the help. There are between triples " (double quotes). It is better to create them each time.
    They will help the user or can use by documentation generator programs.
    The function must indent at the same level than the function comments.
    All variables define inside a function exist only inside the function.
    A function completes its execution by returning a value to the caller by the return statement.

    Example
    :
    def addition(A,B) :
     """function addition
     
       Returns the sum of the two given values.
       A and B"""
     sum=A+B
     return sum
     
    >>> sum=0
    >>> print add(3,2), sum
    5, 0

    Default value for function's arguments

    Make argument optional is done by assigns a default value to it, by adding the equal sign and the default value after the parameter's name. Remark: an optional parameter can never be follow by a non-optional parameter.

    Example :
    def increment(A,B=1)
      """function increment"""
          accepts two values and returns their sum..
          by defaut add one to the lone value give"""
      return A+B
     
    >>>print increment(5)
    6
    >>>print increment(5,2)
    7



    Previous Page
    11/13

    Next Page