In [ ]:
# Function: Block of code
# stops repeation of a code.

In [1]:
# your function should be pre-defined.
# every function returns a value.
# if there is nothing to return , you get None.
# return is a keyword ,which marks the end of the funtion.
# can i have more than one return value in my function? yes
# in conditional based programming.


def my_func():
    print "hello world"

In [2]:
print my_func()


hello world
None

In [5]:
def my_func():
    return "hello world"
    print "hello1"
    print "hello2"
    print "hello3"

In [6]:
print my_func()


hello world

In [8]:
# namespaces
# local scope/variables/namespaces vs global scope/variables/namespaces.
# variable defined inside a function are called local variables/namespaces.
# Local variables are available during the run time of the function.
# Local variables cannot be accessed from outside of a function. We dont have that syntax of
# facility available.
# lifetime of a variable is during the runtime of the function.

def my_new():
    a = 10
    return a

# calling my already defined function my_new()
print my_new()
print a


10
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-8-b0f0be6049a3> in <module>()
      8 # calling my already defined function my_new()
      9 print my_new()
---> 10 print a

NameError: name 'a' is not defined

In [10]:
# locals()
# locals is a inbuild function and it shows local variables.

def my_new():
    a = 10
    print locals() # {'a':10}
    return a

In [13]:
print my_new()


{'a': 10}
10

In [20]:
print globals()


