Starter Tutorials Blog
Tutorials and articles related to programming, computer science, technology and others.
Subscribe to Startertutorials.com's YouTube channel for different tutorial and lecture videos.
Home » Programming » C Programming » Programs » Functions » C program to demonstrate call by value and call by reference
Suryateja Pericherla Categories: Functions. No Comments on C program to demonstrate call by value and call by reference
Join Our Newsletter - Tips, Contests and Other Updates
Email
Name

In this article we will learn to implement a C program to demonstrate call by value and call by reference. A C program is provided below to illustrate how to use call by value and call by reference parameter passing mechanisms in case of functions.

 

Program is as follows:

/*
 * C program to demonstrate call by value and call by reference
 * Author: P.S.SuryaTeja
*/
#include <stdio.h>
#include <conio.h>
#include <math.h>
#include <stdlib.h>

void swapval(int x, int y)
{
	int temp;
	temp = x;
	x = y;
	y = temp;
}

void swapref(int *x, int *y)
{
	int temp;
	temp = *x;
	*x = *y;
	*y = temp;
}

int main(int argc, char **argv)
{
	int num1, num2;
	printf("Enter two numbers: ");
	scanf("%d%d", &num1, &num2);
	swapval(num1, num2);
	printf("\nAfter swapping using call by value, num1 = %d and num2 = %d", num1, num2);
	swapref(&num1, &num2);
	printf("\nAfter swapping using call by reference, num1 = %d and num2 = %d", num1, num2);
    getch();
    return 0;
}

 

Input and output for the above program is as follows:

Enter two numbers: 20 50

After swapping using call by value, num1 = 20 and num2 = 50
After swapping using call by reference, num1 = 50 and num2 = 20

 

How useful was this post?

Click on a star to rate it!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Leave a Reply

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

Facebook
Twitter
Pinterest
Youtube
Instagram
Blogarama - Blog Directory