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.