Write a C program to add two numbers using call by value and reference.

Function Main
    Declare Integer n1, n2
    Output "This is a call by value add(2, 30):"
    Output add(2, 3)
    Output "Input first number (n1):"
    Input n1
    Output "Input second number(n2):"
    Input n2
    Output "This is a call by reference add(n1, n2):"
    Output add(n1, n2)
End

Function add (Integer first, Integer second)
    Declare Integer result
    Assign result = first + second
Return Integer result

#include <stdio.h>

int add(int first, int second);

int main() {
    int n1, n2;
    printf("This is a call by value add(2, 3):\n");
    printf("%d\n", add(2, 3));
    printf("Input first number(n1):\n");
    scanf("%d", &n1);
    printf("Input second number(n2):\n");
    scanf("%d", &n2);
    printf("This is a call by reference add(n1, n2):\n");
    printf("%d\n", add(n1, n2));
    return 0;
}

int add(int first, int second) {
    int result;
    result = first + second;
    
    return result;
}

This is a call by value add(2, 3):
5
Input first number(n1):
20
Input second number(n2):
30
This is a call by reference add(n1, n2):
50

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.