Types of Function in C Programming Example | Call by Value vs Call by Reference

Types of Function in C Programming Example | Call by Value vs Call by Reference

Do you know,

  • What are the different types of functions in C?
  • How to declare, define and call the function in C?
  • What is the difference between call by value and call by reference?

In this tutorial, I will explain all these points one-by-one with programming examples.

C language is a procedure-oriented programming language.

The program is divided into small blocks known as functions, which enhances the reusability and efficiency of the program.

What is the function in C?

A function is a block that contains a well-organized set of instructions that are performed whenever the function is called.

Example:

The best example of a function is the ‘main()’ function.

main() Function in C

What is special about the main function?

  • It is a special function because the execution of a C program starts from this function.
  • When all the instructions and function calls present in main() are executed, the C program ends.
  • Every C program must contain one and only one main() function.

Built-in Functions in C

Other examples of functions would be printf(), scanf(), getch(), etc.

These are the functions which are already defined to the compiler and are stored in libraries like stdio.h and conio.h.

User Defined Functions in C

However, we can always write user-defined functions as per our requirements.

There are two steps for establishing a function in C.

  • function declaration
  • function definition

Function Declaration in C

Function declaration means declaring the function by stating its return type, function name, and parameters.

What is the return type in function?

The return type specifies the nature of value returned by the function. The function might return anything like integers, characters, decimals, etc.

The return type void is used whenever the function returns nothing.

The parameters are the list of arguments which are fed as input to the program.

On the other hand, a function declaration means writing the body of the function.

Function declaration syntax-

return_type function_name(parameters);

Function definition syntax-

return_type function_name(parameters)
{
//body of function;
}

User Define Function Example:

For example, let’s consider a simple program that uses a user-defined function called ‘sum’ which add two integers and prints the sum.

#include<stdio.h>

void sum(int x, int y);

int main()
{
  int a1,b1,a2,b2,a3,b3;
  printf ("enter three pairs of inetegers");
  scanf(“%d%d%d%d%d%d”,&a1,&b1,&a2,&b2,&a3,&b3);
  printf("sum of the first pair is:");
  sum(a1,b1);
  printf("sum of the second pair is:");
  sum(a2,b2);
  printf("sum of the third pair is:");
  sum(a3,b3);
  return 0;
}

void sum(int x, int y)
{
  int s;
  s=x=y;
  printf(“%d”,&s);
}

Further, you can also classify the types of functions based on the number and types of input parameter or return type of the function.

Function calls and pointer:

In C programming, to execute a function, you need to call it.

In the case of a parameterized function, arguments need to pass while function call.

Now, there are two types of function calls–

  • call by value
  • call by reference

Difference between call by value and call by reference

Call by value

In call by value, a copied set of value of arguments is passed to the function.

Hence, all the operations performed on variables does not affect the original set of arguments.

For example: Write a C program to swap the values of variables using Call by Value.

#include<stdio.h>

void swap(int a, int b)

int main()
{
  int x=2,y=4;
  printf("Before swap:");
  printf("x=%d, y=%d",x,y);
  swap(x,y)
  printf("After swap:");
  printf("x=%d, y=%d", x,y);
  return 0;
}

void swap(int a, int b)
{
  int temp;
  temp=a;
  a=b;
  b=temp;
}

The above program passes the arguments x and y to the function called swap by a call by value method. The values of x and y are copied to a and b. Later the values of a and b are interchanged with the help of another integer variable called temp. First, the value of a is stored in temp, then the value of b is stored in a and at last, the value of temp is stored in b.

Call by reference

In call by reference, instead of the copied set, the address of the variables is passed to the function. Hence, the operations performed to change the values in the original set.

Now, whenever a variable is declared or assigned some value, the C compiler allocates a location for it in the memory. The address of this memory cell is a number. So, the data type which can store this address is known as a pointer. While declaring a pointer, the special character ‘*’ is used.

For example:

int *p;

The asterisk means ‘value at the address’.

Hence, *p means values at the address stored in p. Whereas, the & operator gives the address of a variable.

For example: Write a C program to swap the values of variables using Call by Reference or pointer.

Let’s see the swapping program but with the help of pointers this time. In this program, instead of passing the values of x and y, we pass the pointers which store the addresses of x and y.

#include<stdio.h>

void swap(int *p, int *q);

int main()
{
  int x=2, y=4;
  printf("Before swap:");
  printf("x=%d, y=%d",x,y);
  swap(&x,&y);
  printf("After swap:");
  prinytf("x=%d, y=%d",x,y);
}

void swap(int *a, int *b)
{
  int temp;
  temp=*a;
  *a = *b;
  *b = temp;
}

Conclusion

There are many advantages of writing your program with functions. They are very useful as it makes your program more modular.

If you are working on any project, create a separate function for similar logical operations. This will reduce the complexity of your program and it will also be easy for debugging.

This is all about different types of functions in C and the difference between call by value and call by reference. I have also explained it with programming examples.

If you have any specific questions related to the functions in C, write in the comment.

2 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *