# cacheSolve will take input as matrix parameter x for which inverse calculation need to be done.
cacheSolve<-function(x) {
# I am caching x matrix in Global environment variable z. When first time cacheSolve will be called, z will not be
# be available in Gobal environment, so search of "z" in Global enviornment will return zero length numeric type vector.
# If z is not available in global enviormment, initialize z as matrix with no data.
if(length(grep("z",ls(envir=.GlobalEnv)))==0) # Search z in Global environment.
{
z<-matrix() #Initialize z as matrix with no data.
}
if(!is.na(z[1][1])) # Check z[1][1] is not NA. When first time cacheSolve will be called z[1][1] will be NA.
{
if(!is.null(x) & identical(x,z)) {
print("getting cached data") # if input matrix is not null and x and already cached z is identical then take output from cache.
return(y) # y is inverse of x and saved in Golbal enviornment variable. So, if same x matrix get called cached y will displayed and cacheSolve function will return from here.
}
}
# if either cacheSolve function is called first time or x is either null or not identical to cached z then call makecacheMatrix
makeCacheMatrix(x) # pass x matrix as input to makeCacheMatrix
y# display global environment variable y.
}
################################### End cacheSolve #####################################################################################################################