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

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

C Language में Character Array या String किसे कहते है जानने के लिए देखें हमारा यह पोस्ट— Strings in C

Function क्या है इसका प्रयोग क्यो किया जाता है यह कितने प्रकार का होता है जानने के लिए देखें—Functions in C

C Language में Structure क्या होता है और इसका प्रयोग क्यो किया जाता है जानने के लिए देखें—Structures in C

Swap values of two variable function call by reference 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 reference 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 reference program in C C++

Callbyref program output in C CPP

Important 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 value
  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