Question:
Functions in C Programming?
2010-04-02 17:51:15 UTC
I'm having a hard time understanding functions: creating your own, calling them, and whatever else is involved.

The program I'm trying to work on uses the int main() to guide the flow of events. Then I have an input function, a processing function, and an output function.

I kind of understand the processing function and how that works.

I'm having trouble understanding how you call an input function. When I was looking around online I would see how your would take in some input in the int main() section and then pass those values to a function which you call...

But you're not really passing anything to the input function are you? Aren't you just trying to call it so int main() diverts to that function in the program? Then you pass the input values from the function to the processing function???

I hope I made some sense, and if I could get a really simplified example of some code then I might be able to figure it out from there. Maybe a little info on the workings of C in this instance because I don't really get it...

Thanks!
Four answers:
?
2010-04-02 18:27:27 UTC
What you've described, a main() that includes a setup function, a processing function and a cleanup function fits nearly 100% of the application programs I've ever seen. They just don't all have all four functions; sometimes, for instance, the setup is so simple it's done in the main() or the processing function.



But the three functions under main() must have some way to communicate forward (setup to processing; setup and processing to cleanup). There are basically three ways to do this:



1. Via global memory. (Frowned upon in structured programming because access to it can't be restricted to only some functions.)

2. Via local memory in the main() function, handed to the subordinate functions via function arguments. (Sometimes global memory variables are also handed off this way.)

3. Via external files. (Relatively sl-o-o-o-w.)



So to answer your question about passing anything to the input (setup -- because it doesn't always do input. Often the processing function does that.) function, yes, there are often call-by-reference parameters passed to it so they can come back and be available to the processing function. Likewise, the processing function may pass back values or structures for the cleanup function to use.



Hope that helps.
?
2016-10-14 09:31:23 UTC
what's a function? A function is a block of code that has a attractiveness and it has a components that it is reusable i.e. it is achieved from as many distinctive factors in a C application as required. function communities fairly some application statements right into a unit and provides it a attractiveness. This unit could be invoked from different factors of a application. a working laptop or computing gadget application won't be able to cope with each and all the initiatives by making use of it self. instead its requests different application like entities – referred to as applications in C – to get its initiatives carried out. A function is a self contained block of statements that carry out a coherent activity of comparable style The call of the function is unique in a C application and is international. It neams that a function could be accessed from any region with in a C application. We bypass suggestions to the function referred to as arguments particular whilst the function is named. And the function the two returns some fee to the factor it became referred to as from or returns not something. we are able to divide an prolonged C application into small blocks that may carry out a definite activity. A function is a self contained block of statements that carry out a coherent activity of comparable style. shape of a function There are 2 significant factors of the function. The function header and the function physique. int sum(int x, int y) { int ans = 0; //holds the respond which would be returned ans = x + y; //calculate the sum return ans //return the respond } function Header interior the 1st line of the above code int sum(int x, int y) It has 3 significant factors The call of the function i.e. sum The parameters of the function enclosed in paranthesis return fee style i.e. int function physique What ever is written with in { } interior the above occasion is the physique of the function. function Prototypes The prototype of a function can supply the uncomplicated suggestions some function which tells the compiler that the function is used wisely or not. It incorporates the comparable suggestions because of the fact the function header incorporates. The prototype of the function interior the above occasion could be like int sum (int x, int y); the only distinction between the header and the prototype is the semicolon ; there could desire to the a semicolon on the top of the prototype. satisfied studying, solid success :)
?
2010-04-02 18:28:00 UTC
I really have no clue what you are talking about...here is the main function there's only one prototype (one correct prototype that is):



int main(int argc, char* argv[])



argc - number of command line arguments including program name (you might say that no arguments and argc = 1)



argv - this is an array of strings which are the command line arguments, argc is the length of this array (again it's always at least of length 1)
TR Hale
2010-04-02 18:05:40 UTC
more info needed. can not compute. error error.

what world are you from?


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