Imagine a task where you need to follow certain mathematical calculations multiple times; for example, you need to raise the power of a number by four and you need to do it 100 times. This will produce 100 lines of the same code which is ambiguity. What if you could encapsulate the procedure of raising the power of a number by four as a function and use it every time you need to do the task instead of repeating the same line of code? It would be great, wouldn't it? Saves a lot of time and a lot of repetitions.
Functions are an integral part of any programming language that works as an instruction set which does a certain task. They will do a certain task wherever you call them on any variable, or object. The R Programming language considers these functions as an object and provides them the temporary access of interpreter at the time of execution. Once the function is done with the task provided, the control gets reverted to the interpreter as it was before.
Yes, there are a lot of built-in functions in R. I should go further and say R has a rich library of built-in functions, which make life easy for an analyst. However, if you find any certain task can be automated using a function, you are free to write a function of your own under the R environment. In this article, we are going to get deep into the same topic; how to create a function of our own, also known as a user-defined function.
Let us see a general syntax of a function in R:
Syntax of a function in R
fun_name - specifies the name of the function. You need to store a function somewhere. fun_name allows you to store the same.
arglist - specifies the list of argument/s we use under a function. This is a required argument while defining a function.
Expr - specifies the expressions which get executed under a function body to have the required task done.
return - is a built-in function in R, that allows you to return a value for the defined function. If we don’t specify return while defining a function, the last line of code inside the function will get executed.
Defining a function to multiply three numbers
Let us define a function of our own that can give us the multiplication of three given numbers every time we call it. Check out the screenshot given below:
Image1: Function defining in R
Let us analyze what we have done here in this code.
We have defined a function with the name product_num that consists of three arguments “a”, “b”, and “c” (These are going to number more specifically).
Inside the function body, we have created a variable named product that holds the multiplication value for “a”, “b”, and “c” using the built-in multiplication operator (“*”).
Finally, we used a combination of print and paste (the built-in functions under R) so that we can print out a nice line of text with a multiplication value of three numbers that gets stored under the product variable.
We have tried executing this function and used 10, 30, and 50 as arguments inside the newly defined function product.num.
Now, time to run this code and check if the function defined works fine and generates the product of three numbers provided as an argument to it.
Image2: Output of function defined
Defining a function that takes a user input
To make this function more versatile or user friendly, we can introduce a part where we ask a user to provide the input values for a, b, and c. In that way, the function becomes more generalized since the user can provide the value of his interest for a, b, and c. Let’s modify our code a bit for this.
Image3: Defining a function with user input
If we try to analyze this code, it is the same as the one we have done previously with added lines that allow us to take the user input.
We define a function with a name product.num using the function keyword. Mandatory step, exclusion of this causes error in code.
We used the readline(prompt = “”) function that allows the user to provide the input value for a, b, and c.
Since the readline function gives us the values that the user input into a text format, we are going to use is.integer() function to convert those into integer values (for a, b, and c).
Once the values are converted into integers, we use the built-in multiplication operator (“*”) that allows us to multiply the three numbers a, b, and c and store the result into a variable named product.
We finally used the combination of print() and paste() (built-in functions in R) to get a nice output line printed on the console.
Finally, we are calling this function to see how it works. However, this time, we are not mentioning any values under it as we want the user to provide the values for the same
Now, time is to run this code and see how it works. See the screenshots below for better realization:
Image4: Output image for function with user input
Here, as soon as we run the code, a prompt gets popped up on the screen. Asking the user to provide a value for “a”. Once the user puts the value and hits enter, it generates a next prompt that asks value for “b”. See the screenshot below:
Image5: Output image with user input for another variable
Finally, the user provides a value for “c” and as soon as the function has value for three of the arguments, it executes the multiplication and returns the final output as shown below:
Image6: Final output for a function defined with user input
This is how we can define a function of our own which is more generalized in a way that it asks a user to provide the values of his own choice and returns the product of those three numbers.
Function scoping in R
Any programmer, sooner or later need to pay attention to the scoping. We don’t pay that much attention to scoping at the start, because we believe that we will never be in need to share the work with anyone else. However, there comes the time, where we need to share our code with the client before we deploy it on production. At that time, the quality of our code is something that comes in picture.
When we define a function under R, it gets defined under the local environment and then tries to make possible communication with the global environment for better execution of the code. There are a certain rule for this local to global communication as shown below:
The function always gets defined under the local environment.
When we use variables as an argument/s under the function body, they are limited to the function body (local scope we call it). Meaning they are getting defined within the function body and could not be found outside the function body.
Every time the function gets evoked, the current evocation is independent of all previous ones. This is an important aspect that allows us to use the function on different values for arguments as well as variables (if defined under the function Ex. product variable in the above function) within the function.
Function arguments are immutable. You can’t change them. You rather change their values every time and not the arguments. For example. While defining the product. num function, we have three arguments a, b, and c. We are changing the values for these arguments every time. We are not changing the arguments itself.
We have seen how to define a function of our own to automate certain tasks. We also have seen how to allow a user to provide the input values for arguments that make the function more generalized. We also have discussed how the function scoping works under R programming.
Let us stop here and I assure you that next time I will come up with something more interesting in the world of R programming. Until then, if you haven’t checked my articles on R programming series, you can navigate to R programming articles and have a look at those. Until the next time, stay safe, keep enhancing! 😊