# future value function with default arguments
= function(pv = 100, r = 0.01, n = 1) {
FV = pv * (1 + r)^n
fv
fv
}
# Execution
FV()
#> [1] 101
=200
pvFV(pv)
#> [1] 202
Functions and Expressions
Reading Assignments
Make sure to read the following sections in the textbook: R Coding Basics, https://www.gastonsanchez.com/R-coding-basics/
Intro to Functions
- Three components of a function:
- body (= the lines of code with in braces)
- arguments (= inputs of your function)
- environment \(\Rightarrow\) Global Environment vs Execution Environment
Example 10.2.1
Expressions
Simple Expression vs Compound Expression
- Simple expression are written with no braces (normally in one single line!).
Simple Expression Examples:
# Every line below is a simple expression
= 1000
deposit
deposit#> [1] 1000
= 0.02
rate
rate#> [1] 0.02
= 3
year
year#> [1] 3
- Compound expressions consist of simple expressions separated by newlines (or semicolons), and most importantly, grouped within braces.
Compound Expression Examples:
{= 1000
deposit
deposit= 0.02
rate
rate= 3
year
year
}#> [1] 3
# Equivalently,
{= 1000; deposit; rate = 0.02; rate; year = 3; year
deposit
}#> [1] 3
When to use compound expression?
Compound expression is typically used together with other programming structures (e.g. functions, conditionals, loops).
Example:
= function(pv = 100, r = 0.01, n = 1) {
FV = pv * (1 + r)^n
fv
fv }
= function(x) {
calc_powers = x ^ 0
zero = x ^ 1
one = x ^ 2
two = x ^ 3
three c(zero, one, two, three)
}
How compound expression is run?
- R runs everything inside of the compound expression as a single block of code.
- Every expression in R has a value: the value of the last evaluated statement.
<- {
x = 1000
deposit
deposit= 0.02
rate
rate= 3
year
year
}
x#> [1] 3
Example: A function with compound expressions
= function(x) {
calc_powers = x ^ 0
zero = x ^ 1
one = x ^ 2
two = x ^ 3
three c(zero, one, two, three)
# OR, return(c(zero, one, two, three))
}calc_powers(5)
#> [1] 1 5 25 125
The value of a function can be established in two ways:
- As the last evaluated simple expression (in the body of the function)
- An explicitly returned value via
return()
Guess the output of the following codes:
<- calc_powers(5)
RESULT
RESULT
zero
one
two three
Results
<- calc_powers(5)
RESULT
RESULT#> [1] 1 5 25 125
# Function's local variables cannot be accessed by the global environment
zero #> Error in eval(expr, envir, enclos): object 'zero' not found
# local variable
one #> Error in eval(expr, envir, enclos): object 'one' not found
# local variable
two #> Error in eval(expr, envir, enclos): object 'two' not found
# local variable
three #> Error in eval(expr, envir, enclos): object 'three' not found
Revisit Example 10.2.1
# title: future value function
# description: computes future value using compounding interest
# inputs:
# - present: amount for present value
# - rate: annual rate of return (in decimal)
# - years: number of years
# output:
# - computed future value
# future value function with default arguments
= function(pv = 100, r = 0.01, n = 1) {
FV = pv * (1 + r)^n
fv return(fv) # It's recommended to use return()
}
# Execution
FV() # Default Execution using pv = 100, r = 0.01, n = 1
#> [1] 101
=200
pvFV(pv) # pv = 200, r = 0.01, n = 1
#> [1] 202
- Steps to write a function:
- Step 1: Start with a concrete example
- Step 2: Make your code more generalizable
- Step 3: Encapsulate the code into a function
- Step 4: Test that the function works
The more complicated the function you write, the more crucial it is to follow the steps.