PascalCaseis similar to camelCase, except the first letter in PascalCase is always capitalized.
Camel case is the practice of writing phrases without spaces or punctuation, indicating the separation of words with a single capitalized letter, and the first word starting with either case.Examples include “iPhone”, “JavaScript”, and “eBay”.
Arguement matching/mapping
When there are multiple arguments in a function.
Arguement maching is mapping between values passed from the function invocationto arguements of the function definition.
Match by position-First value is passed as first arguement while second value is passed as second arguement.
An arguement which has some default value and is not required to specify in a function call.
We extend the original function by adding one more arguement and assign a fixed value to it in thefunction definition itself . Then adding the same arguement in the function body also(2 here).
We can call the function without specifying the value for this default arguement.
Additional arguements in R using Ellipsis/Triple dots
Ellipsis is also a function arguement. It is a special arguement meaning “anything else”.
We use it when we have a function with few known arguements, and we want to use some more arguements but are not sure what those additional arguements could be.
In R Ellipsis should be the last arguement in the function.
Using Ellipsis we can pass additional arguemnts to the function without changing the function definition.
These additional arguements cannot be mapped either using position or using name. We use ellipsis to map these additional arguements.
Typically, ellipsis are used to add additional arguements to some external functions. Here, we have passed additional arguements to sum function.
Lazy evaluation of default arguement in R function
Evaluation of a default arguement (whose value is set to another variable) is deferred until it is first used.
We would not supply any value for default arguement, even then R will not raise any error in execution of first line. It will be evaluated only after first use. This behavior is known as lazy evaluation.
GetTotalMarks(exam.marks= c(50L,60L,70L,80L), viva.marks= c(5L,6L,7L,8L))#to call function by passing two interger vector as vlaues to the arguements.
[1] 61.5 72.5 83.5 94.5
Multiple return values from a function
There is no need of explicit return statement for the functions which return only a single value . By default, last line of function is treated as return value.
If we need multiple return values from a functionwe can use a list with the return statement.
To get any individual value we can use any of list-subsetting technique.
GetTotalMarks(exam.marks= c(50L,60L,70L,80L), viva.marks= c(5L,6L,7L,8L))#to call function by passing two interger vector as vlaues to the arguements.
[1] 55 66 77 88
(01) To look into function object
GetTotalMarks # retrun whole function by using function name without parenthesis
function(exam.marks, viva.marks ){
GetTotalMarks<-exam.marks + viva.marks
GetTotalMarks
}
formals(GetTotalMarks) #extract/access arguements fo function
$exam.marks
$viva.marks
body(GetTotalMarks) # extract/access body of function
{
GetTotalMarks <- exam.marks + viva.marks
GetTotalMarks
}
(02) To assign a function to another variable
Fall2020.total.marks <- GetTotalMarks#to assign a function to another variable
Fall2020.total.marks #newly created function has the same content
function(exam.marks, viva.marks ){
GetTotalMarks<-exam.marks + viva.marks
GetTotalMarks
}
(03) To use functions as arguement to other function
GetTotalMarks(exam.marks= c(50L,60L,70L,80L), viva.marks= c(5L,6L,7L,8L))# traditional method to call a function
[1] 55 66 77 88
do.call(GetTotalMarks, list(exam.marks= c(50L,60L,70L,80L), viva.marks= c(5L,6L,7L,8L)))# pass the function object as first parameter to do.call method and to call arguements of function we create a list and pass it as second parameter
[1] 55 66 77 88
Anonymous Function
Anonymous function is a function without any name.
Used to create small functions.
Here, we have used do.callmethod using anonymous function as first parameter and to call arguements of function we create a list and pass it as second parameter