{'_dh': [u'/home/tcloudost/Documents/git_repositories/python-batches/batch-57/functions'], '__': '', 'my_func': <function my_func at 0x7f67b55b9f50>, '__builtin__': <module '__builtin__' (built-in)>, '_iii': u'# scope resolution\n# first look into locally and then globally.\n# global variables are those values which are available both inside and outside the function.\n\ny = 10 # global variable.\n\nprint globals()\n\ndef my_new1():\n    print locals() # {}\n    return y', 'quit': <IPython.core.autocall.ZMQExitAutocall object at 0x7f67b6e86290>, '_i9': u'print my_new()', '_i8': u'# namespaces\n# local scope/variables/namespaces vs global scope/variables/namespaces.\n\ndef my_new():\n    a = 10\n    return a\n\n# calling my already defined function my_new()\nprint my_new()\nprint a', '_i7': u'# namespaces\n# local scope/variables/namespaces vs global scope/variables/namespaces.\n\ndef my_new():\n    a = 10\n    return a\n\n# calling my already defined function my_new()\nprint my_new()', '_i6': u'print my_func()', '_i5': u'def my_func():\n    return "hello world"\n    print "hello1"\n    print "hello2"\n    print "hello3"', '_i4': u'print my_func()', '_i3': u'def my_func():\n    return "hello world"', '_i2': u'print my_func()', '_i1': u'def my_func():\n    print "hello world"', 'exit': <IPython.core.autocall.ZMQExitAutocall object at 0x7f67b6e86290>, 'get_ipython': <bound method ZMQInteractiveShell.get_ipython of <ipykernel.zmqshell.ZMQInteractiveShell object at 0x7f67bfae8950>>, '_i': u'# scope resolution\n# first look into locally and then globally.\n# global variables are those values which are available both inside and outside the function.\n\ny = 10 # global variable.\n\n\n\ndef my_new1():\n    print locals() # {}\n    return y', 'my_new1': <function my_new1 at 0x7f67b55b9e60>, '_i14': u'# scope resolution\n\ny = 10\n\ndef my_new1():\n    print locals()\n    return y', '__doc__': 'Automatically created module for IPython interactive environment', '_i20': u'print globals()', 'my_new': <function my_new at 0x7f67b4d32f50>, '_ii': u'print my_new1()\nprint y', '__builtins__': <module '__builtin__' (built-in)>, 'In': ['', u'def my_func():\n    print "hello world"', u'print my_func()', u'def my_func():\n    return "hello world"', u'print my_func()', u'def my_func():\n    return "hello world"\n    print "hello1"\n    print "hello2"\n    print "hello3"', u'print my_func()', u'# namespaces\n# local scope/variables/namespaces vs global scope/variables/namespaces.\n\ndef my_new():\n    a = 10\n    return a\n\n# calling my already defined function my_new()\nprint my_new()', u'# namespaces\n# local scope/variables/namespaces vs global scope/variables/namespaces.\n\ndef my_new():\n    a = 10\n    return a\n\n# calling my already defined function my_new()\nprint my_new()\nprint a', u'print my_new()', u'# locals()\n# locals is a inbuild function and it shows local variables.\n\ndef my_new():\n    a = 10\n    print locals()\n    return a', u'print my_new()', u'print my_new()\nprint locals()', u'print my_new()', u'# scope resolution\n\ny = 10\n\ndef my_new1():\n    print locals()\n    return y', u'print my_new1()', u'print my_new1()\nprint y', u'# scope resolution\n# first look into locally and then globally.\n# global variables are those values which are available both inside and outside the function.\n\ny = 10 # global variable.\n\nprint globals()\n\ndef my_new1():\n    print locals() # {}\n    return y', u'print my_new1()\nprint y', u'# scope resolution\n# first look into locally and then globally.\n# global variables are those values which are available both inside and outside the function.\n\ny = 10 # global variable.\n\n\n\ndef my_new1():\n    print locals() # {}\n    return y', u'print globals()'], '__name__': '__main__', '___': '', '_': '', '_sh': <module 'IPython.core.shadowns' from '/usr/local/lib/python2.7/dist-packages/IPython/core/shadowns.pyc'>, '_i13': u'print my_new()', '_i12': u'print my_new()\nprint locals()', '_i11': u'print my_new()', '_i10': u'# locals()\n# locals is a inbuild function and it shows local variables.\n\ndef my_new():\n    a = 10\n    print locals()\n    return a', '_i17': u'# scope resolution\n# first look into locally and then globally.\n# global variables are those values which are available both inside and outside the function.\n\ny = 10 # global variable.\n\nprint globals()\n\ndef my_new1():\n    print locals() # {}\n    return y', '_i16': u'print my_new1()\nprint y', '_i15': u'print my_new1()', '_ih': ['', u'def my_func():\n    print "hello world"', u'print my_func()', u'def my_func():\n    return "hello world"', u'print my_func()', u'def my_func():\n    return "hello world"\n    print "hello1"\n    print "hello2"\n    print "hello3"', u'print my_func()', u'# namespaces\n# local scope/variables/namespaces vs global scope/variables/namespaces.\n\ndef my_new():\n    a = 10\n    return a\n\n# calling my already defined function my_new()\nprint my_new()', u'# namespaces\n# local scope/variables/namespaces vs global scope/variables/namespaces.\n\ndef my_new():\n    a = 10\n    return a\n\n# calling my already defined function my_new()\nprint my_new()\nprint a', u'print my_new()', u'# locals()\n# locals is a inbuild function and it shows local variables.\n\ndef my_new():\n    a = 10\n    print locals()\n    return a', u'print my_new()', u'print my_new()\nprint locals()', u'print my_new()', u'# scope resolution\n\ny = 10\n\ndef my_new1():\n    print locals()\n    return y', u'print my_new1()', u'print my_new1()\nprint y', u'# scope resolution\n# first look into locally and then globally.\n# global variables are those values which are available both inside and outside the function.\n\ny = 10 # global variable.\n\nprint globals()\n\ndef my_new1():\n    print locals() # {}\n    return y', u'print my_new1()\nprint y', u'# scope resolution\n# first look into locally and then globally.\n# global variables are those values which are available both inside and outside the function.\n\ny = 10 # global variable.\n\n\n\ndef my_new1():\n    print locals() # {}\n    return y', u'print globals()'], 'y': 10, '_i19': u'# scope resolution\n# first look into locally and then globally.\n# global variables are those values which are available both inside and outside the function.\n\ny = 10 # global variable.\n\n\n\ndef my_new1():\n    print locals() # {}\n    return y', '_i18': u'print my_new1()\nprint y', '_oh': {}, 'Out': {}}

