abc <- function ( a_in = NULL, b_in = NULL, c_in = NULL ) #*****************************************************************************80 # ## abc() stores, saves, and returns varables "a", "b" and "c". # # Discussion: # # Calling abc() with no input arguments returns the current # values of A, B, and C. # # Calling abc(a_in,b_in,c_in) supplies new values for A, B, # and C which overwrite the current values. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 11 May 2021 # # Author: # # John Burkardt # # Input: # # real A_IN: a new value for A. # # real B_IN: a new value for B. # # real C_IN: a new value for C. # # Persistent: # # real A_DEFAULT: the current value of A. # # real B_DEFAULT: the current value of B. # # real C_DEFAULT: the current value of C. # # Output: # # real A_OUT: the current value of A. # # real B_OUT: the current value of B. # # real C_OUT: the current value or C. # { # # Persistent data is stored as attributes associated with this function. # a_default <- attr ( abc, "a_default" ) b_default <- attr ( abc, "b_default" ) c_default <- attr ( abc, "c_default" ) # # Initialize the persistent data once. # Note that we need to use a DOUBLE ARROW assignment here! # This makes the assignment "globally". # if ( is.null ( a_default ) ) { a_default = 1.0 attr ( abc, "a_default" ) <<- a_default } if ( is.null ( b_default ) ) { b_default = 2.0 attr ( abc, "b_default" ) <<- b_default } if ( is.null ( c_default ) ) { c_default = 3.0 attr ( abc, "c_default" ) <<- c_default } # # New values, if supplied on input, overwrite the current values. # Note that we need to use a DOUBLE ARROW assignment here! # This makes the assignment "globally". # if ( ! is.null ( a_in ) ) { a_default = a_in attr ( abc, "a_default" ) <<- a_default } if ( ! is.null ( b_in ) ) { b_default = b_in attr ( abc, "b_default" ) <<- b_default } if ( ! is.null ( c_in ) ) { c_default = c_in attr ( abc, "c_default" ) <<- c_default } # # The current values are copied to the output. # a_out = a_default b_out = b_default c_out = c_default output = c ( a_out, b_out, c_out ) return ( output ) }