# # Unit 6 - User-functions # # Basics # # The main novelty - conceptual. # What does these codes do: # 1 counter <- 0 vec <- c(3, 4, 5, 999, 999) for (val in vec) { counter <- counter + 1 } print(counter) length(vec) # 2 vec <- c(3, 5, 2, 2, 1) uniqueVec <- c() for (val in vec) { if (!val %in% uniqueVec) { uniqueVec <- c(uniqueVec, val) } } print(uniqueVec) unique(vec) # By now you know several functions. # What do you really know about them? # - Input, operation, output # - We are not interested in their actual code. # - We are interested in not repeating their code! sample(1:10, size = 1) length(vec) sort unique seq rep ls # Three kinds of functions # - functions which are at the language's base # - functions which maybe should be imported # - functions we write ourselves # Basic structure and grammar # Without return. myFunc <- function() { print("hello world") } # The execution starts here! # Some possible code print("Beginning") # function call myFunc() # Some possible code print("End") # Parameters and arguments # one parameter myFunc <- function(x) { print(x) } myFunc(3) print("END") sample <- function(x, size, ... ) sample(x = 1:10, size = 1) myFunc <- function(x) { z <- x + 1 print(z) } myFunc(3) print("END") # dynamic typing myFunc <- function(x) { print(x + 3) } myFunc(3) myFunc("Kolargol") myFunc(c(5, 3, 2)) # More than one parameter: # correspondence and assignment by order. myFunc <- function(x, y) { print(x) print(y) } myFunc(3, 6) # What would this function call print? myFunc(6) # And this? myFunc <- function() { print("Bamba") } myFunc(3) myFunc <- function(x, y) { a <- x + 6 print(a) print(y) } myFunc(3, "A") myFunc("A", 3) # Correspondence and assignment by # explicit statement of the parameters. # base-function print(rep(x = 3, times = 6)) print(rep(3, 6)) # user-function myFunc <- function(x, y) { a <- x + 6 print(a) print(y) } myFunc(y = "A", x = 3) # Basic structure; with return. # Note the parenthesis. myFunc <- function(x) { print("I'm here!") return (x + 3) } # calling, assigning, and printing newResult <- myFunc(9) print(newResult) # without assignment print(myFunc(9)) # Problem: # Write the function myFind(). # The function has two parameters: # - num - a number # - vec - a numeric vector # The function returns the index of # num's first appearance in vec, # or -1 if num does not appear in vec. myFind <- function(num, vec) { for (ind in 1:length(vec)) { if (vec[ind] == num) { return (ind) } } return (-1) } myVec <- c(3, 5, 4, 2, 3, 2) myNum <- 99 print(myFind(myNum, myVec)) # function code following an executed return # is not executed myFunc <- function(x) { return (x+1) print("So sad...") } print(myFunc(1)) # Scopes # A function can access a variable # defined outside of it. myFunc <- function() { return (x) } # Execute this step by step, # inspecting the Encironment. x <- 7 result <- myFunc() print(result) # If it defines a variable # by the same name, it uses # this variable myFunc <- function() { x <- 5 return (x) } x <- 7 result <- myFunc() print(result) # A variable defined in a function # cannot be accessed from outside. myFunc <- function() { z <- 5 return (z) } result <- myFunc() print(result) print(z) # The main novelty of this lesson is conceptual. library(plots) library(lsr) # The General structure of our programs # from now own func1 <- function() ... func2 <- function() ... func3 <- function() ... code ... func1() code ... func2() code ... func3() code ...