CSC115 ยท 2081 ยท 10 marks
Differentiate between library function and user defined function.Write a program to swap two values using call by reference concept.[2+8]
Differentiate between library function and user defined function.Write a program to swap two values using call by reference concept.[2+8]
Quick answer
(a) Difference Between Library Function and User Defined Function
Difference Between Library Function and User Defined Function + Swap Using Call by Reference
As per the course notes, C programs have two types of functions: Library Functions and User Defined Functions.
| S.N. | Library Function | User Defined Function |
|---|---|---|
| 1. | A predefined function stored in a header file or preprocessor directive. | Not predefined; it is defined by the programmer according to need. |
| 2. | Programmer can simply use this function by including the respective header file. | Programmer has to declare, define, and call this function by themselves. |
| 3. | Program is usually short as the programmer does not have to define the function. | Program is usually lengthy as the programmer has to define the function. |
| 4. | Program development time is faster. | Program development time is usually slower. |
| 5. | Program will be simple. | Program will be complex. |
| 6. | Requires a header file to use it (e.g., #include<stdio.h>). | Requires a function prototype to use it. |
Examples:
- Library functions:
printf(),scanf(),sqrt(),strlen()- User defined functions: any function written by the programmer for a specific task
(b) Program to Swap Two Values Using Call by Reference
Concept: Call by Reference
In call by reference, instead of passing the actual values to the function, the addresses (pointers) of the variables are passed. This means any changes made inside the function directly affect the original variables in the calling function.
Program
#include<stdio.h>
#include<conio.h>
/* Function prototype (declaration) */
void swap(int *a, int *b);
int main()
{
int x, y;
printf("Enter first value : ");
scanf("%d", &x);
printf("Enter second value : ");
scanf("%d", &y);
printf("\nBefore Swap: x = %d, y = %d\n", x, y);
/* Passing addresses of x and y - Call by Reference */
swap(&x, &y);
printf("After Swap: x = %d, y = %d\n", x, y);
getch();
return 0;
}
/* Function definition */
void swap(int *a, int *b)
{
int temp;
temp = *a; /* temp stores value at address a */
*a = *b; /* value at address a = value at address b */
*b = temp; /* value at address b = temp */
}
Step-by-Step Explanation
| Step | Statement | What Happens |
|---|---|---|
| 1 | swap(&x, &y) | Addresses of x and y are passed to the function |
| 2 | int *a, int *b | Pointer a holds address of x; pointer b holds address of y |
| 3 | temp = *a | temp gets the value stored at address a (i.e., value of x) |
| 4 | *a = *b | Value at address a (i.e., x) is replaced by value at address b (i.e., y) |
| 5 | *b = temp | Value at address b (i.e., y) is replaced by temp (original x) |
Sample Output
Enter first value : 10
Enter second value : 20
Before Swap: x = 10, y = 20
After Swap: x = 20, y = 10
Key Point
In call by reference, the function works with the actual memory locations of the variables. Therefore, the swap is reflected back in
main(), unlike call by value where only copies are passed and the original values remain unchanged.
Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.