Untitled Document
Functions
1. Program - To Demo a Simple Function Click Here
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # Program To Demo a Simple Function 
       # a function is declared in python using def keyword
       
       # Ex-1 Simple Function ; a function is declared in python using def keyword
       def hello_func():
       	pass # Since we cannot keep function body blank we have used pass keyword
       hello_func()
       print(hello_func())

       
2. Program - To Print statement within function Click Here
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # Program - To Print  statement within function
       
       def hello_func():
       	print("Hello Function !!")
       hello_func()
       
3. Program - To return value(s) from a function Click Here
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # Program - To return value(s) from a function
       # when we put return keyword in function body it returns value to caller ; 
       # now caller either need to store that in avriable or it can be printed on screeen
       
       def hello_func():
       	return 'Hello Function'
       
       print(hello_func())
       
       # OR Another Approach
       
       def hello_func():
       	return 'Hello Function'
       
       val = hello_func()
       print(val)
       
4. Program - To Demo function chaining Click Here
       #!/bin/ksh 
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # Program - To Demo function chaining 
       # function chaining means passing an o/p of function to another function ; here is the simple example
       # he below code converts the string into Upper case 
       
       def hello_func():
       	return 'Hello Function'
       
       print(hello_func().upper())
       
5. Program - passing Argument to a function Click Here
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # Program - passing Argument to a function
       # Ex -1 In below example we have greeting varibale in function declaration ; where we will pass argument whicl calling 
       
       def hello_func(greeting):
       	return '{} Mangesh !! '.format(greeting)
       
       print(hello_func("Hello"))
       
       # Ex - 2 Passing default argument to a function ; please run below example and try to undertsnad the output
       
       def hello_func(greeting , name='You'):
       	return '{},  {}'.format(greeting, name)
       
       print(hello_func ('Hi'))       
       print(hello_func('Hi', "Mangesh "))
       
       # Ex - 3 Pssing multiple and key value pairs as a argument to a function
       # here *args and **kwargs are used for unpacking values passed to afunction
       
       def student_info(*args, **kwargs):
       	print(args)
       	print(kwargs)
       
       student_info('Math','Art', name="Mangesh", age=22)   
       
       # Ex -4 Passing List and Dictionaries as a argument to a function 
       # IMP - while passing  list amd dictionary to function we need to unapck it by adding * and **for dictionary 
       
       def student_info(*args, **kwargs):
       	print(args)
       	print(kwargs)
     
       courses = ['Math', 'Art']
       info = {'name': 'Mangesh', 'age':22}
       
       student_info(*courses, **info)
       
Untitled Document