PHP excercise calculateTip

exercise:
Write a function calculateTip() which takes a number representing the total cost of a meal as its first argument. It should also take a second, optional argument—an integer representing the percent tip desired (eg. 25 will indicate a 25% tip should be calculated). If no second argument is passed in, the function should default to a 20% tip. The function should return the new total—the previous total plus the calculated tip.

function calculateTip ($tip = "100*(1+(25/100))")
{
 echo "$tip";
}
echo calculateTip (100,25);
echo calculateTip (100);
echo calculateTip (65,15);

I am beginner and doing exercise to calculate tip as above, is someone could help to correct? as I could not get the hint suggested by exercise as well. thank you

I’m also struggling with this particular exercise and the hints.

They say you should be passing in two arguments, a cost($cost) and then an optional value for tip($tip). If no value for tip is passed into the function there is a default value. I think they show you how to set an optional argument in a previous exercise.

You should also do your calculations inside the function. Using $cost and $tip to calculate a final $final_cost. Then return that final cost.

I hope this helps

1 Like

I am also a noobie and at first got stuck doing this exercise.
First of all, make it 2 parameters in the function calculateTip ($cost,$tip=20)
Than make a new $total in the body of the function and finally return it.
Finish with twice echo of the function with different integer arguments.
What I got is:

<?php
function calculateTip($total_cost,$tip = 20)
{
$new_total = ($total_cost * $tip) / 100 + $total_cost;
return $new_total;
}
echo calculateTip(100);
echo calculateTip(100,50);


I hope that helps.

3 Likes

Hello fellows.

I have a question about getting the default value outside the function.

<?php
$total = 0;
$tip = 20;


function calculateTip($total, $tip){
 
  return $total + ($total*$tip/100);
}

echo calculateTip(100, 25) . "\n";
echo calculateTip(100) . "\n";
echo calculateTip(65, 15);

Why is this throwing an error? $tip already has the value assigned when it is defined.

Learning too, this is how i did it:

<?php // Write your code below: function calculateTip($tc, $tip = 20) { return $tc * (1 + ($tip/100)); } echo calculateTip(500, 40); echo "\n"; echo calculateTip(500);

This is how I did it with trial and error it was not perfect but I did it

function calculateTip($tc, $tip = 18)

{

$new_total = ($tc * $tip) / 100 + $tc;

return round($new_total);

}

echo calculateTip(80);

Your function definition doesn’t declare a default value for $tip.

Parameter names declared in the function hide any with the same name variable from outside of the function so $total and $tip in the function are different to the ones outside.

You have set a value (not a default value) in the variable $tip but not the parameter $tip of the function.

To set the default value for a parameter you must include that in the function definition.

e.g.

function calculateTip($total, $tip = 20)
{
  return $total * (1 + $tip / 100);
}

The function is named incorrectly, it doesn’t return the tip but the new total including the tip.

calculateTotalWithTip would have been a better name.