---
title: "How to Write Simple Function in R Studio"
description: "Explained how to write simple function and execute it in R studio. Given syntax and example explanation."
url: https://srinimf.com/2018/11/13/ideas-you-need-to-write-simple-function-in-r-language/
date: 2018-11-13
modified: 2026-10-10
author: "Srini"
image: https://i0.wp.com/srinimf.com/wp-content/uploads/2018/11/pexels-photo-7674849.jpeg?fit=1880%2C1255&ssl=1
categories: ["R Language"]
tags: ["R Language Tips and Tricks", "Writing Function in R"]
type: post
lang: en
---

# How to Write Simple Function in R Studio

A function is an object in the R program. The purpose is repeated tasks [you can avoid](https://srinimf.com/2019/01/20/3-go-to-statements-to-avoid-in-cobol/) and improve your productivity. The theory behind this is you can give *one or more arguments in input* and you can get o*ne or more values as Return. *

Related[Bash Script Language Fundamentals: Learn How to Automate Tasks](https://srinimf.com/2023/07/29/how-to-write-shell-script-unlock-the-commands/)

## Syntax [to Write](https://srinimf.com/2011/08/07/easytrieve-os390-for-mainframe/) Function in R studio

```
functionname <- function(arg1,arg2,arg3,...)
{    do any code in here when called    return(returnobject)}
```

Related[How to Pass Arguments in Python Function](https://srinimf.com/2018/12/31/python-pass-by-value-vs-reference-read-today/)

The* functionname* is a valid name in R. The symbol ‘<-‘ you can say as assigning the name to function. In the above syntax. There are three arguments.

The code inside of braces is called ‘Body’. You can give one or more calls to return statements. Once the execution encounters ‘return’, it exits from the function.

The *returned object* is the output given back to the user.  

*More You Need to Know About Functions in R*

## Simple Function

```
myfirst <- function()
{
fib.a <- 1
fib.b <- 1
cat(fib.a,", ",fib.b,", ",sep="")
repeat{
temp <- fib.a+fib.b
fib.a <- fib.b
fib.b <- temp
cat(fib.b,", ",sep="")
if(fib.b>150){
cat("BREAK NOW...")
break
}
}}
```

I have written a function called *myfirst *, and it does not have any arguments. There are two variables fib.a and fib.b. 

The cat() functions to display information to the terminal or user. When you run the above code in the R command prompt, this function *myfirst* stores in the workspace.

You know that a function is also called an object in the R language. This object you can see using ls() function.

> ls() function shows the objects present in the current workspace

## The ls() function showing in R Studio

![R Function](https://i0.wp.com/srinimf.com/wp-content/uploads/2018/11/r-function1.jpg?h=535&ssl=1)

The last line “myfirst” shows the function is imported to the workspace. The command we used is ls().

## How Function Works in R Language

```
> myfirst()
```

In the above line, I have not passed any arguments. Since, while creating a function I did not pass any arguments.

So far you are good…

Result of MYFIRST function

```
[1] "myfirst" 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, BREAK NOW...
```

The ‘break’ statement says to exit from the function. The ‘sep=’ function adds one space.

### RStudio On line Compiler

You can work on [R Online Studio](https://rextester.com/l/r_online_compiler) here.

#### Related Posts

- [Tableau Integration with R Features to Read now](https://srinimf.com/2017/08/14/the-power-feature-tableau-integration-with-r-for-solutions/)DateAugust 14, 2017
- [3 Real-time House Keeping Ideas to Clean Data in R Language](https://srinimf.com/2018/11/11/3-real-time-house-keeping-ideas-to-clean-data-in-r-language/)DateNovember 11, 2018
- [Pass by Value and Pass by Reference in Python](https://srinimf.com/2022/05/23/python-pass-by-value-vs-pass-by-reference-simple-example/)DateMay 23, 2022

### Share this:

-
- [Tweet](https://twitter.com/share)
- [![](https://i0.wp.com/assets.pinterest.com/images/pidgets/pinit_fg_en_rect_gray_20.png)](https://www.pinterest.com/pin/create/link/?url=https%3A%2F%2Fsrinimf.com%2F2018%2F11%2F13%2Fideas-you-need-to-write-simple-function-in-r-language%2F&media=https%3A%2F%2Fi0.wp.com%2Fsrinimf.com%2Fwp-content%2Fuploads%2F2018%2F11%2Fpexels-photo-7674849.jpeg%3Ffit%3D1200%252C801%26ssl%3D1&description=How%20to%20Write%20Simple%20Function%20in%20R%20Studio)
-
- [Share on Tumblr](https://www.tumblr.com/share)
- [Share on WhatsApp (Opens in new window) WhatsApp](https://srinimf.com/2018/11/13/ideas-you-need-to-write-simple-function-in-r-language/?share=jetpack-whatsapp)
- [More](#)
-

- [Print (Opens in new window) Print](https://srinimf.com/2018/11/13/ideas-you-need-to-write-simple-function-in-r-language/?share=print)
- [Share on Reddit (Opens in new window) Reddit](https://srinimf.com/2018/11/13/ideas-you-need-to-write-simple-function-in-r-language/?share=reddit)
- [Share on Telegram (Opens in new window) Telegram](https://srinimf.com/2018/11/13/ideas-you-need-to-write-simple-function-in-r-language/?share=telegram)
- [Email a link to a friend (Opens in new window) Email](mailto:?subject=%5BShared%20Post%5D%20How%20to%20Write%20Simple%20Function%20in%20R%20Studio&body=https%3A%2F%2Fsrinimf.com%2F2018%2F11%2F13%2Fideas-you-need-to-write-simple-function-in-r-language%2F&share=email)
-

### Like this:

Like Loading…
