0% found this document useful (0 votes)
22 views

Function Argument

The document discusses function arguments and return values in R. It demonstrates how to define functions that take arguments, set default values for arguments, and return values. Functions are defined using the function keyword and curly braces, and arguments are specified within the parentheses. The last line of a function is automatically returned, or the return command can be used to explicitly return a value.

Uploaded by

Elangovan M
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Function Argument

The document discusses function arguments and return values in R. It demonstrates how to define functions that take arguments, set default values for arguments, and return values. Functions are defined using the function keyword and curly braces, and arguments are specified within the parentheses. The last line of a function is automatically returned, or the return command can be used to explicitly return a value.

Uploaded by

Elangovan M
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

112 Chapter 8 Writing R functions

8.2 Function Arguments


More often than not we want to pass arguments to our function. These are easily added
inside the parentheses of the function declaration. We will use an argument to print
“Hello Jared.”
Before we do that, however, we need to briefly learn about the sprintf function. Its
first argument is a string with special input characters and subsequent arguments that will
be substituted into the special input characters.

> # one substitution


> sprintf("Hello %s", "Jared")

[1] "Hello Jared"

> # two substitutions


> sprintf("Hello %s, today is %s", "Jared", "Sunday")

[1] "Hello Jared, today is Sunday"

We now use sprintf to build a string to print based on a function’s arguments.

> hello.person <- function(name)


+ {
+ print(sprintf("Hello %s", name))
+ }
> hello.person("Jared")

[1] "Hello Jared"

> hello.person("Bob")

[1] "Hello Bob"

> hello.person("Sarah")

[1] "Hello Sarah"

The argument name can be used as a variable inside the function (it does not exist
outside the function). It can also be used like any other variable and as an argument to
further function calls.
We can add a second argument to be printed as well. When calling functions with more
than one argument, there are two ways to specify which argument goes with which value,
either positionally or by name.

> hello.person <- function(first, last)


+ {
+ print(sprintf("Hello %s %s", first, last))
+ }
> # by position
> hello.person("Jared", "Lander")
8.2 Function Arguments 113

[1] "Hello Jared Lander"

> # by name
> hello.person(first="Jared", last="Lander")

[1] "Hello Jared Lander"

> # the other order


> hello.person(last="Lander", first="Jared")

[1] "Hello Jared Lander"

> # just specify one name


> hello.person("Jared", last="Lander")

[1] "Hello Jared Lander"

> # specify the other


> hello.person(first="Jared", "Lander")

[1] "Hello Jared Lander"

> # specify the second argument first


> # then provide the first argument with no name
> hello.person(last="Lander", "Jared")

[1] "Hello Jared Lander"

Being able to specify the arguments by name adds a lot of flexibility to calling functions.
Even partial argument names can be supplied, but this should be done with care.
> hello.person(fir="Jared", l="Lander")

[1] "Hello Jared Lander"

8.2.1 Default Arguments


When using multiple arguments it is sometimes desirable to not have to enter a value for
each. In other languages functions can be overloaded by defining the function mutliple
times, each with a differering number of arguments. R instead provides the capability to
specify default arguments. These can be NULL, characters, numbers or any valid R object.
Let’s rewrite hello.person to provide “Doe” as the default last name.
> hello.person <- function(first, last="Doe")
+ {
+ print(sprintf("Hello %s %s", first, last))
+ }
>
> # call without specifying last
> hello.person("Jared")

[1] "Hello Jared Doe"


114 Chapter 8 Writing R functions

> # call with a different last


> hello.person("Jared", "Lander")

[1] "Hello Jared Lander"

8.2.2 Extra Arguments


R offers a special operator that enables functions to take an arbitrary number of arguments
that do not need to be specified in the function definition. This is the dot-dot-dot
argument (…). This should be used very carefully, although it can provide great flexibility.
For now we will just see how it can absorb extra arguments; later we will find a use for it
when passing arguments between functions.
> # call hello.person with an extra argument
> hello.person("Jared", extra="Goodbye")

Error in hello.person("Jared", extra = "Goodbye"): unused argument (extra =


"Goodbye")

> # call it with two valid arguments and a third


> hello.person("Jared", "Lander", "Goodbye")

Error in hello.person("Jared", "Lander", "Goodbye"): unused argument ("Goodbye")

> # now build hello.person with ... so that it absorbs extra arguments
> hello.person <- function(first, last="Doe", ...)
+ {
+ print(sprintf("Hello %s %s", first, last))
+ }
> # call hello.person with an extra argument
> hello.person("Jared", extra="Goodbye")

[1] "Hello Jared Doe"

> # call it with two valid arguments and a third


> hello.person("Jared", "Lander", "Goodbye")

[1] "Hello Jared Lander"

8.3 Return Values


Functions are generally used for computing some value, so they need a mechanism to
supply that value back to the caller. This is called returning and is done quite easily. There
are two ways to accomplish this with R. The value of the last line of code in a function is
automatically returned, although this can be bad practice. The return command more
explicitly specifies that a value should be returned and the function should be exited.
To illustrate, we will build a function that doubles its only argument and returns
that value.
> # first build it without an explicit return
> double.num <- function(x)
+ {

You might also like