10 Function

  • R uses the keyword function to create a function
  • Python uses the keyword def to create a function
  • Both copy the objects as arguments to the function
  • Both evaluates the function as lazy evaluation

10.1 R: function

fn_odd_even = function(x){
  
    if (x %% 2 == 0){
        msg = paste0('The number ', x, ' is EVEN')
    } else {
        msg = paste0('The number ', x, ' is ODD')
    }

    return(msg)
  
}


x = 15

fn_odd_even(x)

[1] “The number 15 is ODD”

10.2 Python: def


def fn_odd_even(x):
  
  if x % 2 == 0:
    msg = 'The number ' + str(x) + ' is EVEN'
  else:
    msg = 'The number ' + str(x) + ' is ODD'
  
  return(msg)


x = 15

print(fn_odd_even(x))

The number 15 is ODD