problem with the return()

Hi all

Hope everyone is well. I have an embarrassingly easy question here, in that I can't figure out how to use the return() correctly in my dead simple example. Say I have a dummy R script called dummy.R saved in the local directory with

dummy<-function(a,b){
result=a+b
return(result)
}

However, in my Rstudio, when I call this function, I just don't see result object appearing in my global environment at all.

Can anyone enlighten me as to what am I neglecting or doing wrong?

Kind regards

John

The function will not make an object in the global environment, it will simply return the value of result. You need to store that value in an object.

NewObj <- dummy(3,4)

How are you calling the function? If you run it with two arguments in the RStudio console, do you not get any return statement?
Also, note that if you want to show the return value while assigning the value to an object, use parentheses.

dummy = function(a,b) {
   result=a+b
   return(result)
}

dummy(5, 6)
#> [1] 11

(x = dummy(5, 6))
#> [1] 11

x
#> [1] 11

Created on 2022-07-16 by the reprex package (v2.0.1)

Thanks for the reply guys. I understand how to properly use the return function now. Your comments were tremendously useful.

This topic was automatically closed 21 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.