Functions Using Python

Introduction 

 
Functions are the piece of code that can be reused within an application at several places. The best benefit of using functions is their reusability. While defining a function, we have to define it only once and we will get different values and different outputs for different places. Functions reduce the size of a program.
 
Different types of function parameters are,
  1. With no parameters, 
  2. With one parameter, 
  3. With two parameters and so on... We can use n number of parameters.
Now, we will see how to write functions in Python.
 
Note
 
For different Python IDEs, we have different ways to execute, however, the result remains the same. For those who are using Visual Studio IDE for Python, the process will be something as follows.
  1. def display(): #function with no arguments    
  2.     print("This is an function with no arguments")    
  3.     return   
  4.    
  5. display()    
  6.   
  7. def callfun():    
  8.     print("This is calling a function into another function")    
  9.     display() #calling display() function into call() function    
  10.     return    
  11.   
  12. callfun()    
  13.   
  14. def message(name): #function with one argument    
  15.     print("Hello, "+name)    
  16.     return    
  17.   
  18. message("Kartik")    
  19. message("Sai Vinayak")    
  20.   
  21. def message(greeting,name): #function with two arguments    
  22.     return greeting+", "+name    
  23.   
  24. msg=message("Hello","Shiv"#message values assigning to msg variable    
  25. show=message("Hi","Rahul"#another message values assigning to show variable    
  26. print(msg)    
  27. print(show)    
  28.   
  29. def add(a,b): #defining a function that calculates the sum of two numbers    
  30.     c=int(a)+int(b) #converting two values into integer type    
  31.     print(c)    
  32.     return  
  33.     
  34. add(5,2)     
The code will be like this.
 
 
Then, the output of the program after execution will be like this.
 
 
This is how we can use functions in Python.