Every program has to have at least one function to define the entry point of the application. In C, we call this the main function and it has two possible prototypes:
int main (void);
int main (int argc, char* argv[] );
The arguments to main are parameters that we can pass from the command line when we execute the program. The first prototype has no arguments (void means no type) and is used in programs that do not require any command line parameters. The second is for those that do. The first argument, argc, tells us the number of arguments that were passed from the command line while the second refers to a null-terminated array of null-terminated character arrays (strings) containing those arguments. The count is always at least 1 because the first argument (argv[0]) always refers to the command itself (e.g., the full path and file name of the executable). argv[argc] is always a null-terminator, thus argv[1] through argv[argc-1] refer to the individual arguments.
The purpose of the main function is to process the command line arguments (if any) and coordinate the program's execution. When execution has completed, the main function returns a value to the hosting environment. Typically we return 0 to indicate the program executed successfully (no error) and any non-zero value to indicate some user-defined error level.
Although we can write an entire program using just the main function alone, non-trivial applications have to make use of functions otherwise we'd end up writing extremely low-level code that is only slightly more abstract than assembly language. Without functions, code would be highly repetitive, tedious to write, prone to error and difficult to maintain.
To reduce the tediousness of code repetition, code that is invoked more than once (outside of a looping structure) can be separated out to create a function with a user-defined name. We use the name to call the function. Functions can also be generalised to accept arguments thus we don't have to write separate functions for code that only differs by which variables it operates upon, we can simply pass those variables to one copy of function. Be eliminating the need for duplicate code, we greatly reduce the cost of maintenance; if we need to modify the code in any way we only need to make the change in one place.
In C we use a bottom-up approach to function design. First we define our low-level functions and then we use these functions as the building blocks for more complex, higher-level functions. In this way, our main function becomes the highest level function.
High-level functions are an abstraction; they allow us to name a block of code such that the name closely reflects the purpose of the code. In this way, we do not have to examine every line of code in order to understand the program; knowing what a function does is usually more important than how it does it. Thus our code becomes easier to read. Code that is easy to read is also easy to maintain.
When we invoke a function, execution automatically returns to the calling code when the function ends. Functions can also return a value to their callers. A function can therefore be thought of as being a miniature program in its own right.
Functions tend to contain very little code. Some will only have one line of code. Many of these are used purely to invoke a more complex line of code, thus making code easier to write. However, as a general rule, we try to keep functions small enough so that we can see the entire function without having to scroll. We can achieve this very easily by refactoring complex code as a function.
Although there is a performance penalty in invoking (and returning from) functions, by keeping functions as simple as possible we can take advantage of inline expansion. That is, the compiler can replace function calls with the function's actual code, substituting its formal argument names for the actual arguments. This naturally creates duplicate code, however duplicate code is only a "bad thing" when we write it ourselves, because we are then forced to maintain all instances of it. Enlisting the compiler to generate duplicate code for us means we gain the advantage of faster execution without the added cost to maintenance. Of course, inline expansion also results in larger code and that can have an impact upon performance as well, however the compiler has built-in optimisers that can determine when inline expansion is appropriate.
The call and return mechanism itself is relatively simple. It is achieved through the use of an area of memory known as the call stack. Every thread of a process has its own dedicated stack, thus there is no problem when two threads concurrently invoke the same function. The call stack is allocated when a thread is invoked and remains in memory until the thread terminates. Thus there is no cost involved in using the stack, we simply need to keep track of the top of the stack, which is achieved through a (hidden) pointer.
When we invoke a function, we need to know the address of the function. The compiler can determine the address from the function's name alone, however we can also use function pointers to invoke a function (function pointers allow us to pass functions to functions). Before we pass control to a function address, we must push the return address onto the stack. The return address is the address of the next machine code instruction that follows the function call. Again, the compiler can determine that address for us. When we pass control to the function address, the body of the function executes. When it is ready to return to its caller, the return address is simply popped from the stack and control returned to that address. This mechanism means that functions can call functions without losing track of where calls came from, much like a breadcrumb trail.
The call stack is also used to pass arguments to functions. The arguments passed to a function are called the actual arguments while the arguments used by a function are the formal arguments. All arguments are passed by value in C and this is achieved by pushing those values onto the stack. When the function gains control, those values are assigned to its appropriate formal arguments. The stack is also used to allocate the function's non-static local arguments (automatic variables). Remember that no allocation actually takes place when we push values onto the stack, all we really do is assign values to the address pointed to by the stack pointer and then increment it accordingly. To release the memory, we simply decrement the pointer.
When a function returns a value, that value is temporary. If we do not assign the value to a variable upon returning from a function, the value is lost. In addition, all automatic variables fall from scope when a function returns, thus we cannot return a reference to a local variable. The value may very well be available on the stack beyond the stack pointer, however that memory can no longer be regarded as being valid; it may be overwritten at any time. Thus all return values must be returned by value. If we need a value to persist beyond the scope of the function, it must be allocated on the free store rather than the call stack. We can then return a pointer (by value) to that allocation.
A main function must be present in every C program.
the main() function,the#include Directive, the variable definition. function prototype, program statements, the function definition,program comments and braces are the components of program in C++
Every C plus plus program that is a main program must have the function 'main'.
In C and C++, as well as in many (all?) languages, a function can be called from more than one place in a program. That's the purpose of functions - to encapsulate pieces of code that are needed in more than one place in the program.
Every C program must have a function, named main(), which is where the program starts execution. If there is no function main(), the computer does not know where to start running the program. The function main() must also do something; if it is just empty, some smarter compilers will note that there is nothing for the program to do, and will give this sort of error message to indicate that you forgot to tell the program what to do.
Every C program has a main() function.
Into the source program.
A main function must be present in every C program.
if you do not used main function in c program when errors are accrued
That's up to you, but the execution of the program begins with function main.
the main() function,the#include Directive, the variable definition. function prototype, program statements, the function definition,program comments and braces are the components of program in C++
Whatever the programmer wants it to be.
It is not possible. In C, any program must have at least one function to be able to compile and execute properly: the function main()
Every C plus plus program that is a main program must have the function 'main'.
In C and C++, as well as in many (all?) languages, a function can be called from more than one place in a program. That's the purpose of functions - to encapsulate pieces of code that are needed in more than one place in the program.
C programs do not function without functions.
Functions are very important in C++, as you can't write the simplest program to print hello without using a function. Overall you can say that function are building blocks of a C++ program. Functions can also be defined by the programmer to reduce program size.