I guess I don't fully understand the way functions in C work.
For instance, I'll use this program as an example: -
#include <stdio.h>
-
-
float celsius(int);
-
-
main()
-
{
-
-
int a;
-
-
for(a = 0; a <= 300; ++a)
-
celsius(a);
-
-
return 0;
-
}
-
-
float celsius(int fahr)
-
{
-
int a, b;
-
-
a = (5.0 / 9.0) * (fahr - 32.0);
-
-
b = printf("%d\n", a);
-
-
return b;
-
}
-
So now, what I don't understand is, the data types. We declare the parameter fahr to be of the int data type. But now, what if I were to (say for instance, in the main function) put a variable as an argument for the celsius function, I don't understand how it works when say for instance, I were to declare this variable as another data type than what fahr is.
Also, I don't understand the return statement, what if I return a variable of type int, but the function is of another data type? hm.
Also, the data types are switched up in this program for a reason. :P
6 1475
I am not getting your question fully.
With whatever i have understood i have given my explanation
1)I think you are asking what will happen if you passa float variable to a function accepting int or vice versa
==>Then the variable will be automatically converted.
If you pass 1.1 to a function which accepts int then it will be truncated and accepted as 1
Same is the case when returning values
If your question is something else then post again
Raghuram
Thanks for the reply friend.
Well, I don't understand functions fully. If I use the return statement to return a variable as type int to a function that is for instance type float, what happens? It is converted? Why do we even declare a function to be any data type? Do we declare them as a data type so that it knows how to store the value the return statement gives it?
Also, I don't understand parameters for functions. If I put a value for this function as an argument for this function, it will be stored in fahr as int correct? Well what happens when I put a variable as the argument? And of a different data type?
Thanks for the reply friend.
Well, I don't understand functions fully. If I use the return statement to return a variable as type int to a function that is for instance type float, what happens? It is converted? Why do we even declare a function to be any data type? Do we declare them as a data type so that it knows how to store the value the return statement gives it?
Also, I don't understand parameters for functions. If I put a value for this function as an argument for this function, it will be stored in fahr as int correct? Well what happens when I put a variable as the argument? And of a different data type?
you are specifying data type so that the compiler can allocate that many number of bytes to it.
i cant get uyour second para fully
Raghuram
Well, what I was talking about in the second paragraph is...
For instance in this program, we have fahr (line 16) declared as int. But say for instance, in line 11, if that variable a was a variable of type float. What happens? I really don't understand entering variables as the argument.
I picture it like this, the variable a's value is now stored in fahr, and converted to the type of fahr (if its a different data type, in this program, fahr and the variable a are both of type int) and then that value is computed. etc. is that right?
Well, what I was talking about in the second paragraph is...
For instance in this program, we have fahr (line 16) declared as int. But say for instance, in line 11, if that variable a was a variable of type float. What happens? I really don't understand entering variables as the argument.
I picture it like this, the variable a's value is now stored in fahr, and converted to the type of fahr (if its a different data type, in this program, fahr and the variable a are both of type int) and then that value is computed. etc. is that right?
Yes.
read my previous post for more info..
1)I think you are asking what will happen if you passa float variable to a function accepting int or vice versa
==>Then the variable will be automatically converted.
If you pass 1.1 to a function which accepts int then it will be truncated and accepted as 1
Same is the case when returning values
Raghuram
Data types in C and C++ are broken into two families: integer and floating point.
Integers are of different sizes (short, int, long, char, etc.) and floating point has different sizes (float, double, long double, etc.).
With integers, any integer can be converted to any other integer and it is your responsibility to be certain the result is OK. That is, converting a larger integer to a smaller one may cause a data loss.
With floating point, when you convert from a large one (double) to a smaller one (float), the number of signifcant figures chnages and you will get a warning about truncation an possible loss of data. This warning does not occur going th other way.
Converting an integer to floating point results in a floating point variable with a zero decimal portion. However, due to the number of significant digits in your integer, you could see rounding and loss of accuracy.
Converting a floating point to an integer always produces a warning about truncation and possible loss of data since integers have no decimal portion.
When you code, try to stick with one family and do not mix integers and floating point. And when you do use floating point, unless there is a reason that you can write down on paper, use double.
Finance and programs dealing with money should use integers. Keep the amounts in pennies. Using floating point for money (because of the nifty decimal point) will cause you trouble when the variables are rounded- and there is no way to turn that off.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: David MacQuigg |
last post by:
I'm not getting any feedback on the most important benefit in my
proposed "Ideas for Python 3" thread - the unification of methods and
functions. Perhaps it was buried among too many other less...
|
by: Aguilar, James |
last post by:
I've heard that virtual functions are relatively ineffecient, especially
virtual functions that are small but get called very frequently. Could
someone describe for me the process by which the...
|
by: Jon Wilson |
last post by:
I have a class which needs to accumulate data. The way we get this data
is by calling a member function which returns float on a number of
different objects of different type (they are all the...
|
by: Ann Huxtable |
last post by:
I have the following code segment - which compiles fine. I'm just
worried I may get run time probs - because it looks like the functions
are being overloaded by the return types?. Is this Ok: ?
...
|
by: York |
last post by:
Hi,
R language has very high-level IO functions, its read.table can read a
total .csv file and recogonize the types of each column. write.table can
do the reverse.
R's MySQL interface has...
|
by: No One |
last post by:
Here is my problem:
I have a certain set of well-defined manipulations that I have to apply
to different types of data. In all cases the manipulations are exactly the
same, and are to be...
|
by: Mikhail Teterin |
last post by:
Hello!
I'd like to have a variable of a pointer-to-function type. The two possible
values are of type (*)(FILE *) and (*)(void *). For example:
getter = straight ? fgetc : gzgetc;
nextchar...
|
by: Ross A. Finlayson |
last post by:
Hi,
I hope you can help me understand the varargs facility.
Say I am programming in ISO C including stdarg.h and I declare a
function as so:
void log_printf(const char* logfilename, const...
|
by: bahadir.balban |
last post by:
Hi,
What's the best way to implement an overloaded function in C? For
instance if you want to have generic print function for various
structures, my implementation would be with a case...
|
by: Bern McCarty |
last post by:
I have a DLL written in C++ (it's really C code that was adjusted to compile
OK as C++) that I compile successfully into IL with the /CLR switch of
Visual C 7.1. I use the resultant library...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |