0
0
R Programmingprogramming~15 mins

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

Choose your learning style9 modes available
Function definition
📖 Scenario: You are helping a small bakery keep track of how many cookies they bake each day. They want to use a simple function to calculate the total cookies baked over several days.
🎯 Goal: Create a function in R that adds two numbers representing cookies baked on two different days, then use it to find the total cookies baked.
📋 What You'll Learn
Create a function called add_cookies that takes two parameters: day1 and day2.
The function should return the sum of day1 and day2.
Create two variables cookies_day1 and cookies_day2 with values 50 and 70 respectively.
Call the function add_cookies with cookies_day1 and cookies_day2 as arguments and store the result in total_cookies.
Print the value of total_cookies.
💡 Why This Matters
🌍 Real World
Bakeries and small businesses often need simple calculations to track daily production or sales. Functions help automate these calculations.
💼 Career
Understanding how to define and use functions is a key skill for data analysis, automation, and software development jobs.
Progress0 / 4 steps
1
Create variables for cookies baked on two days
Create two variables called cookies_day1 and cookies_day2 and set them to 50 and 70 respectively.
R Programming
Need a hint?

Use the assignment operator <- to assign values to variables.

2
Define the function to add cookies
Define a function called add_cookies that takes two parameters: day1 and day2. The function should return the sum of day1 and day2.
R Programming
Need a hint?

Use function keyword to define a function in R.

3
Call the function and store the result
Call the function add_cookies with cookies_day1 and cookies_day2 as arguments and store the result in a variable called total_cookies.
R Programming
Need a hint?

Call the function by writing its name followed by parentheses with arguments inside.

4
Print the total cookies baked
Write a line to print the value of total_cookies.
R Programming
Need a hint?

Use the print() function to show the value on the screen.