#C subroutines?
14 messages in this thread
Ok folks I'm stumped. Are Subroutines possible in C programs and if so how
do you define a subroutine and more importantly how do you CALL a
subroutine? I have C source code to load and display a picture in a Window
I'd like to ADD it to the Source Code I have for the Tri Window prg. Any
help would be greatly appreciated.
Subroutines are not only possible in C, they are encouraged. Sample
subroutine to calculate the hypotinuse of a right triangle:
float hypot( float a, float b)
{
return sqrt(a*a + b*b);
}
This sample conforms to the ANSI C standard, and may not be familure to
those who know only the K&R (the old standard). The first line tells the
compiler to create a subroutine called hypot which will receive two
floating point numbers and return a floating point number. The
instructions for the subroutine are between the brackets. The only
instruction (this is a very simple instruction) says to return the result
of the square root of etc…
You purists will say "Ah, but sqrt takes a double and returns a double."
Correct, and that is why I used the ANSI definition. The compile will
take care of all the conversion for me.
To use the function, in the main program you would say something like
printf( "The three sides of the triangle are %9.4f, %9.4f, %9.4f\n",
x, 12.2, hypot( x, 12.2 ) );
Actually, and introductory C manual should have at least one chapter
devoted to function.
C you around.
Bill Roberts — Autopilot from Ellettsville, Indiana
Thanx for the help Bill. Problem is I haven't gotten around to getting a
Book on C, just been reading folks Source code and trying to figger it out
myself. Also using the ACE files avail here in Amigatech.
I'm using DICE so ANSI is what I'm using.
Would this be a valid subroutine?
iff
{
command
command
command etc etc etc
}
between the Qoutes I'd place the commands neccessary to load a 5 bit plane
image off a ramdisk then display in a window. The Source Code I put up
earlier is the Source code I wish to use as the basis. I want to load a
image into the TheMapWindow window of the prg. I always prefferred
learning by doing. Thanx again.
Peter,
PMFJI… your example is almost valid — what's missing is that you
need to tell the compiler what kind of value your function returns,
even if that is "void" ( == no value returned at all…) … also, you'll
need to indicate what the compiler should be expecting as calling
parameters, again, void if there are none. Some examples:
void say_help( void )
{
printf("Help!\n");
}
Note, this would be called from inside another routine by
…
say_help();
…
int itest( double r1, double r1 )
{
if (r1<r2) return 1;
else if (r2<r1) return -1;
else return 0;
}
to call that one,
…
double a, b;
int j;
…
j = itest(a,b);
…
etc, etc.
There are lots of considerations wrt variable scope and so forth that
would be covered in an introductory C book — I'd heartily recomend that
you get one. The local library should have a ton of them if you don't
want to spend buck$, but I use mine ALL the time (got K&R second edition,
which has ansi stuff in it — there are many others that people might
recomend if you wish).
Also, there used to be a 4-diskette c manual available to download… I
forget at the moment who by, but it was quite complete….
Good coding….
(AutoPilot'ed from)
JcP
Thanx Jeff. I have the 13 Disk C encyclopedia from Anders Bjerin(I think
thats his name). I'll try it, Tho the iff loader/displayer I have also
hasa some includes. Will this effect it's ability to be used as a
Subroutine?
Thanx
Yes, Anders Bjerin was the name I couldn't remember.
Now, if I understand your other question correctly, you have a situation
where you've got a c module that you want to call from another module —
maybe a main, or whatever — and the module that you want to call has
some
#include <some_garbage.h>
lines in it. Briefly, that shouldn't be a problem, as long as you have
the needed include files on your system in an appropriate place such that
your c compiler (actually the preprocessor, but that's another discussion)
can find them. In certain circumstances, you'd also have to #include
some of the files in your main code. An example of such might be when
one of the include files typedefs or #defines something that is used
as a parameter to the function(s) you want to call.
The other thing that might get'cha is if the code you're trying to call
has a main. That can usually be dealt with, too, by changeing it's name
and possibly parameters…. but again that's probably a topic for a
separate conversation.
Good Luck,
(AutoPilot'ed from)
JcP
Thanx for the info Jeff. Will try changing the main(); in the second set
of source code and see if things improve.
>> Would this be a valid subroutine?
>> iff
>> {
>> command
>> etc
>> }
You need a set of parens after iff to hold the parameters (even if you
don't have any parameters. Good form if you have no parameters is
iff( void )
Then someone reading your code later knows for sure you didn't have
parameters. Also, you need to tell what the function returns, for
example:
int iff( void )
if you return an integer. If you don't return something (equivalent to a
FORTRAN subroutine), use
void iff( void ).
A word of advice, don't pass things around in global variables. At work,
we are trying to update some code from PL/I to C, and most of the data is
passed in global variables. What a nightmare. If you want the subroutine
or function to use something, pass it as a parameter. If you need only
one thing back, return it with a function. If you need more than one
thing back, use parameters. This I have learned from twenty plus years as
a programmer.
Bill Roberts — Autopilot from Ellettsville, Indiana
Good advice, Bill.
DJ
The Subroutine I want will simply Load an IFF and Display it. it need not
return anything. Thanx for the help.
Was he talking about functions, or FORTRAN style subroutines? It is hard
to naively translate FORTRAN into C.
-ash
declaring a C subroutine (example)
int my_routine(char *p)
{
int i = 0;
while (*p)
p++;
i++;
}
return i; }
main() {
printf("%d\n", my_routine("hello")); }
should yield the output of 5.
To learn C programming, there is only one book that you need: The C programming
language, by Dennis Kernighan and Brian Ritchie.
Thanx for the info TJ
Not a problem.