function issues, please help!

This is a discussion on function issues, please help! within the C Programming forums, part of the General Programming Boards category; Hey there, I am working on my first solo program and running into a road block. I keep going back ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    3

    Smile function issues, please help!

    Hey there,

    I am working on my first solo program and running into a road block. I keep going back to various sources for function syntax, and it seems like I am writing correctly. Probably not. I am hoping that some new eyes can point out my screw-ups below. Note: my compiler (using the Pelles C IDE).

    Code:
    #include <stdio.h>
    
    int choice;
    float temp;                   /* this is the user imput and thus the variable for the converter function */
    
    float convertF(void);
    float convertC(void);   /* prototyped the converter functions */
    
    int main()
    {
    	for( ; ; )
    	{
    		printf( "Enter a number to make a selection:\n" );
    		printf( "\n 1. Farenheit to Celsius\n 2. Celsius to Farenheit\n 3. Quit\n" );
    		scanf( "%d", &choice);
    
    		if(choice==1)
    		{
    			float result;
    
    			printf( "Please enter a decimal Farenheit value (e.g. 32.0)\n" );
    			scanf( "%f", temp );
    			
    			result=convertF(void);
    
    			printf( "\n %f degrees Farenheit equals %f degrees Celsius \n", temp, result);
    
    			continue;
    		}
    
    		if(choice==2)
    		{
    			printf( "Please enter a decimal Celsius value (e.g. 10.5)\n");
    			continue;
    		}
    
    		if(choice==3)
    		{
    			printf( "Goodbye!\n");
    			break;
    		}
    	}
    
    	return(0);
    } 
    
    float convertF(void)
    {
    	 float Cel;
    	
    
    	Cel=(temp - 32)/1.8;
    		return(Cel);
    }
     
    float convertC(void)
    {
    	float Far;
    	
    	Far=(temp * 1.8) + 32;
    		return(Far);
    }
    I am getting 2 main errors:

    (1) the "result=convertF(void);" is getting an "illegal expression" error. Note that the convertF function was originally convertF(temp); my thinking being that I wanted to pass the temp value obtained from the user to the function. I got a lot more errors with that set-up, so I changed it to void as an experiment (and got less errors). Hmm...

    (2) the same expression from (1) is also getting a "too many arguments for convertF" error. I just don't get this one, because at least one reference uses this format in an example of a function call.

    Anyway, please tell me what I am doing wrong with the functions/function calls.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So, you can't pass "void" to a function. If you don't want to pass anything to a function, then, don't pass anything to the function.

    Your original thought was correct, you do indeed need to pass things to the function. Comments:
    Code:
    #include <stdio.h>
    
    int choice;  /*bad and wrong */
    float temp; /*bad and wrong */
    
    float convertF(void);  /*incorrect prototype for this function */
    float convertC(void);   /*incorrect prototype for this function */
    
    int main()
    {
    	for( ; ; )  /* possibly the worst way to do this loop */
    	{
    		printf( "Enter a number to make a selection:\n" );
    		printf( "\n 1. Farenheit to Celsius\n 2. Celsius to Farenheit\n 3. Quit\n" );
    		scanf( "%d", &choice);
    
    		if(choice==1)
    		{
    			float result;
    
    			printf( "Please enter a decimal Farenheit value (e.g. 32.0)\n" );
    			scanf( "%f", temp );
    			
    			result=convertF(void);  /*you need to tell convertF about the input*/
    
    			printf( "\n %f degrees Farenheit equals %f degrees Celsius \n", temp, result);
    
    			continue;  /* pointless */
    		}
    
    		if(choice==2)
    		{
    			printf( "Please enter a decimal Celsius value (e.g. 10.5)\n");
    			continue;  /* similarly pointless */
    		}
    
    		if(choice==3)
    		{
    			printf( "Goodbye!\n");
    			break;
    		}
    	}
    
    	return(0);
    } 
    
    float convertF(void)  /*convertF must take an input parameter, presumably of type float, if you want to be able to use "temp" below */
    {
    	 float Cel;
    	
    
    	Cel=(temp - 32)/1.8;
    		return(Cel);
    }
     
    float convertC(void)   /*convertC must take an input parameter, presumably of type float, if you want to be able to use "temp" below */
    {
    	float Far;
    	
    	Far=(temp * 1.8) + 32;
    		return(Far);
    }

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    3

    follow up

    First, thanks for your thoughts. I have a couple follow up questions, if you don't mind.

    Was my original "float convertF(temp)" the correct way to pass temp to the function? If so, what is with the errors I mentioned?

    Also, what is the correct way to make to program run until the user "quits?" That was my intention with the infinite loop.

    Finally, why were my global variable declarations "bad and wrong?"

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    To take the questions in reverse order, 999999 times of 1000000 global variables are bad and wrong. In this case, they were a hack to prevent you from having to learn about functions. As it was, you were only able to convert one thing to fahrenheit. Should you, at some point, need to convert two different things to fahrenheit, then you would have to write another function, with a slightly different name, using a different global variable. And then, should you need to convert three different things .....

    The cheap and easy way to run until the program quits is to loop until choice equals 3. That's what choice is supposed to do, after all.

    So convertF(temp) is the correct way to pass temp to the function -- note the lack of the word float there, just as you don't have float in your code now either. That means you would need to define the function to take a parameter -- and when defining the function, you need to specify the type of the parameter as well, so the definition would contain (float temp).

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,788
    Firstly, a quote
    Quote Originally Posted by Wikipedia
    They are usually considered bad practice precisely because of their nonlocality: a global variable can potentially be modified from anywhere, (unless they reside in protected memory) and any part of the program may depend on it. A global variable therefore has an unlimited potential for creating mutual dependencies, and adding mutual dependencies increases complexity. See Action at a distance. However, in a few cases, global variables can be suitable for use. For example, they can be used to avoid having to pass frequently-used variables continuously throughout several functions.
    Now, as for errors in the code... let's start with warnings:
    Warning 1 warning C4244: '=' : conversion from 'double' to 'float', possible loss of data g:\w00t\visual studio 2008\projects\temp\temp4.cpp 52
    Warning 2 warning C4244: '=' : conversion from 'double' to 'float', possible loss of data g:\w00t\visual studio 2008\projects\temp\temp4.cpp 60
    Warning 5 warning C6066: Non-pointer passed as parameter '2' when pointer is required in call to 'scanf' g:\w00t\visual studio 2008\projects\temp\temp4.cpp 22

    Then I would like to remind you to call a function, you type its name, then a (, then its arguments (type their names), followed by a closing );.
    "void" is not a name of a variable and thus is invalid. It is used only in the prototype to say the function accepts no arguments.

    For the double to float warnings, you have two choices... either use a double data-type instead of float or append the "f" prefix after your double numbers:
    Cel=(temp - 32)/1.8f;

    And the rest, tabstop should have covered.
    Last edited by Elysia; 12-05-2008 at 01:58 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 01:28 PM
  5. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21