PDA

View Full Version : Function in PHP help



hale88
12-04-2009, 09:58 PM
I really need help in this assignment since Function is so complicated. I only understand around 70% of it.
here is the assingment: Please write it in basic code so I can learn more and modify it later on

"Write a function called Celsius that accepts one argument for the Fahrenheit temperature and returns the value of the Fahrenheit temperature converted to Celsius.
Here is the formula to convert a Fahrenheit temperature to Celsius
C = 5 / 9 * (F – 32)
To use this function create a for loop that goes from 0 to 100.
In each iteration of the loop
• call the Celsius function passing the function the loop counter
• print the loop counter and the value returned from the Celsius function on the same line
This will create a list where the first number represents the Fahrenheit temperature and the second number represents the corresponding Celsius temperature."

thank you much

s1nykuL
12-05-2009, 02:36 PM
It is difficult tot judge at which level you need help. Do you know how to construct pseudo-code of what you want to achieve, and picture that code running in your head?

For example:

function tempconvert(void)
{

for (Tfar = 0 to 100 step 1)
{
print tfar;
print coma;
print celsius(Tfar);
print newline;
}
}


function celsius(Tfar)
{
return 5/9*(Tfar -32);
}

Can you write the above in php? or is it that you understand the logic and require more specific help with the php language itself?

hale88
12-05-2009, 08:16 PM
This is what I have got so far However at "function tempconvert(void)" which stand for??

function tempconvert(void)
{

for ($temperature= 0; $temperature <=100; $temperature++)
{
print $temperature;
print ",";
print celsius($temperature);
print "<br />";
}



return 5/9*($temperature-32);
}

W1zzard
12-06-2009, 03:08 AM
s1nykuL's solution is correct, it's almost php code already. if you want us to do your homework you should at least invest the time to get it into presentable shape. dont forget that you go to school to learn, not to learn how to paste from the internet

s1nykuL
12-07-2009, 01:40 PM
Your code is almost complete, look a little more closely, I think you may have omitted a closing brace and a function name. Note: celsius in my pseudo-code is a stand alone function.

As W1zzard has stated, providing you with the actual php code to do this will achieve very little and you will learn very little.

This is a link to ALL the resource you require to complete your assignment. http://www.php.net/manual/en/

Please feel free to ask specific questions about your code and I will help, but I am not going to write code for you ;)

function tempconvert(void) {
Code that does stuff
}
tempconvert is the function name, the void bit means it does not accept passed values.