Board index » cppbuilder » How to wirte a simple function

How to wirte a simple function


2003-10-06 08:10:10 PM
cppbuilder58
Hello !!
Please could anybody tell me how to write a simple function that I can use
it many times and call it from other events ? Lets say how can we write a
function that sums varibales ?
Best regards
Cagdas
 
 

Re:How to wirte a simple function

Quote
Please could anybody tell me how to write a simple function
that I can use it many times and call it from other events ?
Lets say how can we write a function that sums varibales ?
This returns the sum of 4 variables:
Put this function prototype in a header file that is included into the
calling source file. If a separate header file is not used then put it at
the top of the source file immediately after the list of includes.
int Sum4(int a, int b, int c, int d);
and this goes into the source file which contains the function.
int Sum4(int a, int b, int c, int d)
{
return a + b + c + d;
}
The following can be used to return the sum of 2, 3, or 4 variables
depending upon the number of calling arguments you use:
Put this function prototype in a header file that is included into the
calling source file. If a separate header file is not used then put it at
the top of the source file immediately after the list of includes.
int SumUpTo4(int a, int b, int c = 0, int d = 0);
and this goes into the source file which contains the function.
int SumUpTo4(int a, int b, int c, int d)
{
return a + b + c + d;
}
Because the last two variables in SumUpTo4 have default values then you can
call it with 2, 3 or 4 calling arguments and the compiler will substitute
the default values for any calling parameters which are not specified. This
allows the same function to be used to sum of 2, 3 or 4 variables. The
default values must be in the function prototype seen by the calling code.
. Ed
Quote
Johny Walker wrote in message
news: XXXX@XXXXX.COM ...