Thread: Newbie student

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    77

    Newbie student

    Hi guys,
    I am working on a program that will convert the Fahrenheit value taken and returning a celsius.

    We have to use a user defined function to calculate. The program doesn't work ofcourse.

    Code:
    #include <stdio.h>
    
    
    
    float Fahrenheit_to_Celsius();
    
    void main()
    
    {
    float fahren;
    printf ("Please enter a Fahrenheit value:");
    scanf ("%.1f", &fahren);
    
    Fahrenheit_to_Celsius();
    {
    float celsius, fahren;
    celsius=5*(fahren-32.0)/9;
    return celsius;
    }
    
    printf("%.1f degrees Fahrenheit is %.1f degrees Celsius", fahren, celsius);
    
    }

    I have a question too. I have seen my teacher writing the user defined fuction like this:

    Code:
    void calculate(int dice1, int dice2);
    so did he just declared the variables in the fuction?. So will this be a local variable or a global variable?. Sorry I am little confused with fuctions again .


    thanks for any help

  2. #2
    Registered User
    Join Date
    Sep 2004
    Posts
    99
    First of all, if you call a function with float as returned value, you need to have a variable to assign that value to. e.g.
    Code:
    float var1;
    
    var1 = function(var2, var3);

  3. #3
    Banned
    Join Date
    Oct 2004
    Posts
    250
    void calculate(int dice1, int dice2);
    This is a function defininition this declares what parameters the function will take

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    77
    Quote Originally Posted by kocika73
    First of all, if you call a function with float as returned value, you need to have a variable to assign that value to. e.g.
    Code:
    float var1;
    
    var1 = function(var2, var3);
    so is that only for float or int and double too?

  5. #5
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    That is for whatever type the function returns.

    aka

    int myFunc() ; //returns an int
    float myFuct(); //returns a float
    char myFunction(); //returns a char
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  6. #6
    Registered User
    Join Date
    Sep 2004
    Posts
    99
    Every function has either return value of float, double, int, etc, or it might be void. Void means no return value. If there is no return value, you can call the function in main() by simply writing the name of function and specifying its parameters (if there are any in function definition). If a function however has return value of any kind, you need a variable to assign the return value to. Basically, that's the rule. Otherwise, where would the thing you calculated in the function go to?

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    77
    hmmm, ok thanks guys. I think I will go read more about fuction, didn't quite get it. Thanks for all your help

  8. #8
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    If a function however has return value of any kind, you need a variable to assign the return value to.
    Well, to be nitpicky you don't need to capture the return value of a function. You can just ignore it.
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    77
    oh okay, I think now I understand. So the returned value has to be assigned to a variable. So my function return a value that is in "celsius" and that goes back to the main fucntion?.

  10. #10
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    yes. post your updated code.
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  11. #11
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    I hope this makes things a little easier for you:
    Code:
    #include <stdio.h>
    
    // f = Fahrenheit
    // c = Celsius
    float f;
    float c;
    
    // This function converts fahrenheit to celsius
    void Fahrenheit_to_Celsius( void )
    {
       // Change fahrenheight to celsius
       
       c = 5 * ( f - 32.0 ) / 9;
    }
    
    // Starting point of the program, always returns an integer
    int main(
       void )   // The function main receives no input
    {
       // Ask the user for a fahrenheit value
       
       printf( "Please enter a Fahrenheit value:" );
    
       // Receive fahrenheit from user
       
       scanf( "%f", &f );
    
       // Convert the fahrenheit from user input into celsius
       
       Fahrenheit_to_Celsius( );
    
       // Output to the user our degree conversion
       
       printf( "%.1f degrees Fahrenheit is %.1f degrees Celsius", f, c );
    
       // Return program successfully
    
       return 0;
    }

  12. #12
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    Kleid-0 - global variables for no reason == WRONG !!!!

    but at least you didn't use a goto
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  13. #13
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    I was so close to using goto, I was like "uuhh I don't know, I've gotta do something wrong with this example...oh I know! Global variables!" lol

  14. #14
    Registered User
    Join Date
    Jan 2005
    Posts
    77
    thanks kleid, that does makes it a lot easier hehe. Yea and teacher says using global variable is a bad habit . I don't know if that's true. Thanks kleid again, now I will go throught the code and learn how you did it.

    thanks

  15. #15
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    Another one gone to the darkside. How many is that for you now Kleid-0?
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Updating in a sequential file?
    By Ronnyv1 in forum C Programming
    Replies: 1
    Last Post: 03-24-2009, 04:41 PM
  2. Database assignment is Killing me!
    By Boltrig in forum C Programming
    Replies: 2
    Last Post: 11-29-2007, 03:56 AM
  3. Newbie college student
    By jat421 in forum C Programming
    Replies: 3
    Last Post: 02-05-2005, 06:07 PM
  4. LinkList Sorting in C
    By simly01 in forum C Programming
    Replies: 3
    Last Post: 11-25-2002, 01:21 PM
  5. Creating a student grade book-how?
    By Hopelessly confused in forum C Programming
    Replies: 5
    Last Post: 10-03-2002, 08:43 PM