The primary perform is an integral a part of most programming languages corresponding to C, C++, and Java. This perform is the entry degree of a program the place the execution of a program begins. It’s a user-defined perform that’s necessary for the execution of a program as a result of When a C program is executed, the working system begins executing this system on the essential() perform.
Syntax of the principle perform in C:
return_type essential(){
// Assertion 1;
// Assertion 2;
// and so forth..
return;
}
We are able to write the principle perform in some ways in C language as follows:
int essential(){} or int essential(void){}
essential(){} or void essential(){} or essential(void){} or void essential(void){}
Within the above notations, int means integer return kind, and void return kind means that doesn’t return any info, or (void) or () means that doesn’t take any info.
Key factors about the principle perform:
- It’s the perform the place this system’s execution begins.
- Each program has precisely one essential perform.
- The title of this perform ought to be “essential” not anything.
- The primary perform at all times returns an integer worth or void.
- The primary perform is named by OS, not the consumer.
Sorts of Fundamental Operate
- Fundamental perform with void return kind
- Fundamental perform with int return kind
- Fundamental perform with the argument
- Fundamental perform with multiple argument and int return kind
- Fundamental perform with multiple argument and void return kind
1. Fundamental perform with void return kind
Let’s see the instance of the principle perform inside which we have now written a easy program for printing a string. Within the under code, first, we embody a header file after which we outline a essential perform inside which we write a press release to print a string utilizing printf() perform. The primary perform is named by the working system itself after which executes all statements inside this perform.
C
|
2. Fundamental perform with int return kind
On this instance, we use the int return kind in the principle() perform that signifies the exit standing of this system. The exit standing is a method for this system to speak to the working system whether or not this system was executed efficiently or not. The conference is {that a} return worth of 0 signifies that this system was accomplished efficiently, whereas another worth signifies that an error occurred.
C
|
3. Fundamental perform with the argument
On this instance, we have now handed some arguments in the principle() perform as seen within the under code. These arguments are referred to as command line arguments and these are given on the time of executing a program. The primary argument argc means argument depend which implies it shops the variety of arguments handed within the command line and by default, its worth is 1 when no argument is handed. The second argument is a char pointer array argv[] which shops all of the command line arguments handed. We are able to additionally see within the output after we run this system with out passing any command line argument the worth of argc is 1.
C
|
The worth of argc is 1 ./369df037-e886-4cfb-9fd4-ad6a358ad7c6
Now, run the applications within the command immediate or terminal as seen under screenshot and handed any arguments. essential.exe is the title of the executable file created when this system runs for the primary time. We handed three arguments “geeks for geeks” and print them utilizing a loop.

4. Fundamental perform with multiple argument and int return kind
This perform is much like the principle perform with an int return kind however this perform signifies that the perform doesn’t take any arguments. On this instance, the principle perform is just like the int essential(void ) which reveals that it returns an integer worth with out passing any arguments to this perform.
C
|
5. Fundamental perform with multiple argument and void return kind
This perform is much like the principle perform with a void return kind however this perform signifies that the perform doesn’t take any arguments. On this instance, we have now outlined the principle perform because the void essential(void ) which reveals that it doesn’t return any worth and in addition we aren’t passing any arguments to this perform.
C
|