Linux C Programming Tutorial Part 8

In our first a part of this ongoing tutorial sequence, we mentioned briefly about features, together with their declaration and physique. What we did not focus on at the moment was the a number of methods during which features are referred to as. Right here, on this tutorial, we’ll shortly focus on the ways in which exist.

In C language, you possibly can name a operate in a few methods: name by worth and name by pointer or tackle. Let’s focus on each these ideas with some simple to grasp examples.

For instance you wish to write a program that swaps two values. This is how you are able to do it:

#embody <stdio.h>

int major()
{
int a=0, b=0, c=0;

printf("Enter two integer valuesn");

scanf("%d %d",&a,&b);

printf("Entered values are: %d and %d", a,b);

c = a;
a = b;
b = c;

printf("nSwapped values are: %d and %d", a,b);

return 0;
}

This is the output of this code with entered values as 5 and 9:

Enter two integer values
5 9

Entered values are: 5 and 9
Swapped values are: 9 and 5

Now, suppose the requirement is to have a separate operate – say ‘swap’ – which does all of the swapping associated work and could be referred to as each time the programmer desires to swap two values. Following is the code that does this:

#embody <stdio.h>

void swap (int val1, int val2)
{
int temp = 0;

temp = val1;
val1 = val2;
val2 = temp;

printf("nSwapped values are: %d and %d", val1,val2);

}

int major()
{
int a=0, b=0;

printf("Enter two integer valuesn");

scanf("%d %d",&a,&b);

printf("Entered values are: %d and %d", a,b);

swap(a,b);

return 0;
}

So there you go. A separate operate named ‘swap’ has been created that receives two values (initially entered by consumer and captured in ‘major’ operate) as arguments after which swaps them and prints the output. 

The best way ‘swap’ has been referred to as right here is named ‘name by worth’. Motive being, when the decision is made, solely the values held by ‘a’ and ‘b’ are handed to the ‘swap’ operate as arguments. These values are acquired by arguments ‘val1’ and ‘val2’, and it is these variables on which the swap course of is carried out. 

Which means that variables ‘a’ and ‘b’ within the ‘major’ operate proceed to carry authentic values even after the swap operation has been carried out. However what if the requirement is to have values of ‘a’ and ‘b’ swapped after calling the ‘swap’ operate? Properly, that is the place the ‘name by pointer/tackle’ technique comes into image.

So mainly what we do right here is, we cross the tackle of variables (like ‘a’ and ‘b’ in our case) as arguments. The functionwhich is named (‘swap’ on this case) is supplied to obtain addresses as arguments after which the swap course of is completed on the values stored at these addresses, which successfully means values of authentic variables (‘a’ and ‘b’ right here) get swapped.

Now, within the earlier paragraph, we stated the operate “is supplied to obtain addresses as arguments.” Properly, by “equipped,” we meant it has particular sort of arguments that may obtain addresses. These arguments are ‘pointer’ sort variables. We’ll focus on ‘pointers’ intimately in an upcoming tutorial, however for now, simply take into account that pointer variables retailer reminiscence addresses as values.

This is how a pointer to an integer is said/outlined:

int *x;

So mainly, x is a pointer variable that can be utilized to retailer reminiscence tackle of an integer variable. Suppose ‘i’ is an integer variable, then this is how one can make ‘x’ retailer the tackle of ‘i’:

x = &i;

And everytime you wish to entry the worth of ‘i’ via ‘x’, you write ‘*x’. For instance, this is how one can change the worth of ‘i’ to, say, 10:

*x = 10;

So with all this in thoughts, this is how one can name ‘swap’ utilizing the decision by tackle or pointer technique:

#embody <stdio.h>

void swap (int *val1, int *val2)
{
int temp = 0;

temp = *val1;
*val1 = *val2;
*val2 = temp;

}

int major()
{
int a=0, b=0, c=0;

printf("Enter two integer valuesn");

scanf("%d %d",&a,&b);

printf("Entered values are: %d and %d", a,b);

swap(&a,&b);

printf("nSwapped values are: %d and %d", a,b);

return 0;
}

So this time, as a substitute of passing values of ‘a’ and ‘b’ as arguments, we handed the addresses of those variables. In ‘swap’ operate, the addresses are acquired in two pointer variables (‘val1’ and ‘val2’). And utilizing the 2 pointer variables, the logic immediately swaps values of ‘a’ and ‘b’. 

This is the output:

Enter two integer values 
6 8
Entered values are: 6 and eight
Swapped values are: Eight and 6

This text ought to’ve given you at the very least a primary thought of what ‘name by worth’ and ‘name by tackle/pointer’ methods of operate calling are, and after they can be utilized. Check out the examples we have listed right here and tell us in feedback under when you’ve got any doubt or question.

LEAVE A REPLY

Please enter your comment!
Please enter your name here