📖 Scenario: You are helping a small bakery calculate the total cost of different types of cakes ordered by customers. Each cake has a price and a quantity ordered.
🎯 Goal: Build a function that takes the price and quantity of a cake as arguments and returns the total cost. Then use this function to calculate the total cost for a specific order.
📋 What You'll Learn
Create a function called calculate_cost that takes two arguments: price and quantity.
Inside the function, multiply price by quantity and return the result.
Call the function with price = 15 and quantity = 3.
Print the result of the function call.
💡 Why This Matters
🌍 Real World
Calculating costs based on quantity and price is common in shops, restaurants, and online stores.
💼 Career
Understanding how to write functions with arguments is essential for automating calculations and processing data efficiently in many programming jobs.
Progress0 / 4 steps
1
Create the function skeleton
Write a function called calculate_cost that takes two arguments: price and quantity. For now, leave the function body empty.
R Programming
Hint
Use the function keyword to define a function with two arguments.
2
Add the calculation inside the function
Inside the calculate_cost function, multiply price by quantity and return the result using the return() function.
R Programming
Hint
Use return(price * quantity) to send back the total cost.
3
Call the function with specific values
Call the calculate_cost function with price = 15 and quantity = 3, and save the result in a variable called total.
R Programming
Hint
Use total <- calculate_cost(15, 3) to call the function and store the result.
4
Print the total cost
Print the value of the variable total to show the total cost.
R Programming
Hint
Use print(total) to display the total cost.
Practice
(1/5)
1. What is the purpose of function arguments in R? my_function <- function(x, y) { x + y }
easy
A. To create variables outside the function
B. To print output from the function
C. To send information into the function for processing
D. To stop the function from running
Solution
Step 1: Understand what arguments do
Arguments are values you give to a function so it can use them inside its code.
Step 2: Look at the example function
The function takes x and y as inputs and adds them, showing arguments are inputs.
Final Answer:
To send information into the function for processing -> Option C
Quick Check:
Function arguments = inputs [OK]
Hint: Arguments are inputs you give to functions [OK]
Common Mistakes:
Thinking arguments print output
Confusing arguments with variables outside
Believing arguments stop function execution
2. Which of the following is the correct way to define a function with two arguments a and b in R?
easy
A. my_func <- function {a, b} return(a + b)
B. my_func <- function(a, b) { return(a + b) }
C. my_func <- function[a, b] { return(a + b) }
D. my_func <- function(a; b) { return(a + b) }
Solution
Step 1: Recall R function syntax
Functions use parentheses () to list arguments separated by commas.
Step 2: Check each option
Only my_func <- function(a, b) { return(a + b) } uses parentheses and commas correctly; others use wrong brackets or separators.
Final Answer:
my_func <- function(a, b) { return(a + b) } -> Option B
Quick Check:
Function args use (a, b) [OK]
Hint: Use parentheses and commas for arguments [OK]
Common Mistakes:
Using square brackets instead of parentheses
Separating arguments with semicolons
Omitting parentheses around arguments
3. What will be the output of this R code?
add <- function(x, y = 5) {
x + y
}
add(3)
medium
A. 8
B. 3
C. 5
D. Error: missing argument y
Solution
Step 1: Understand default argument values
The function add has y defaulting to 5 if not provided.
Step 2: Call the function with one argument
Calling add(3) uses x=3 and default y=5, so result is 3 + 5 = 8.
Final Answer:
8 -> Option A
Quick Check:
3 + default 5 = 8 [OK]
Hint: Default arguments fill in missing values [OK]
Common Mistakes:
Expecting error when argument is missing
Ignoring default argument values
Confusing which argument has default
4. Identify the error in this R function definition:
multiply <- function(x, y) {
result <- x * z
return(result)
}
medium
A. Function should not use return statement
B. Function name cannot be multiply
C. Arguments must have default values
D. Variable z is not defined inside the function
Solution
Step 1: Check variables used inside function
The function uses z but only x and y are arguments; z is undefined.
Step 2: Understand variable scope
Variables inside functions must be defined or passed as arguments; z is missing.
Final Answer:
Variable z is not defined inside the function -> Option D
Quick Check:
Undefined variable = error [OK]
Hint: Use only defined arguments or variables inside functions [OK]
Common Mistakes:
Using variables not passed as arguments
Thinking return is optional error
Believing arguments must have defaults
5. You want to create a function greet that takes a name and an optional greeting message (default "Hello"). Which function definition is correct to achieve this?
In R, arguments with defaults must come after those without defaults.
Step 2: Check each option
greet <- function(name, message = "Hello") { paste(message, name) } correctly places message with default after name without default and uses paste correctly.