Programming Example
Function with three dot ... parameter in R programming language
Function with three dot ... parameter in R programming language
# function argument as a three dot ...
hello.person <- function(firstName, lastName = "Ansari", ...)
{
print(sprintf("Hello %s %s", firstName, lastName))
}
# Azmi will ignore because of the ...
hello.person("Rumman", "Ansari", "Azmi")
# Azmi will ignore because of the ...
hello.person("Rumman", extra = "Azmi")
# another way to call
hello.person("Rumman")
hello.person(firstName = "Rumman")
hello.person(firstName = "Ansari", "Azmi")
# this function calling will not work
# because "firstName" is missing
hello.person(lastName = "Rumman")
> # Azmi will ignore because of the ...
> hello.person("Rumman", "Ansari", "Azmi")
[1] "Hello Rumman Ansari"
>
> # Azmi will ignore because of the ...
> hello.person("Rumman", extra = "Azmi")
[1] "Hello Rumman Ansari"
>
> # another way to call
> 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
> # because "firstName" is missing
> hello.person(lastName = "Rumman")
Error in sprintf("Hello %s %s", firstName, lastName) :
argument "firstName" is missing, with no default
First read the algorithm, then study the program code line by line. After that, compare the code with the output and finally go through the explanation. This approach helps learners understand both the logic and the implementation properly.
After understanding this example, try to rewrite the same program without looking at the code. Then change some values or logic and run it again. This helps improve confidence and keeps learners engaged on the page for longer.