Generic functions

When a function behaves differently in different situation, then it is called generic function. For example, consider the case of print() and summary() function.

  1. Print and summary functions on vectors.
a<-c(1,2,3)
print(a) # In this case print is used for dispalying vectors
## [1] 1 2 3
summary(a) # Summary contains min, max etc...
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##     1.0     1.5     2.0     2.0     2.5     3.0
  1. Print and summary functions on lists.
b<-list(name="Pradeep", age=32,food=c("Idly","Dosa"))
print(b) # Print is used to display lists
## $name
## [1] "Pradeep"
## 
## $age
## [1] 32
## 
## $food
## [1] "Idly" "Dosa"
summary(b)# Summary contains length, mode etc..
##      Length Class  Mode     
## name 1      -none- character
## age  1      -none- numeric  
## food 2      -none- character

So, print and summary are generic funtions used in different ways. There are many more generic functions in R. Like plot