R Tutorial: How to pass data between functions

When starting to look at functions and having tested them in Python and Javascript, it was quickly apparent how programming languages are so similar.

Except for the syntax you use in each; the programming is quite similar.

The purpose of this video is to:

  • Start on using functions from the ground up.
  • Don’t over-complicate the example; keep it easy enough to follow.

How to write the code to pass data between functions

As this is a short video, the code that went into making it is pretty straight forward

# create a function
function.a <- function(){
  newvarb <- 2
}

function.b <- function(){
  newvarb <- function.a()*2 # this takes in the value of function a and multiplies it by two
}
print(function.b()) # Prints out the value of function b

Below is the video that will take you through each line, and show the output that we are looking to achieve

How can we use this in our projects

No matter what programming language you use or choose to learn, the concept of functions will appear in some shape or form. Their ability to quickly run a repeatable process and return a value, which can be called from anywhere in a program allows the programmer to reduce their coding time swiftly and reduce repetitive tasks that only need to run once.

This video has an equivalent in Python, and you can see it here  Python Functions – passing data between them

Data Analytics Ireland

how to pass data between functions

In this python program, we are learning how to pass data between functions, a tool that will become very handy especially when you are trying to automate certain tasks that are repetitive.

Functions serve a number of benefits:

  • You can pass a number of arguments to them to be processed.
  • They are easily identified by using the def keyword in your code.
  • a return statement can give you the output of the function to show on the screen or pass to another function.

It compliments r-tutorial-how-to-pass-data-between-functions/  and can be seen that this handy bit of functionality is used widely across many different programming languages.

Below is a video that will help to give an understanding of how to pass data between functions when trying to learn python:

 

In many of the Data Analytics Ireland    YouTube channel videos, there is an emphasis on creating content that eliminates duplication of code within the code. We have also started incorporating classes as well and you can see here How to create a class in Python , a tutorial on how to create one.

Classes by their nature have methods, which are called on the objects that created them ( the class), and can alter their state, whereas a function will run and just return a value.

It is important to understand the distinction as while the two will most likely achieve the same outcome, it is the ability to change the class state that will differentiate the two.