Showing posts with label python functions. Show all posts
Showing posts with label python functions. Show all posts

Thursday, May 12, 2016

Default Argument - Functions

I have been talking about functions a lot more because function is the most essential part of any high-level programming language. Here we will discuss how can we make functions to do different things depending upon different arguments passed. Take a look at the below example  -

def summ(a=2,b=2):
    c=a+b
    print "Value :%s"%str(c)
   
summ(8,2)
summ(3)
summ()

summ(a=5,b=-3)

Here , The first call to the function summ will add the number 8,2 and print result 10, Then in the next one we have passed only one argument , so the function will skip the first default argument and set a to 3, then it get the default value for b, and add them . So the result will be 5. For the next call to the function , the function will use both the default value and give output 4.  You can also pass a value to function by specifying the variable name( here we have assigned b to -3 and passed).
So by having default value assigned in function declaration we can have sophisticated code with no rigidity.


Feautured Post

Python Trivia Post: enumerate()

Python has a very useful inbuilt function, which is called enumerate(). This is needed whenever you need the numbering of a certain element...