0
0
R Programmingprogramming~15 mins

Function arguments in R Programming - Mini Project: Build & Apply

Choose your learning style9 modes available
Function arguments
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Use print(total) to display the total cost.