44  44. return vs cat

We covered this earlier. However, studnets who are new to R are often confused about these ideas …

#..................................................................
# *** TL;DR *** (i.e. the main idea) ...
# (keep reading the comments below for a more lengthy explanation): 
#..................................................................
#
#     The countdown function does NOT return the numbers
#     nor the word "blastoff!". Rather the return value of countdown() is the 
#     value of cat("blastoff!") function, which is invisible, and is NULL.

# Same code as above
countdown <- function (){
  for (num in 10:1){  # each time through the loop another value from 10:1 is assigned to num
    cat(num," ")      # display num followed by a space
    Sys.sleep(0.25)   # sleep for 0.25 seconds
  }
  
  cat("blastoff!")    # after the loop has finished, display "blastoff!"
}
countdown()  # 10 9 8 7 6 5 4 3 2 1 blastoff!
10  9  8  7  6  5  4  3  2  1  blastoff!
x <- countdown()
10  9  8  7  6  5  4  3  2  1  blastoff!
x # NULL
NULL
#..................................................................
# *** A SOMEWHAT MORE LENGTHY EXPLANATION OF THESE CONCEPTS ***
#..................................................................
# First of all, remember that if a function doesn't execute an explicit
# return statement, then the value of the final command to be executed is returned
# from the function.
#
# Therefore, in the case of the countdown function above, what is "returned" is the 
# value of cat("blastoff!") ... (but this is NOT the word "blastoff!" ... keep reading).

# The return value of cat is always NULL.
# In the next line the return value is NULL, NOT "hello".  
# The return value, NULL, is then assigned to the variable x.
# Even though the word "hello" is NOT the return value,
# nevertheless, the word hello (without quotes) is displayed because cat 
# will always display info to the screen.
x <- cat("hello")  # displays hello (without quotes) to the screen
hello
x # NULL
NULL
# One more example ...
# Remember, the rep function repeats the VALUE of the first argument.
rep("hello", 5)   # "hello" "hello" "hello" "hello" "hello"
[1] "hello" "hello" "hello" "hello" "hello"
rep(seq(1,3), 5) # 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 
 [1] 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3
# In the following example, the return value of cat is NULL.
# However, NULL is NOT repeated 5 times, only once.
# This is due to the special nature of the value NULL (which basically
# means "nothing"). NULL is special in that even 5 NULLs are displayed
# as a single NULL - don't worry too much about the intricacies of NULL,
# but you must know that the return value of cat is NOT "hello".

rep(cat("hello"), 5) # helloNULL
hello
NULL
rep(NULL, 5) # NULL  (only once)
NULL
# Bottom line: You can use cat to display info to the screen
# but don't expect that information to be "returned" from the function.


# SOME TAKEAWAYS:
# REMEMBER: 
#
# A few things to remember ...
#
#  1. If a function doesn't contain a return statement, then the 
#     value of the final command to be executed in the function
#     is returned.
#
#  2. Based on #1 above, the value that is returned from countdown()
#     is the value cat("blastoff!"). HOWEVER ... keep reading ...
#
#  3. It is very important to understand that the cat function 
#     does NOT "return" the information that is displayed. 
# 
#     The purpose of cat is TO DISPLAY INFORMATION TO THE SCREEN.
#     The purpose of cat is NOT to return a value.
#     Programmers use the cat function to display information,
#     NOT to generate a "value".The actual return value of cat
#     is NULL. You never really need to use this NULL value,
#     but technically, NULL is the return value. 
#
#  4. You don't actually see the word NULL on the screen when you 
#     run the countdown function since the return value of cat is 
#     an "invisible" value - we explained the concept of invisible
#     return values earlier in the semester when we first discussed
#     the cat function.)

© 2025 Y. Rosenthal. All rights reserved.