Pre-requisites
- Install Ubuntu 20.04 LTS
- Install GCC compiler
- Get sudo rights access
Example 01: Function Overloading in C
Let’s start our first example to see how function overloading simply fails in C language. We have to utilize the “touch” instruction within the terminal shell to create a new C-type document. To open this new file, you need a file editor. We have been utilizing the “GNU Nano” editor to open it.
Execute the following affixed instructions to get started.
$ nano test.c
After launching the file in an editor, we have included as we have defined two user-defined standard input-output headers. After the header, we have defined two user-defined methods with a similar name and different parameters, i.e., “cal”. The first user-defined method, “cal” takes 1 argument of integer type, while the other “cal” method takes two arguments of integer type. The first function has been simply multiplying the passed integer value with itself and saving it to another integer type variable, “t” that has just been initialized.
After this, the calculated value would be displayed in the shell via the “printf” statement used within it. The other function takes two integer values in its argument and uses the passed values to calculate the total by multiplying both. The calculated values would be printed out in the shell as per the “printf” statement.
Within the main() method, we have been calling both methods one after another. The first method has been called while having 1 parametric integer value in it. The other same name function has been called while passing two integer type values in its parameters. The main method gets closed here.
Save this code and exit the file. Use the “GCC” compiler package to compile the code and see the function overloading errors. The command has been given in the image output below. You can see that compiling this file throws a lot of exceptions due to the same name of functions.
Execute the following affixed instruction.
Example 02: Alternative to Function Overloading
Within this example, we will be letting you know about the alternative method of implementing function overloading. We have opened the same file once more and added three header packages at the top of it with #include keyword. We have defined two user-defined functions of the same name with a single character change in them. Both function names start with “cal”.
The first method has been taking two integer type values as an argument in its parameters. While the other has been taking two character-type pointers as an argument in its parameters. The first “cal” method has been taking these two integer values and add them up together. The calculated values would be returned to the main method. On the other hand, the other “cal” method initialized another character type pointer, “z”.
The strlen() method is applied to both string variables to get lengths, and the malloc() method has been applied to length to get memory size. The pointer “z” has been getting memory via the malloc() method as it has to store the string value. The strcpy() method is used to copy the string value of the pointer variable “x” to “z”. While the strcat() method is used to append the value of the pointer variable “y” at the end of “z”. The updated value of “z” has been returned to the main() method. We have defined the function declaration with the “_Generic” keyword.
The main() method is declaring two integer variables to be passed to the first “cal()” method. Two character pointers have been initialized with string values and passed to the second “cal()” method.
Compiled the code with the “GCC” package. Upon the execution of this code, we got the sum of two integers and the concatenated string.
Execute the following affixed instructions to get started.
$ ./a.out
Conclusion:
This guide contains the illustration of using an alternative method of function overloading in C. We have covered the example to see how function overloading fails in C and how the _Generic term can save us. If you have gone through this guide carefully, you would be able to perform function overloading in C easily from now on. We hope that you may find this article best.
from https://ift.tt/3p9HMiJ
0 Comments