| by Arround The Web | No comments

How to Call a Function in C?

Functions in any programming language provide a very good way of enhancing the modularity of the code which in turn favors code reusability. However, to use functions effectively in any programming language, you must know how to call them properly. Therefore, this guide will talk about the different ways of calling a function in the C programming language.

Ways of Calling a Function in the C Programming Language

There are two different methods of calling a function in the C programming language: call by value and call by reference. In the former one, we send a copy of the actual variable to the function while calling it. Because of this, any change in the value of this variable inside the body of the function is not reflected outside. On the other hand, in the latter case, we send a reference of the actual variable to the function while calling it which is why any change in the value of this variable inside the body of the function is also reflected outside. You will be able to understand this concept well by going through the following two examples.

Example # 1: The Call by Value Method

As the heading says, in this example, we will be calling a function by value. For that, we have created the C script shown in the image below:

In this script, we have defined two different integers and then printed their original values on the terminal so that we can easily compare them with their new values later on. After that, we have called the “newValues()” function. Inside this function, the value of the first variable is incremented by “2” and the value of the second variable is incremented by “4”. Then, we have printed the new values of these variables on the terminal. After that, again in the “main()” function, we have printed the values of these variables on the terminal to see if the change in their values is reflected outside the “newValues()” function or not.

To compile this example script, we have used the following command:

$ gcc function.c –o function

For running this script, we have used the command shown below:

$ ./function

Since we have called our function by value in this example, therefore, the changed values of our variables were only displayed during the execution of the function and not before and after calling the function. This can be seen in the following output:

Example # 2: The Call by Reference Method

Now, we will be using the very same script that we have designed in our first example. However, this time, we will be calling our function by reference. For that, you can take a look at the modified script shown in the image below:

In this script, instead of passing a copy of the variables to the “newValues()” function, we have passed their references to it. This slight change will be depicted in the output of our modified script.

You can verify from the output shown in the following image that this time, the changed values were also depicted outside the “newValues()” function once it was being called. It is so because, the change is made in the actual variables and not in their copies.

Conclusion

This article was written to give you an idea about the different ways in which you can call a function in the C programming language. There are primarily two ways of doing so: call by value and call by reference. By reading this article, you will be able to learn both these methods along with their significance in the C programming language.

Share Button

Source: linuxhint.com

Leave a Reply