- Home
- Training Library
- Big Data
- Courses
- 6. Functions in R

# What is returned from a function in R

## Contents

###### Fundamentals of R

## The course is part of this learning path

**Difficulty**Intermediate

**Duration**27m

**Students**45

**Ratings**

### Description

**Course Description**

This module looks at functions, how to create functions, and how they can be used in R.

**Learning Objectives**

The objectives of this module are to provide you with an understanding of:

- How to define a function in R
- How to use built in functions in R
- What a return value is
- That functions can be stacked, and that they do not require an input
- That arguments can be named or

**Intended Audience**

Aimed at all who wish to learn the R programming language.

**Pre-requisites**

No prior knowledge of R is assumed

Delegates should already be familiar with basic programming concepts such as variables, scope and functions

Experience of another scripting language such as Python or Perl would be an advantage

Understanding mathematical concepts will be beneficial

**Feedback**

We welcome all feedback and suggestions - please contact us at qa.elearningadmin@qa.com to let us know what you think.

### Transcript

- [Instructor] When creating a function in R, one might wonder what is going to be returned. In some languages, we see that the last line of the body of a function is returned. This is usually the case in R also. We also have the option where we can explicitly define the return statement. I can create a function called getNumber. And I'd like that function to get the number using number as a parameter from, say for example, the Fibonacci sequence, which I could define as following, ranging from one all the way up to 21. And, I could ask implicitly as being the last line of my function here that I would like to return the numbered index of this Fibonacci sequence here. And here, if I was to run the getNumber function on number three, I would be asking for the third index of the Fibonacci sequence, which has been defined here as ranging from one to 21. And the third index would range from one and then two would be returned as the output. I could repeat the same logic if I was to change that from three to say six. I could ask for the sixth item inside the Fib's vector, and I could run through here and count up to eight and see that this is the sixth number in the Fibonacci sequence, as defined within my function. As an aside, I'd like to state that this is bad practice to define variables within a function. I'm using that purely here as an example to understand what is being returned from the function. I'll continue now and explain how we can explicitly define the return statement. Let's say we had to find a different function called getNumber_return, R E T. And if I had defined it as the following, where I have explicitly decided that I would like to return the numbered item in the Fibonacci sequence. Thereafter, multiplying by two the entire vector of one through til 21 and then, asking for the Fib number to be returned in the same way I had done earlier. We'll see that the doubled value will not be returned because the explicitly defined returned statement will be returned to the screen. And here I now choose to use the same numbers that we had earlier, but this time with the updated function. And we can see that the output has not changed. And again, we received back eight. So, the fact that there's anything done after this return statement has no impact on what it is output. So we have explained that we can explicitly define the return statement if we choose.

**Students**571

**Labs**1

**Courses**11

**Learning paths**1

Kunal has worked with data for most of his career, ranging from diffusion markov chain processes to migrating reporting platforms.

Kunal has helped clients with early stage engagement and formed multi week training programme curriculum.

Kunal has a passion for statistics and data; he has delivered training relating to Hypothesis Testing, Exploring Data, Machine Learning Algorithms, and the Theory of Visualisation.

Data Scientist at a credit management company; applied statistical analysis to distressed portfolios.

Business Data Analyst at an investment bank; project to overhaul the legacy reporting and analytics platform.

Statistician within the Government Statistical Service; quantitative analysis and publishing statistical findings of emerging levels of council tax data.

Structured Credit Product Control at an investment bank; developing, maintaining, and deploying a PnL platform for the CVA Hedging trading desk.