Syntax
The syntax for the function prototype has been shown in the image.
- Return_Type: It shows the function return type, i.e., int.
- Function_name: It shows the function name i.e. user-defined functions.
- Type: Within the brackets, this keyword shows the type of argument to be passed.
- Parameter: This keyword shows the argument to be passed in the function.
Example 01: Without Function Prototype
Let’s create a new file test.c illustrating the C extension at the end of its name via the “touch” query. In this sample example, we will see how the code works without the function prototype.
Open the test.c file to add some C script in it.
We have included the input-output stream header at the start of a C code. After that, we have defined the main method to start the code execution from here. Two variables have been initialized and passed to the user-defined function “Max”. This is the function call. The control has been given to the “Max” function. Within the “Max” function, the condition has been applied to both the values passed in the arguments. This function will check the greater value and return the largest value to the main method. The main method has defined another variable, “v” to save the returned value. The print statement would be utilized to show the maximum value. You can view that there is no function prototype in this code.
Now, compile the code with C compiler, i.e., GCC alongside the title of a file “test.c”. The output shows the warning that we have missed the “Max” function prototype declaration. On the other hand, it works fine when we have executed the code using the “a.out” command.
$ ./a.out
Let’s update the code by replacing the positions of the functions. We have added the user-defined function “Max” before the main method() and afterward the header.
You can see that the interpreting and execution of a code don’t show any warning.
$ ./a.out
Example 02: With Function Prototype
Let’s have one more sample example of function prototype declaration within the C code. So, opened the same file once again.
This code contains the function prototype declaration at the start of the code before the main() method. The declaration contains the function’s return type as “int” before the function name “Max”. Two integer-type arguments n1 and n2, have been declared. The implementation of the main() and Max() methods is the same as in the above example. The main thing is that the user-defined method “Max()” has been defined after the main() method.
When the code file got interpreted, it did not show up any warning. This means that we have used a proper and correct way to use the function prototype in the code. The execution of the code shows the maximum value is 18.
$ ./a.out
Example 03
Let’s have another example of creating and using the function prototype in the C code. So, we have created another C file named “new.c” by using the instruction displayed in the image below.
After that, the newly created file has been opened in the GNU nano editor using the instruction shown in the snapshot.
We have added a function prototype for the user-defined function New() after the header line. This prototype declaration has no arguments. So, the user-defined method will not be taking any arguments from the main method. The main method is utilized to call the New() method. The New() method has been displaying the value of a variable and its increment.
After the interpretation, the execution of this code worked fine and displayed the variable value along with its incremented value.
$ . /a.out
Conclusion:
This article contains the making of a function prototype in C language. The examples include the explanation of how the code works with and without function prototype declaration. It explains how the function prototype can be used in the case of a user-defined function being defined before and after the main method. The example codes have been explained in detail with implementation screenshots. Now, to fully understand the function prototype in C, implement all these samples codes on your system.