### Thread: Function help

1. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Jun 2013
Location
Dhaka, Bangladesh
Posts
116
Rep Power
2

#### Function help

Help me write a function that converts a temperature measurement from celsius to fahrenheit. The formula is, Celsius = (5/9)(Fahrenheit -32).
Here's my attempt:
Code:
```int convert(int i)
{
int p;
p = ((9i/5) + 32);
return p;
}```
2. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Jan 2013
Posts
159
Rep Power
20
And do you have a problem with your attempt? If so please ask specific questions.

Jim
3. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Jun 2013
Location
Dhaka, Bangladesh
Posts
116
Rep Power
2
Originally Posted by jimblumberg
And do you have a problem with your attempt? If so please ask specific questions.

Jim
Yes I am having a problem. Here's the entire code:
Code:
```#include <stdio.h>

main()
{
int i;
for (i = 0; i <= 300; ++i)
printf("%d %d\n", i, convert(i));
return 0;
}
int convert(int i)
{
int p;
p = ((9i/5) + 32);
return p;
}```
I only get a series of 32.
4. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Jan 2013
Posts
159
Rep Power
20
Let's start by looking at the following line:
Code:
`p = ((9i/5) + 32);`
First in C/C++ and most other programming languages you must specify the operand, (9 * i).

Next this is integer math, so remember there are no fractions. 9/5 will yield 1. To get fractional amounts you mus use floating point numbers.

Jim
5. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Jun 2013
Location
Dhaka, Bangladesh
Posts
116
Rep Power
2
Originally Posted by jimblumberg
Let's start by looking at the following line:
Code:
`p = ((9i/5) + 32);`
First in C/C++ and most other programming languages you must specify the operand, (9 * i).

Next this is integer math, so remember there are no fractions. 9/5 will yield 1. To get fractional amounts you mus use floating point numbers.

Jim
Here's my revised code:
Code:
```#include <stdio.h>

main()
{
int i;
for (i = 0; i <= 300; ++i)
printf("%6.3f %6.3f\n", i, convert(i));
return 0;
}
int convert(int i)
{
float p;
p = ((9*i/5) + 32);
return p;
}```
Now all I get is a series of 0.000s
6. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Jan 2013
Posts
159
Rep Power
20
Your function is returning an integer, but your printf() is trying to print a floating point number. The printf() function requires that the format specifier match the variable type. If they don't match you won't get the expected results.

I really suggest you change your function to return a floating point number and use floating point numbers in that function. Remember to also change your constants to floating point by specifying the value with a decimal point (9.0).

Jim
7. No Profile Picture
I haz teh codez!
Devshed Frequenter (2500 - 2999 posts)

Join Date
Dec 2003
Posts
2,565
Rep Power
2338
You skipped part of jimblumberg's response. I'll requote it for you.

Next this is integer math, so remember there are no fractions. 9/5 will yield 1. To get fractional amounts you mus use floating point numbers.
Change 5 to 5.0 and see what happens.
8. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Jun 2013
Location
Dhaka, Bangladesh
Posts
116
Rep Power
2
Originally Posted by jimblumberg
Your function is returning an integer, but your printf() is trying to print a floating point number. The printf() function requires that the format specifier match the variable type. If they don't match you won't get the expected results.

I really suggest you change your function to return a floating point number and use floating point numbers in that function. Remember to also change your constants to floating point by specifying the value with a decimal point (9.0).

Jim
If I try to compile this:
Code:
```#include <stdio.h>

main()
{
int i;
for (i = 0; i <= 300; ++i)
printf("%6.3f %6.3f\n", i, convert(i));
return 0;
}
float convert(float i)
{
float p;
p = ((9.0*i/5.0) + 32.0);
return p;
}```
it says "Conflicting types for 'convert'" for
Code:
`float convert(float i)`
and, "previous implicit declaration of 'convert' was here" for
Code:
`printf("%6.3f %6.3f\n", i, convert(i));`
:(
9. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Jun 2013
Location
Dhaka, Bangladesh
Posts
116
Rep Power
2
Sorry if I sound too dumb. I am new in this field.
10. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Jan 2013
Posts
159
Rep Power
20
Your function needs a function prototype before main(). Remember your functions and variables must be defined before they can be used.

Also look at this snippet:
Code:
```	int i;
...
printf("%6.3f %6.3f\n", i, convert(i));```
What would be the proper format specifier for i, which happens to be an int? Remember integers don't have fractional parts and that printf() insists that the format string match the variables.

Jim

#### Comments on this post

• arman.khandaker agrees : Thanks a tonne for giving me time!
11. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Jun 2013
Location
Dhaka, Bangladesh
Posts
116
Rep Power
2

Originally Posted by jimblumberg
Your function needs a function prototype before main(). Remember your functions and variables must be defined before they can be used.

Also look at this snippet:
Code:
```	int i;
...
printf("%6.3f %6.3f\n", i, convert(i));```
What would be the proper format specifier for i, which happens to be an int? Remember integers don't have fractional parts and that printf() insists that the format string match the variables.

Jim
The code finally worked!
Code:
```#include <stdio.h>

float convert(float i)
{
float p;
p = ((5.0/9.0)*(i - 32.0));
return p;
}

main()
{
float i;
for (i = 0; i <= 300; ++i)
printf("%1.1f %1.1f\n", i, convert(i));
return 0;
}```
I also changed the formula.
Thanks a lot for helping me around! :)
12. Code:
```#include <stdio.h>

main()
{
int i;
for (i = 0; i <= 300; ++i)
printf("%6.3f %6.3f\n", i, convert(i));
return 0;
}
float convert(float i)
{
float p;
p = ((9.0*i/5.0) + 32.0);
return p;
}```
Look at the warnings that you get:

