Challenge: Simple Function Practice
Example of a Function with Arguments from the Previous Chapter
function.h
123456double convert_usd_to_eur(double usd_amount) { const double exchange_rate = 0.91; double euros = usd_amount * exchange_rate; return euros; }
Swipe to start coding
Create a function withdraw
that simulates a bank account withdrawal. It accepts the current balance and withdrawal amount as arguments. If the balance is sufficient, deduct the amount and return the new balance. Otherwise, return the original balance.
- Implement a function
withdraw
with a return type ofint
and twoint
parameters (balance
andamount
). - Use an
if
statement to check if the balance is sufficient for the withdrawal. - If the balance is sufficient, return the new balance after subtracting
amount
. - If the balance is insufficient, return the original balance (
balance
variable).
Example
withdraw(500, 200) => 300
withdraw(500, 500) => 0
withdraw(200, 500) => 200
Solution
solution.cpp
Thanks for your feedback!
single
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Awesome!
Completion rate improved to 3.85
Challenge: Simple Function Practice
Swipe to show menu
Example of a Function with Arguments from the Previous Chapter
function.h
123456double convert_usd_to_eur(double usd_amount) { const double exchange_rate = 0.91; double euros = usd_amount * exchange_rate; return euros; }
Swipe to start coding
Create a function withdraw
that simulates a bank account withdrawal. It accepts the current balance and withdrawal amount as arguments. If the balance is sufficient, deduct the amount and return the new balance. Otherwise, return the original balance.
- Implement a function
withdraw
with a return type ofint
and twoint
parameters (balance
andamount
). - Use an
if
statement to check if the balance is sufficient for the withdrawal. - If the balance is sufficient, return the new balance after subtracting
amount
. - If the balance is insufficient, return the original balance (
balance
variable).
Example
withdraw(500, 200) => 300
withdraw(500, 500) => 0
withdraw(200, 500) => 200
Solution
solution.cpp
Thanks for your feedback!
single