0% found this document useful (0 votes)
0 views6 pages

Practical 7 (Swap)

This document outlines a practical exercise for swapping two numbers using call by reference in C programming. It explains the concepts of standard library functions and user-defined functions, including their prototypes, definitions, and calls. The document also provides an algorithm, flowchart, and code examples in both C and Python for implementing the swap functionality.

Uploaded by

ajaydhapate16
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views6 pages

Practical 7 (Swap)

This document outlines a practical exercise for swapping two numbers using call by reference in C programming. It explains the concepts of standard library functions and user-defined functions, including their prototypes, definitions, and calls. The document also provides an algorithm, flowchart, and code examples in both C and Python for implementing the swap functionality.

Uploaded by

ajaydhapate16
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Practical No.

Aim: Program to swap two numbers using call by reference(Swapping of two numbers)

Theory:

a function is a block of code that perform a specific task and it runs only when it is called.There
are basically two types of function:

1.Standard library function:


The standard library function are built in function in c programming.This means that we
do not have to write a definition or the function’s body to call them. We can simply call them
without defining them as they are already defined. However, we need to include the library at the
beginning of the code for calling a library function. We can then use the proper syntax of the
function to call them. Printf(), scanf(), ceil(), and floor() are examples of library functions.

2. User-Defined Functions
These are the functions that a developer or the user declares, defines, and calls in a
program. This increases the scope and functionality, and reusability of C programming as we can
define and use any function we want. A major plus point of C programming is that we can add a
user-defined to any library to use it in other programs.

The user-defined function in C can be divided into three parts:


1. Function Prototype
2. Function Definition
3. Function Call

Function Prototype:

A function prototype is also known as a function declaration which specifies the function’s
name, function parameters, and return type. The function prototype does not contain the body
of the function. It is basically used to inform the compiler about the existence of the
user-defined function which can be used in the later part of the program.

Syntax

return_type function_name (type1 arg1, type2 arg2, ... typeN argN);

Function Definition:

Once the function has been called, the function definition contains the actual statements that will
be executed. All the statements of the function definition are enclosed within { } braces.
Syntax

return_type function_name (type1 arg1, type2 arg2 .... typeN argN) {

// actual statements to be executed


// return value if any
}

Function Call:

In order to transfer control to a user-defined function, we need to call it. Functions are called
using their names followed by round brackets. Their arguments are passed inside the brackets.

Syntax

function_name(arg1, arg2, ... argN);


Passing Parameters to User-Defined Functions:
We can pass parameters to a function in C using two methods:
1. Call by Value
2. Call by Reference

1. Call by value

In call by value, a copy of the value is passed to the function and changes that are made to the
function are not reflected back to the values. Actual and formal arguments are created in
different memory locations.
E.g z=add(a,b);

2. Call by Reference

In a call by Reference, the address of the argument is passed to the function, and changes that are
made to the function are reflected back to the values. We use the pointers of the required type to
receive the address in the function.
E.g swap(&a,&b)
Algorithm:
Step 1: Start
Step 2: Set a <-10 and b <-20
Step 3: call the function swap(&a,&b)
Step 3a: start function
Step 3b: Assign t <- *x
Step 3c: Assign *x <- *y
Step 3d: Assign *y <- t
Step 3e: End function
Step 4: Print x and y.
Step 5: End

Flowchart:
Result:
In this way, we have implemented swapping of two numbers using call by reference.
Name:
PRN:
Branch:
Aim: Program to swap two numbers using call by reference(Swapping of two numbers)
using C.

#include <stdio.h>
swap (int *, int *);
int main()
{
int a, b;
printf("\nEnter value of a & b: ");
scanf("%d %d", &a, &b);
printf("\nBefore Swapping:\n");
printf("\na = %d\n\nb = %d\n", a, b);
swap(&a, &b);
printf("\nAfter Swapping:\n");
printf("\na = %d\n\nb = %d", a, b);
return 0;
}
swap (int *x, int *y)
{
int temp;
temp = *x;
*x = *y;
*y = temp;
}

Output:
Name:
PRN:
Branch:

Aim: Program to swap two numbers using call by reference(Swapping of two numbers)
using python.

def swap_numbers(a, b):


a, b = b, a
return a, b

num1 = float(input("Enter the first number: "))


num2 = float(input("Enter the second number: "))

num1, num2 = swap_numbers(num1, num2)

print("After swapping:")
print("First number:", num1)
print("Second number:", num2)

Output:

Enter the first number: 5


Enter the second number: 10
After swapping:
First number: 10.0
Second number: 5.0

You might also like