Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do you create your own functions?

Status
Not open for further replies.

bnhshallow

Programmer
Dec 14, 2001
12
US
Hey everyone, im trying to create my own functions but i cant find any books or anything that tells me how.
help me plz. thx, BNH
 
If all your code is in one file, then declare the function definition near the top and the implementation down below:


/* include files */
#include "stdio.h"


/* function definitions */
int MultiplyByTwo(int iValue);


/* main function */
int main()
{
int iMyValue = 25;
int iNewValue = 0;

/* call your function */
iNewValue = MultiplyByTwo(iMyValue);

/* use the new value however you want */
printf("%i multiplied by two is %i\n", iMyValue, iNewValue);
}


/* your function implementation /*
int MultiplyByTwo(int iValue)
{
return (iValue * 2);
}

If your code is split into separate files, then create a header file (i.e. MyApplication.h) and put the function definition (but not the implementation) in that file. #include "MyApplication.h" at the top of your main .c file and in any other .c file that uses your function.
 
i tried your syntax but the compiler gave me warnings and removed the program. if its not too much trouble, do you think you could post an example sorce code.

Thanks
 
That *was* example source code.

Please tell me what warnings the compiler gave you.

It might be that it "removed the program" because the program actually ran okay within a dos window, but when it finished the dos window went away?

Are you in Windows using Microsoft Visual C++? Or Borland C? Or what...
 
im using a program i downloaded from the internet called LCC-win32 or something like that. the warning was:
line 21: missing return value {

i just checked a lot of stuff and looked in the internal program launcher and it does work, but for some wierd reason id removes the program.
 
One thing I forgot that I should have added at the end of the main() function was the statement "return (0);" Here's the whole main() function again with that correction:


/* main function */
int main()
{
int iMyValue = 25;
int iNewValue = 0;

/* call your function */
iNewValue = MultiplyByTwo(iMyValue);

/* use the new value however you want */
printf("%i multiplied by two is %i\n", iMyValue, iNewValue);

return(0);
}


Also, if you *are* in Windows using Visual C++, set a "break point" on the newly added return(0) statement so the DOS window will stick around long enough for you to see the result.
 
i Get it now thanks alot. i reread a part in "The absolute beginners Guide to C" and now its alot clearer.

Thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top