1 of 9

Class 15

Functions

2 of 9

AGENDA

Today’s Class …

  • Functions

During Class …

  • Function and control structure activity

Next Class …

3 of 9

Overview

  • Often, when working on a project, you will find yourself doing the same thing over and over again
  • Functions allow you to automatic common tasks
  • If you are copy and pasting code more than twice, you might want to use a function

Class 15

3

4 of 9

Julia’s Examples

Class 15

4

5 of 9

Julia’s Examples

Class 15

5

6 of 9

Benefits of Functions

  • More organized, more understandable code
  • Less errors
  • Increased productivity over time (you can reuse functions and if you need to make a change you only need to make a change in one place)

Class 15

6

7 of 9

Parts of a function

name

The arguments- The arguments are things that vary across calls

The body- The body is the code that’s repeated across all the calls.

Class 15

7

8 of 9

Parts of a function

name <- function(arguments)

{ body

}

Class 15

8

9 of 9

Function Example

Class 15

9

# function to add 2 numbers

add_num <- function(a,b)

{

  sum_result <- a+b

  return(sum_result)

  }

# calling add_num function

 sum = add_num(35,34)

#printing result

print(sum)