C:>gcc -Wall convert.c
convert.c:4: warning: return-type defaults to `int'
convert.c: In function `main':
convert.c:7: warning: implicit declaration of function `convert'
convert.c:7: warning: double format, different type arg (arg 2)
convert.c:7: warning: double format, different type arg (arg 3)
convert.c: At top level:
convert.c:11: warning: type mismatch with previous implicit declaration
convert.c:7: warning: previous implicit declaration of `convert'
convert.c:11: warning: `convert' was previously implicitly declared to return `int'

C:>
convert.c:7: warning: implicit declaration of function `convert'
The compiler reads from the top of the file to the bottom. When it tries to call convert(), it has no idea what you are talking about because you haven't told it anything about convert. Old legacy behavior causes C to assume that you're talking about a function that takes one int argument and returns an int. That assumption caused the following warnings:
convert.c:7: warning: double format, different type arg (arg 3)
convert.c: At top level:
convert.c:11: warning: type mismatch with previous implicit declaration
convert.c:7: warning: previous implicit declaration of `convert'
convert.c:11: warning: `convert' was previously implicitly declared to return `int'
When the compiler reached the actual declaration for convert, that actual declaration conflicted with the assumed implicit declaration you had forced on the compiler previously, so naturally the compiler complained.

You need to either move convert above main or else you need to place a function prototype before main; eg:
Code:
```#include <stdio.h>

float convert(float i);

main()
{
int i;
for (i = 0; i <= 300; ++i)
printf("%6.3f %6.3f\n", i, convert(i));
return 0;
}
float convert(float i)
{
float p;
p = ((9.0*i/5.0) + 32.0);
return p;
}```
Please pay especial attention to the fact that the function prototype ends in a semicolon, but the actual function itself does not have a semicolon there.

convert.c:7: warning: double format, different type arg (arg 2)
is because i is an int but you're telling printf to treat it like a floating-point value. That will cause really weird results. It's an int, so treat it like an int!

convert.c:4: warning: return-type defaults to `int'
What's the return type of main? It should be int, so why don't you declare it as int? That's that old legacy defaulting undeclared identifiers as int that created a generation of lazy C programmers. Do not be lazy! Declare main to be int!
13. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Jun 2013
Location
Dhaka, Bangladesh
Posts
116
Rep Power
2

Originally Posted by dwise1_aol
Code:
```#include <stdio.h>

main()
{
int i;
for (i = 0; i <= 300; ++i)
printf("%6.3f %6.3f\n", i, convert(i));
return 0;
}
float convert(float i)
{
float p;
p = ((9.0*i/5.0) + 32.0);
return p;
}```
Look at the warnings that you get:

convert.c:7: warning: implicit declaration of function `convert'
The compiler reads from the top of the file to the bottom. When it tries to call convert(), it has no idea what you are talking about because you haven't told it anything about convert. Old legacy behavior causes C to assume that you're talking about a function that takes one int argument and returns an int. That assumption caused the following warnings:

When the compiler reached the actual declaration for convert, that actual declaration conflicted with the assumed implicit declaration you had forced on the compiler previously, so naturally the compiler complained.

You need to either move convert above main or else you need to place a function prototype before main; eg:
Code:
```#include <stdio.h>

float convert(float i);

main()
{
int i;
for (i = 0; i <= 300; ++i)
printf("%6.3f %6.3f\n", i, convert(i));
return 0;
}
float convert(float i)
{
float p;
p = ((9.0*i/5.0) + 32.0);
return p;
}```
Please pay especial attention to the fact that the function prototype ends in a semicolon, but the actual function itself does not have a semicolon there.

convert.c:7: warning: double format, different type arg (arg 2)
is because i is an int but you're telling printf to treat it like a floating-point value. That will cause really weird results. It's an int, so treat it like an int!

convert.c:4: warning: return-type defaults to `int'
What's the return type of main? It should be int, so why don't you declare it as int? That's that old legacy defaulting undeclared identifiers as int that created a generation of lazy C programmers. Do not be lazy! Declare main to be int!
Thanks for your analysis and advice. I'll keep that in mind! :)