Swap values of two variable function call by value program in C C++

Swap values of two variable function call by value program in C C++

C Language के बारे में अधिक जानने के लिए देखें—Brief Introduction of C

C और C++ दोनों भाषाओं में महत्वपूर्ण अंतर क्या है जानने के लिए देखें—C vs C++

Function में call by value और call by reference क्या होता है जानने के लिए देखें—Call by value and reference

Swap values of two variable function call by value program in C

#include<stdio.h>
void swap(int,int);
int main()
{
    int a,b;
    printf("Enter a b: ");
    scanf("%d%d",&a,&b);
    printf("Before swapping values in main a = %d b = %d\n",a,b);
    swap(a,b);
    printf("After swapping values in main a = %d b = %d\n",a,b);
}
void swap (int a, int b)
{
    int temp;
    temp=a;
    a=b;
    b=temp;
    printf("After swapping values in function a = %d b = %d\n",a,b);
}

Swap values of two variable function call by value program in C++

#include<iostream>
using namespace std;
void swap(int,int);
int main()
{
    int a,b;
    cout<<"Enter a b: ";
    cin>>a>>b;
    cout<<"Before swapping values in main a = "<<a<<" b = "<<b<<endl;
    swap(a,b);
    cout<<"After swapping values in main a = "<<a<<" b = "<<b<<endl;
}
void swap (int a, int b)
{
    int temp;
    temp=a;
    a=b;
    b=temp;
    cout<<"After swapping values in function a = "<<a<<" b = "<<b<<endl;
}

Output of Swap values of two variable function call by value program in C C++

Callbyvalue program output in C CPP

Useful function programs in C C++

  1. Sum of two numbers using function
  2. Average of three numbers using function
  3. Swapping using function call by reference
  4. Passing array as function argument
  5. Passing structure as function argument
  6. Pointer to Function
Share it to:

Published by

admin

I am a computer teacher, programmer and web developer