In [19]:
# scope resolution
# first look into locally and then globally.
# global variables are those values which are available both inside and outside the function.

y = 10 # global variable.

def my_new1():
    print locals() # {}
    return y

In [18]:
print my_new1()
print y


{}
10
10
tcloudost@tcloudost-VirtualBox ~/Documents/git_repositories/python-batches/batch-57/functions $ python Python 2.7.6 (default, Jun 22 2015, 17:58:13) [GCC 4.8.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> print globals() {'__builtins__': , '__name__': '__main__', '__doc__': None, '__package__': None} >>> >>> a = 10 >>> print globals() {'__builtins__': , '__name__': '__main__', '__doc__': None, 'a': 10, '__package__': None} >>> >>> def f(): ... y = 30 ... return y ... >>> >>> print globals() {'a': 10, 'f': , '__builtins__': , '__package__': None, '__name__': '__main__', '__doc__': None} >>> print y Traceback (most recent call last): File "", line 1, in NameError: name 'y' is not defined >>> print a 10

In [21]:
# global keyword
# banking

balance=0

def my_deposit():
    balance = balance + 1000
    return balance

def my_withdraw():
    balance = balance - 300
    return balance

In [22]:
print my_deposit()


---------------------------------------------------------------------------
UnboundLocalError                         Traceback (most recent call last)
<ipython-input-22-31fdef4eec56> in <module>()
      1 
----> 2 print my_deposit()

<ipython-input-21-6023e4c0521a> in my_deposit()
      5 
      6 def my_deposit():
----> 7     balance = balance + 1000
      8     return balance
      9 

UnboundLocalError: local variable 'balance' referenced before assignment

In [23]:
# hopefully this is the solution.

balance=0  # global

def my_deposit():
    balance=0    # local
    balance = balance + 1000
    return balance

def my_withdraw():
    balance=0    # local
    balance = balance - 300
    return balance

In [24]:
print my_deposit()


1000

In [25]:
print balance


0

In [26]:
print my_withdraw()
print balance


-300
0

In [27]:
# Lastly let reach out the solution.

balance=0  # global

def my_deposit():
    global balance
    print locals()
    balance = balance + 1000
    return balance

def my_withdraw():
    global balance
    print locals()
    balance = balance - 300
    return balance

In [28]:
print my_deposit()
print balance  # 1000


{}
1000
1000

In [29]:
print my_withdraw()
print balance


{}
700
700

In [32]:
# functional parameters
# position based parameters

def my_add(a,b):
    print locals()
    return a + b

print my_add('linux',' rocks')
print my_add(' rocks','linux')


{'a': 'linux', 'b': ' rocks'}
linux rocks
{'a': ' rocks', 'b': 'linux'}
 rockslinux

In [33]:
# passing parameters by keys
def my_add(a,b):
    print locals()
    return a + b

print my_add(b=' rocks',a='linux')


{'a': 'linux', 'b': ' rocks'}
linux rocks

In [35]:
# default parameters
# multiplication table of 2
# default is just a variable, its not a key word.
# please use variables of your choice.


def my_multi(num,default=10):
    for value in range(1,default+1):
        print "{} * {} = {}".format(num,value,num*value)

In [36]:
my_multi(2)


2 * 1 = 2
2 * 2 = 4
2 * 3 = 6
2 * 4 = 8
2 * 5 = 10
2 * 6 = 12
2 * 7 = 14
2 * 8 = 16
2 * 9 = 18
2 * 10 = 20

In [37]:
my_multi(2,5)


2 * 1 = 2
2 * 2 = 4
2 * 3 = 6
2 * 4 = 8
2 * 5 = 10

In [ ]:
# putty - http://cache.filehippo.com/img/ex/1125__putty1.png
# def putty(hostname,port=22)

# putty(mynewip)    # connects on ssh port
# putty(mynewip,23) # connects on telnet port