19 Writing Function in R

Simple function R ... with no return value.

say.hello <- function(name){

print(sprintf("Hello %s!", name))

}

say.hello("World")

Call function with argument name

print.person <- function(name, age, city){

print(sprintf("%s %d %s", name, age, city))

}

print.person(age = 30, name = "Sam", city = "NY")

Order of the variable does not matter if you pass the argument value by name like the example above

Default value of argument

show.time <- function(tz = ""){

now = Sys.time()

if(tz == ""){

print(now)

}else{

print(format(now, tz = tz))

}

}

show.time("GMT")

show.time()

Return value

gettime <- function(){

return Sys.time()

}