C Program to Swap Two Numbers Using Functions

C Program to swap two numbers using functions. In this program, we can swap two numbers using functions for beginners to understand the function concepts in c.

Function in C

A function is defined as a relationship between a set of inputs and a set of possible outputs, with each input relating to exactly one output. 

A function is a collection of statements that work together to complete a task. 

Every C program has at least one function, main(), and even the simplest programs can define more functions. 

The name, return type, and parameters of a function are all specified in a function declaration.

Temporary Variable in C

A temporary variable is a variable in computer programming that has a short lifetime and is used to hold data that will be discarded soon or before it can be stored in a more permanent memory location.

It is usually declared as a local variable, or a variable with local scope, because it is short-lived.

C Program to Swap Two Numbers Using Functions

#include<stdio.h>
void swap(int *,int *);
int main()
{
int n1,n2;
printf("Swap The Numbers\n");
printf("First Number");
scanf("%d",&n1);
printf("Second Number");
scanf("%d",&n2);
printf("Before Swapping n1 = %d, n2 = %d ",n1,n2);
swap(&n1,&n2);
printf("\nAfter Swapping n1 = %d, n2 = %d \n\n",n1,n2);
return 0;
}
void swap(int *p,int *q)
{
int tmp;
tmp = *p; 
*p=*q; 
*q=tmp; 
}

Output:

Swap The Numbers
First Number45
Second Number67
Before Swapping n1 = 45, n2 = 67
After Swapping n1 = 67, n2 = 45