Write a function in R programming Language which will take parameter and the value will default value.

R Programming Language / Function in R Language

296

Program:

# R function

hello.person <- function(firstName, lastName = "Ansari")
{
  print(sprintf("Hello %s %s", firstName, lastName))
}

# different way of function calling
hello.person("Rumman")

hello.person(firstName = "Rumman")

hello.person(firstName = "Ansari", "Azmi")

# this function calling will not work
hello.person(lastName = "Rumman")

Output:

> hello.person("Rumman")
[1] "Hello Rumman Ansari"
> 
> hello.person(firstName = "Rumman")
[1] "Hello Rumman Ansari"
> 
> hello.person(firstName = "Ansari", "Azmi")
[1] "Hello Ansari Azmi"
> # this function calling will not work
> hello.person(lastName = "Rumman")
 Error in sprintf("Hello %s %s", firstName, lastName) : 
  argument "firstName" is missing, with no default

Explanation:

# this function calling will not work hello.person(lastName = "Rumman")

This Particular section is dedicated to Programs only. If you want learn more about R Programming Language. Then you can visit below links to get more depth on this subject.