Question:
C Language: How to use "User defined Functions" in another program?
Reddy
2011-11-28 18:10:05 UTC
/* program to print Welcome message */
#include

void p()
{
printf("Welcome");
}

void main()
{
p();
}

==

I saved the above program as "p.c"

but the new program is not working why?:
-------------------------------

#include
#include"p.c"
void main()
{
p();
}

-------------------------------

Compile error message:

"Body has already been defined for function 'main()' "
Four answers:
Silent
2011-11-28 18:16:45 UTC
Well, like the error message says, you have a function called main() in both places. Why is main() defined in both files?



As an aside, the return type of main() must always be int in standard C. Your compiler may allow you to use void, but this is bad practice. A standard-compliant program should return an int, where 0 indicates success and any other value indicates some type of failure.



Edit:



If you want to talk to me, you can either:



1. Email me through my profile, or

2. Edit your question to include more information.



Please do not login with other accounts and answer your own question. It's difficult to follow and against the rules.



You need to define main() in one place only. Don't remove it entirely.
chrisholm
2016-10-24 05:50:14 UTC
it relatively is unquestionably worry-loose to share some applications between diverse courses. There are some the variety to try this. despite the fact that, the superb way is to create a library. First, some clarification. once you're making a C software, you have a variety of of .c information. those are your source information containing your C source code. those are compiled into merchandise information via the compiler. the item information commonly are .o or .obj information watching the compiler. information like important.c are used to create important.o. the item information incorporate gadget language, that's the language of the CPU. After the item information are created, they must be mixed to make the basically authentic executable. you spot, the item information at the instant are not likely finished. If one merchandise document is attempting to call a functionality in yet another merchandise document, the compiler can not incredibly make that take place. So it places a marker indicating what it desires to do and leaves that very final determination to the linker step. The final section is stated as linking. that's like doing connect the dots. The linker unearths all the applications in all the item information then unearths all the places the place they're stated as and inserts the certainly calls to the applications. An merchandise document may well be utilized in extra beneficial than one software via specifying it to the linker. Programmers are continuously attempting to make existence much less complicated. and because this sharing of code is worry-loose, they got here up with the thank you to make this technique much less complicated. merchandise information may well be mixed right into a library. on the link section, relatively of linking with the item information, the library is used. there is not something that particular a pair of library. it is not that lots distinctive than a zipper document that includes diverse information. utilising a library or set of merchandise information quickly is the comparable ingredient. actually, a software may well be equipped (and in many circumstances is) from countless merchandise information and libraries. all the applications presented via C come from libraries. The c software language has no equipped-in applications. So once you write a C software and phone printf, you're already utilising a library. the variety you create and use libraries will fluctuate from one compiler to a distinctive. examine examine examine. solid success.
Last wish
2011-11-28 18:43:18 UTC
Your issue is that you have 2 main functions..



you only need 1 main and a function to call upon.





So first you need to declare your function that you plan to call.. usually before the main. Then you need you initialize it after the main. I think you probably need to see an example to get it, so i wrote you up a really really simple program that gives the parameter of a square using ONLY function calls in the main...









#include

#include

#include





void int_side1(void); // declaring the function to get side 1. ( they need the ";" )

void int_side2(void); // declaring the fuction to get side 2



int calc(void); // declaring the function to do the calculation



int side1, side2,total; // global variables



int main (void)



{

int_side1(); // the called function for side 1

int_side2(); // the called function for side 2

calc(); // the called function for the calculations





getch();





}



// these are the functions that im calling

void int_side1(void)

{

printf("Insert side 1 of rectangle\n");

scanf("%d",&side1);



}



void int_side2(void)

{

printf("nsert side 2 of the rectangle\n");

scanf("%d",&side2);



}

calc(void)

{



int result; // this is the local variable... only can be used in this function exclusively.

result = 2 * (side1 + side2);

printf("The parimeter of teh retangle is %d \n",result);











return (result); // here is the return result... which is the answer.



}







global variables can be used everywhere....



local variables can only be used int the function its in..





Notice the main () does nothing but call functions? ... look at the program and try to notice whats going on
Gautham Reddy
2011-11-28 18:24:55 UTC
Well, like the error message says, you have a function called main() in both places. Why is main() defined in both files?

===============



@Silent:



What is the solution?



Without main() program is not working


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...