Write a program to differentiate call by value and call by reference in C language.

Tested on: Turbo C++ Version 4.0Time Complexity: O (1)Space Complexity: O (1)Open Source: MIT
1
2
    #include <stdio.h>
3
    #include <conio.h>
4
    void swapref(int *x, int *y)
5
    {
6
        int temp;
7
        temp = *x;
8
        *x = *y;
9
        *y = temp;
10
    }
11
    void swapval(int x, int y)
12
    {
13
        int temp;
14
        temp = x;
15
        x = y;
16
        y = temp;
17
    }
18
    void main()
19
    {
20
        int a = 5, b = 2;
21
        clrscr();
22
        printf("Before calling swapval function a=%d,b=%d", a, b);
23
        swapval(a, b);
24
        printf("\nAfter calling swapval function a=%d, b=%d", a, b);
25
        printf("\nBefore calling swapref function a=%d,b=%d", a, b);
26
        swapref(&a, &b);
27
        printf("\nAfter calling swapref function a=%d, b=%d", a, b);
28
    }
29
30
        

Output

1
2
        Before calling swapval function a=5,b=2
3
	After calling swapval function a=5, b=2
4
	Before calling swapref function a=5,b=2
5
	After calling swapref function a=2, b=5
6
7
   

Rate the Code

Reviews are published after a quick safety check (usually within 24 hours).

Reviews are public. Developers can see basic device specifications to respond. Learn more

How to run differentiate call by value and call by reference program in C-Language?

To run differentiate call by value and call by reference program in C-Language, follow the below mentioned steps:

  1. Copy the Code: Press the copy button in the code block to copy the complete code.
  2. Open Turbo C++ Version 4: Open the Turbo C++ (Version 4) software on your system.
  3. Paste the code: Paste the copied code into Turbo C++’s editor.
  4. Compile the Code: Compile the program by pressing the Alt+F9 keys. This shows you the errors and warnings, if any.
  5. Run the code: Press the Ctrl+F9 keys to run the program if the program is successfully compiled.
  6. Enter the Input: Provide the required inputs or your desired inputs, if applicable.
  7. Receive the Result: Get the result of the program on the screen based on the provided inputs.

More about Call by Value and Call by Reference in C language program

Aim: To demonstrate the difference between call by value and call by reference.

In this program, the two variables are swapped with the help of call by value and call by reference, so that you can easily understand the difference between call by value and call by reference.

Concepts used in this program

  • Call by value: A copy of the variables is provided; the original values stay unchanged.
  • Pointers: To store and access memory addresses.
  • Address operator (&): To pass the memory address to the function.
  • Dereference Operator: With the help of a pointer, the value of the original variable can be accessed and modified.
  • Call by reference (using pointers): To modify the value of the original variable.
  • Swapping: With the help of a temp variable, the values of the two variables are swapped.

Real-Life Applications

  • Guest Mode on a Smartphone:

Do you use Guest Mode or Guest Profile on your smartphone or any other gadget?

If someone uses a gadget in Guest Mode and modifies its settings, these modifications will usually stay within the scope of the Guest Profile. Once the gadget returns to the Original Profile, its original settings will remain unmodified.

Some settings might be common to both profiles. If one modifies these settings in the Original Profile, this modification will have an impact on the settings in the Guest Profile as well.

How does this relate to Call by Value and Call by Reference?

  • Call by Value: Similar in principle to having a copy of the settings that are modified independently from the original settings.
  • Call by Reference: Similar in principle to modifying data that is common for multiple parts of the program.
License: This program's source code is open-sourced under the MIT License, while the rest of the program's text content and overall dhimanp platform and brand content remain proprietary under dhimanp.

Submit your modified code

Your submissions are published after a quick safety check (usually within 7 days).

Submissions are public. Developers can see basic device specifications to respond. Learn more

Disclaimer

The information that you shared with us will be publicly visible, and everyone can see your information. And we (dhimanp) are not responsible if anyone misuses this information. We can remove any submitted code at any time for better transparency.

Privacy Policy

If you share your data with us by submitting your code for credit, then we may show your full name, school or college name, and city name on our website.

We are not using this data to track you and are not selling this data to anyone.

If you want to remove your name from our website, you can contact us, and we will remove your name from our website. In the removal process, full credit, including your full name, school or college, city, and code, will be removed from our website.