Function overloading is a very well-known concept used in object-oriented languages having many functions with the same name and different parameters in a single code. The object-oriented programming languages which support function overloading include Java and C . As the C compiler doesn’t allow it to be used in the code hence, it isn’t easy to implement function overloading in C. Yet; we can still achieve the same thing with some technique. Let’s start this article with the opening of the shell terminal of Ubuntu 20.04 LTS.

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.

$ touch test.c

$ 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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/1-55.jpg616b5779273fb.jpg" data-lazy- height="272" src="data:image/svg xml,” width=”529″>

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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/2-53.jpg616b57792f76f.jpg" data-lazy- height="364" src="data:image/svg xml,” width=”708″>

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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/3-51.jpg616b577938b7a.jpg" data-lazy- height="381" src="data:image/svg xml,” width=”650″>

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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/4-49.jpg616b57793f3cd.jpg" data-lazy- height="76" src="data:image/svg xml,” width=”346″>

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.

About the author

<img alt="" data-lazy-src="https://secure.gravatar.com/avatar/d014e3711df41253029f4d4199698df8?s=112&r=g" data-lazy- height="112" src="data:image/svg xml,” width=”112″>

Kalsoom Akhtar

Hello, I am a freelance writer and usually write for Linux and other technology related content