How to write/use a function within a function?

Hello,

I am trying to create a function that tells me what the value of an investment would be if I invested at a certain time. So I first create a function to calculate the percent change in the asset between the two time points -- the current price of the stock/asset and the old price of the asset that it was trading at. Just a simple percent change calculator.

Percent_Change <- function(current, old){
  
  numerator <- current - old
  denominator <- old
  percent_change <- numerator/denominator
  return(percent_change)
}

The next function I want to create is the actual calculator that calculates and returns the value that your investment would be if you threw in a certain amount, and this function depends on the percent change function above. Here is what I have for that:

Amount_if_Invested <- function(initial_investment_amount, Percent_Change(new, old)){
  
  percent_change <- Percent_Change(new, old)
  multiplier <-  1 + percent_change
  
  the_ current_investment_value <- initial_investment_amount*multiplier
  
  return(the_current_investment_value)
}

To be clear, I know I could just define some function like

Amount_if_Invested <- function(initial_investment_amount, current_price, older_price){

  numerator <- current_price - older_price
  denominator <- older_price
  percent_change <- numerator/denominator
  multiplier <-  1 + percent_change
  
  investment_value <- initial_investment_amount*multiplier
  return(investment_value)
}

But the goal is to get experience and understand how R works when defining functions that have inputs as other functions, you know? My problem is, as is, the function doesn't work. When I try to run/use this function defined above that calls my percent change function, I get this as output:

I know it's kind of gross, but I'm just trying to get better at programming in general and more experience with things. Is this something that happens, or should doing something like this be avoided like the black plague? I've heard of things called Modular Programming and Dynamic Programming and I thought they did things like this. Plus, perhaps I just want a function to calculate percent change independently of calculating the value of an investment, you know? Thanks for taking the time to read my post.

Just as arguments put initial_investment_amount, new, old.

Amount_if_Invested <- function(initial_investment_amount, new, old){
  percent_change <- Percent_Change(new, old)
  multiplier <-  1 + percent_change
  the_current_investment_value <- initial_investment_amount*multiplier
  return(the_current_investment_value)
}

Amount_if_Invested(10, 3, 2)
[1] 15

Grzegorz

Here are a couple of other options. You can define Amount_if_invested to take an argument that is itself a function and also pass in the arguments for that function. In method 1, I fix the number of arguments to two for the passed-in function. In Method 2, I use the ... argument to capture all additional arguments and use them for the passed-in function.

Percent_Change <- function(current, old){
  
  numerator <- current - old
  denominator <- old
  percent_change <- numerator/denominator
  return(percent_change)
}
#method 1
Amount_if_Invested <- function(initial_investment_amount, FunctionToUse, arg1, arg2){
  
  percent_change <- FunctionToUse(arg1, arg2)
  multiplier <-  1 + percent_change
  
  the_current_investment_value <- initial_investment_amount*multiplier
  
  return(the_current_investment_value)
}
Amount_if_Invested(1000, Percent_Change, 110, 100)
#> [1] 1100

#method 2
Amount_if_Invested2 <- function(initial_investment_amount, FunctionToUse, ...) {
  percent_change <- FunctionToUse(...)
  multiplier <-  1 + percent_change
  
  the_current_investment_value <- initial_investment_amount*multiplier
  
  return(the_current_investment_value)
}
Amount_if_Invested2(1000, Percent_Change, 120, 100)
#> [1] 1200

Created on 2022-02-04 by the reprex package (v2.0.1)

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.