0% found this document useful (0 votes)
2 views12 pages

Functions in R

The document covers the topic of writing functions in R programming, including how to call functions, use named arguments, set default values, and return values. It provides examples of function creation and usage, emphasizing the importance of functions in organizing and simplifying code. Key learning outcomes include understanding function syntax, argument passing, and return mechanisms.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views12 pages

Functions in R

The document covers the topic of writing functions in R programming, including how to call functions, use named arguments, set default values, and return values. It provides examples of function creation and usage, emphasizing the importance of functions in organizing and simplifying code. Key learning outcomes include understanding function syntax, argument passing, and return mechanisms.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Programming for Data Analysis

(CT127-3-2-PFDA and Version VC1)

Functions
Topic & Structure of the lesson

-Writing functions
- How to call a function
- Named Arguments
- Default values for Arguments
- Return value from Functions

CT038-3-2 Object
CT127-3-2 Oriented Development
Programming with Java
for Data Analysis File I/O
Functions SlideSlide
2 of 255of 12
Learning outcomes

At the end of this topic, you should be able to:


• Understand how to write a function and calling
them
• How to pass arguments and return values in
function

CT038-3-2 Object
CT127-3-2 Oriented Development
Programming with Java
for Data Analysis File I/O
Functions SlideSlide
3 of 355of 12
Key terms you must be able to use

- If you have mastered this topic, you should


be able to use the following terms correctly
in your assignments and exams:
- function
- argument
- return value

CT038-3-2 Object
CT127-3-2 Oriented Development
Programming with Java
for Data Analysis File I/O
Functions SlideSlide
4 of 455of 12
Functions
- Functions are used to logically break our code
into simpler parts which become easy to maintain
and understand.
- It’s pretty straightforward to create your own
function in R programming.
- Syntax for Writing Functions in R
func_name <- function (argument) {
statement
}
-Here we can see that the reserved word function is used to declare
a function in R
-The statements within the curly braces form the body of the
function.
- func_name-> name assigned to a function

CT038-3-2 Object
CT127-3-2 Oriented Development
Programming with Java
for Data Analysis File I/O
Functions SlideSlide
5 of 555of 12
Example of a Function

pow <- function(x, y) {


# function to print x raised to the power y
result <- x^y
print(paste(x,"raised to the power", y, "is", result))
}

- Here, create a function called pow()


- It takes two arguments, finds the first raised to the power of second
argument and prints the result in appropriate format
- paste()-built-in function

How to call a function ?


> pow(8,2)
[1] “8 raised to the power 2 is 64”

CT038-3-2 Object
CT127-3-2 Oriented Development
Programming with Java
for Data Analysis File I/O
Functions SlideSlide
6 of 655of 12
Default Values for Arguments
-We can assign default values to arguments in a function in R.
- This is done by providing an appropriate value to the formal argument
in the function declaration.
- Here is the above function with a default value for y
Eg:

pow <- function(x, y = 2) {


# function to print x raised to the power y
res <- x^y
print(paste(x,"raised to the power", y, "is",res
}
> pow(3)
[1] "3 raised to the power 2 is 9"
> pow(3,1)
[1] "3 raised to the power 1 is 3“
- Y is optional and will take value 2 when not provided

CT038-3-2 Object
CT127-3-2 Oriented Development
Programming with Java
for Data Analysis File I/O
Functions SlideSlide
7 of 755of 12
R Return Value from Function
-Functions are generally used for computing some value, so they need
a mechanism to supply that value back to the caller
- This is called returning.
- The value of the last line of the code in a function is automatically
returned.
- The return command specifies that a value should be returned and
the function should be exited
- The syntax of return is:
return(expression)
Eg: check <- function(x) {
if (x > 0) {
result <- "Positive"
}else if (x < 0) {
result <- "Negative"
}else {
result <- "Zero"
}
return(result)
}
CT038-3-2 Object
CT127-3-2 Oriented Development
Programming with Java
for Data Analysis File I/O
Functions SlideSlide
8 of 855of 12
Quick Review Questions

• How to create a function in R?


• How to pass parameters in R?
• What is the purpose of return value in a
function?

CT038-3-2 Object
CT127-3-2 Oriented Development
Programming with Java
for Data Analysis File I/O
Functions SlideSlide
9 of 955of 12
Summary of Main Teaching Points

• Functions
• Create function
• Passing arguments
• Return value

CT038-3-2 Object
CT127-3-2 Oriented Development
Programming with Java
for Data Analysis File I/O
Functions SlideSlide
10 of1055of 12
Q&A

CT038-3-2 Object
CT127-3-2 Oriented Development
Programming with Java
for Data Analysis File I/O
Functions SlideSlide
11 of1155of 12
Next Session

• Data Exploration
-Reading data into R
- Read from Excel sheet
- Read from Table

CT038-3-2 Object
CT127-3-2 Oriented Development
Programming with Java
for Data Analysis File I/O
Functions SlideSlide
12 of1255of 12

You might also like