954,492 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

function not recognised

I am trying to make a function that rounds number..
I declared the function above the main and voided it
Then I recalled the function in the program and listed the variables needed in brackets
and then I wrote the function

I can't work out what is wrong.

#include<stdio.h>
#include<math.h>

void round2int(float,float);
int main (void)
{

float roundednum,x;
do
{
printf("Enter a number (Enter zero (0) to quit program)");
scanf("%f",&x);
round2int(x,roundednum);
}
while(x!=0);
fflush(stdin);
getchar();
return 0;
}

void round2int(float test, float number)
{
float number,test;
number=floor(test+05);
printf("%f",number);
}

terence193
Junior Poster in Training
54 posts since Apr 2008
Reputation Points: 6
Solved Threads: 0
 

in your function you are declaring twice the variables needed. 1st time as arguments and 2nd time when you say

float number, test;


this cleans up the variables and you get zero or memory gabadge.

SpyrosMet
Light Poster
46 posts since Feb 2010
Reputation Points: 7
Solved Threads: 3
 

Since your function is called round2Int, you could have it just "return" an int and print it from main().

int round2int(float test, float number)
{
  return floor(test+.5);
}
thines01
Postaholic
Team Colleague
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
 
in your function you are declaring twice the variables needed. 1st time as arguments and 2nd time when you say

Oh, I get it. Actually that was the mistake, and there was no need for 2 floats. One was enough.

#include<stdio.h>
#include<math.h>

void round2int(float);
int main (void)
{

   float x;
   printf("Enter a number (Enter zero (0) to quit program)\n");
   while(x!=0)
   {
	printf("\n\nNumber: ");
	scanf("%f",&x);
	round2int(x);
   }
   fflush(stdin);
   getchar();
   return 0;
}


void round2int(float test)
{
  test=floor(test+0.5);
  printf("Rounded Number: %f",test);
}

This worked well.

@thines01: print it from main().

How can I print it from the main? I know it sounds stupid, but I am still a beginner to the language.
I've tried this but it didn't work

printf("%f",*round2int);
terence193
Junior Poster in Training
54 posts since Apr 2008
Reputation Points: 6
Solved Threads: 0
 

If your function returns an int, you coud do this:

if(0 != x)
{
   printf("I rounded it to (%d)\n\n", round2int(x,roundednum));
}
thines01
Postaholic
Team Colleague
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
 
How can I print it from the main? I know it sounds stupid, but I am still a beginner to the language.


There are three conventional options for accessing the calculated result in a function from the calling function:Return the result from your function:

#include <stdio.h>
#include <math.h>

float round2int(float test)
{
    return floor(test + 0.5);
}

int main (void)
{
    float x;
    
    while (1) {
        printf("Enter a number (Enter zero (0) to quit program): ");
        scanf("%f", &x);
        
        if (x == 0)
            break;
            
        printf("%f\n", round2int(x));
    }
    
    return 0;
}
Pass a pointer to the destination object as an argument: #include <stdio.h> #include <math.h> void round2int(float test, float *result) { *result = floor(test + 0.5); } int main (void) { float x, result; while (1) { printf("Enter a number (Enter zero (0) to quit program): "); scanf("%f", &x); if (x == 0) break; round2int(x, &result); printf("%f\n", result); } return 0; } (not recommended) Use a global variable: #include <stdio.h> #include <math.h> float result; void round2int(float test) { result = floor(test + 0.5); } int main (void) { float x; while (1) { printf("Enter a number (Enter zero (0) to quit program): "); scanf("%f", &x); if (x == 0) break; round2int(x); printf("%f\n", result); } return 0; }
Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

Thanks Narue .. Is there any reason why the 3rd is not recommended?.. since it was the one I learned in my notes..

Anyways the 'Return Method' will always work and I understood that now(for the current problems lol :P

terence193
Junior Poster in Training
54 posts since Apr 2008
Reputation Points: 6
Solved Threads: 0
 
Is there any reason why the 3rd is not recommended?.. since it was the one I learned in my notes..


Global variables are difficult to work with in larger programs because they can be accessed from anywhere. Bugs where you find yourself asking "where did that variable get changed?" aregreatly complicated by variables with unrestricted access.

Anyways the 'Return Method' will always work


Not always, but it should be your go to method.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: