Key Concepts
A function is a basic data recipe. It is also an input-output machine. It accepts arguments, and returns the final product of the recipe.
- The function
function()
- arguments
- argument values
- argument default values
- explicit vs implicit argument calls
- return values
- arrow vs. equal sign
Loan Calculator
Let’s create a mortgage calculator as a simple example. A mortgage calculator considers the total loan amount (the principal), the interest rate or APR (annual percentage rate), and the period of the loan in order to determine how much needs to be paid each month so that payments are distributed equally across the loan term.
![](https://res.cloudinary.com/value-penguin/image/upload/c_limit,dpr_1.0,f_auto,h_1600,q_auto,w_1600/v1/mortgages/amort-30-yr)
If we look up an amortization (repayment schedule) formula, we see it is calculated as follows:
\[
monthly \ payments = \frac{principal \cdot interest \ rate}{1-(1+interest \ rate)^{- \ loan \ duration}}
\]
When we translate this mathematical formula into R code, the new function will look like this:
eyJsYW5ndWFnZSI6InIiLCJwcmVfZXhlcmNpc2VfY29kZSI6IiBjYWxjTW9ydGdhZ2UgPC0gZnVuY3Rpb24oIHByaW5jaXBhbCwgeWVhcnMsIEFQUiApXG4ge1xuICAgIG1vbnRocyA8LSB5ZWFycyAqIDEyXG4gICAgaW50LnJhdGUgPC0gQVBSIC8gMTJcbiAgICBcbiAgICAjIGFtb3J0aXphdGlvbiBmb3JtdWxhXG4gICAgbW9udGhseS5wYXltZW50IDwtICggcHJpbmNpcGFsICogaW50LnJhdGUgKSAvIFxuICAgICAgICAgICAgICAgICAgICAgICAoMSAtICgxICsgaW50LnJhdGUpXigtbW9udGhzKSApXG4gICAgXG4gICAgbW9udGhseS5wYXltZW50IDwtIHJvdW5kKCBtb250aGx5LnBheW1lbnQsIDIgKVxuICAgIFxuICAgIHJldHVybiggbW9udGhseS5wYXltZW50IClcbiBcbiB9Iiwic2FtcGxlIjoiIyBDaGVjayB0aGUgZnVuY3Rpb24ncyBhcmd1bWVudHM6XG5hcmdzKCBjYWxjTW9ydGdhZ2UgKVxuXG4jIENhbGN1bGF0ZSBhIG1vcnRnYWdlIGZvciBhICQxMDAsMDAwIGhvdXNlIGZvciAzMCB5ZWFycyB3aXRoIGEgNC41IHBlcmNlbnQgaW50ZXJlc3QgcmF0ZSAoMC4wNDUgQVBSKVxuY2FsY01vcnRnYWdlKCBwcmluY2lwYWw9MTAwMDAwLCB5ZWFycz0zMCwgQVBSPTAuMDQ1ICkgXG5cbiMgVHJ5OiBjYWxjTW9ydGdhZ2UoIHByaW5jaXBhbD0xMDAwMDAgKVxuY2FsY01vcnRnYWdlKCBwcmluY2lwYWw9MTAwMDAwLCBBUFI9MC4wNDUgKSJ9
What happens if you omit an argument from the function? Why?
Default Arguments
We can add default values for arguments. These defaults allow us to utilize the function without specifying values for those arguments.
eyJsYW5ndWFnZSI6InIiLCJwcmVfZXhlcmNpc2VfY29kZSI6IiBjYWxjTW9ydGdhZ2UgPC0gZnVuY3Rpb24oIHByaW5jaXBhbCwgeWVhcnM9MzAsIEFQUj0wLjA1IClcbiB7XG4gICAgbW9udGhzIDwtIHllYXJzICogMTJcbiAgICBpbnQucmF0ZSA8LSBBUFIgLyAxMlxuICAgIFxuICAgICMgYW1vcnRpemF0aW9uIGZvcm11bGFcbiAgICBtb250aGx5LnBheW1lbnQgPC0gKCBwcmluY2lwYWwgKiBpbnQucmF0ZSApIC8gXG4gICAgICAgICAgICAgICAgICAgICAgICgxIC0gKDEgKyBpbnQucmF0ZSleKC1tb250aHMpIClcbiAgICBcbiAgICBtb250aGx5LnBheW1lbnQgPC0gcm91bmQoIG1vbnRobHkucGF5bWVudCwgMiApXG4gICAgXG4gICAgcmV0dXJuKCBtb250aGx5LnBheW1lbnQgKVxuIFxuIH0iLCJzYW1wbGUiOiIjIE5vdGljZSB0aGUgbmV3IGRlZmF1bHQgYXJndW1lbnRzOlxuYXJncyggY2FsY01vcnRnYWdlIClcblxuIyBDYWxjdWxhdGUgYSBtb3J0Z2FnZSBmb3IgYSAkMTAwLDAwMCBob3VzZSBmb3IgMzAgeWVhcnMgd2l0aCBhIDQuNSBwZXJjZW50IEFQUlxuY2FsY01vcnRnYWdlKCBwcmluY2lwYWw9MTAwMDAwLCB5ZWFycz0zMCwgQVBSPTAuMDQ1IClcblxuIyBOb3cgdHJ5OiBjYWxjTW9ydGdhZ2UoIHByaW5jaXBhbD0xMDAwMDAgKVxuY2FsY01vcnRnYWdlKCBwcmluY2lwYWw9MTAwMDAwICkifQ==
Note that calcMortgage( principal=100000 ) now works because the function uses the default values for years and APR.
Defaults
eyJsYW5ndWFnZSI6InIiLCJwcmVfZXhlcmNpc2VfY29kZSI6IiBjYWxjTW9ydGdhZ2UgPC0gZnVuY3Rpb24oIHByaW5jaXBhbCwgeWVhcnM9MzAsIEFQUj0wLjA0NSApXG4ge1xuICAgIG1vbnRocyA8LSB5ZWFycyAqIDEyXG4gICAgaW50LnJhdGUgPC0gQVBSIC8gMTJcbiAgICBcbiAgICAjIGFtb3J0aXphdGlvbiBmb3JtdWxhXG4gICAgbW9udGhseS5wYXltZW50IDwtICggcHJpbmNpcGFsICogaW50LnJhdGUgKSAvIFxuICAgICAgICAgICAgICAgICAgICAgICAoMSAtICgxICsgaW50LnJhdGUpXigtbW9udGhzKSApXG4gICAgXG4gICAgbW9udGhseS5wYXltZW50IDwtIHJvdW5kKCBtb250aGx5LnBheW1lbnQsIDIgKVxuICAgIFxuICAgIHJldHVybiggbW9udGhseS5wYXltZW50IClcbiBcbiB9Iiwic2FtcGxlIjoiYXJncyggY2FsY01vcnRnYWdlIClcblxuIyBDYWxjdWxhdGUgYSBtb3J0Z2FnZSBmb3IgYSAkMTAwLDAwMCBob3VzZSBmb3IgMTUgeWVhcnMgd2l0aCBhIDMgcGVyY2VudCBBUFJcblxuY2FsY01vcnRnYWdlKCBwcmluY2lwYWw9MTAwMDAwLCB5ZWFycz0xNSwgQVBSPTAuMDMgKSJ9
Can you still use custom values for those arguments after defaults are set?
Implicit Argument Calls
An explicit call to arguments always uses the formal argument name such as principal=100000
.
You can, however, also use implicit argument calls. These rely on the order arguments are specified in the function, and the order of your values in your call.
Implicit arguments calls can be risky, however, because it is very easy to mix them up.
eyJsYW5ndWFnZSI6InIiLCJwcmVfZXhlcmNpc2VfY29kZSI6IiBjYWxjTW9ydGdhZ2UgPC0gZnVuY3Rpb24oIHByaW5jaXBhbCwgeWVhcnM9MzAsIEFQUj0wLjA0NSApXG4ge1xuICAgIG1vbnRocyA8LSB5ZWFycyAqIDEyXG4gICAgaW50LnJhdGUgPC0gQVBSIC8gMTJcbiAgICBcbiAgICAjIGFtb3J0aXphdGlvbiBmb3JtdWxhXG4gICAgbW9udGhseS5wYXltZW50IDwtICggcHJpbmNpcGFsICogaW50LnJhdGUgKSAvIFxuICAgICAgICAgICAgICAgICAgICAgICAoMSAtICgxICsgaW50LnJhdGUpXigtbW9udGhzKSApXG4gICAgXG4gICAgbW9udGhseS5wYXltZW50IDwtIHJvdW5kKCBtb250aGx5LnBheW1lbnQsIDIgKVxuICAgIFxuICAgIHJldHVybiggbW9udGhseS5wYXltZW50IClcbiBcbiB9Iiwic2FtcGxlIjoiIyBUcnkgdGhlc2U6XG5cbmNhbGNNb3J0Z2FnZSggMTAwMDAwLCAzMCwgMC4wNSApXG5cbmNhbGNNb3J0Z2FnZSggMzAsIDEwMDAwMCwgMC4wNSApIn0=
Which of these calculations is correct?
Explain why.
Object Assignment vs Argument Assignment
Your Turn
Create a function to convert Fahrenheit temperatures to Celsius.
- What arguments do you need?
- How many decimals do you need? Consider using the
round( number, decimals )
function to simplify output.
- Don’t forget a return statement!
\[
celsius = ( \ fahrenheit − 32 \ ) × \frac{5}{9}
\]
If you want to check your work, 212 degees Fahrenheit is equivalent to 100 degrees Celsius.
eyJsYW5ndWFnZSI6InIiLCJzYW1wbGUiOiJjb252ZXJ0VG9DZWxzaXVzIDwtIGZ1bmN0aW9uKCkgICMgYXJndW1lbnRzIGhlcmVcbntcbiAgICAjIHlvdXIgY29kZSBoZXJlXG59XG5cbnRlbXAuaW4uZmFocmVuaGVpdCA8LSAyMTJcbmNvbnZlcnRUb0NlbHNpdXMoIHRlbXAuaW4uZmFocmVuaGVpdCApIn0=
Can you use this function to convert from Celsius to Fahrenheit?