Difference between call by value and call by reference
There are two types of parameter passing schemes.1. Call by value
2. Call by reference
Call by Value:
A copy of actual arguments are passed to the formal arguments. So, any modification to formal arguments won't affect the original value of actual arguments.
Example C program to illustrate call by value:
#include <stdio.h> /* swaps given two number */ void swap(int a, int b) { int tmp; tmp = a; a = b; b = tmp; printf("Inside swap() -> a = %d\tb = %d\n", a, b); return; } int main() { int a, b; printf("Enter two integers: "); scanf("%d%d", &a, &b); swap(a, b); // pass by value printf("Inside main() -> a = %d\tb = %d\n", a, b); return 0; }
Output:
jp@jp-VirtualBox:~/$ ./a.out
Enter two integers: 10 20
Inside swap() -> a = 20 b = 10
Inside main() -> a = 10 b = 20
Enter two integers: 10 20
Inside swap() -> a = 20 b = 10
Inside main() -> a = 10 b = 20
Call by Reference:
Here, the address of variables are passed from the actual argument to the formal argument. So, the called function acts on addresses rather than values. Here, the formal arguments have the pointer to the actual arguments. So, any change made to formal arguments reflect on actual arguments.
Example C program to illustrate call by reference
#include <stdio.h> /* swaps given two number */ void swap(int *a, int *b) { int tmp; tmp = *a; *a = *b; *b = tmp; printf("Inside swap() -> *a = %d\t*b = %d\n", *a, *b); return; } int main() { int a, b; printf("Enter two integers: "); scanf("%d%d", &a, &b); /* address of variable is passed to swap() */ swap(&a, &b); // pass by reference printf("Inside main() -> a = %d\t b = %d\n", a, b); return 0; }
Output:
jp@jp-VirtualBox:~/$ ./a.out
Enter two integers: 10 20
Inside swap() -> *a = 20 *b = 10
Inside main() -> a = 20 b = 10
Enter two integers: 10 20
Inside swap() -> *a = 20 *b = 10
Inside main() -> a = 20 b = 10
thank u..
ReplyDeletefor the first time i understood this...
Hello Everyone !
ReplyDeleteUSA SSN Leads/Dead Fullz available, along with Driving License/ID Number with good connectivity.
All SSN's are Tested & Verified.
**DETAILS IN LEADS/FULLZ**
->FULL NAME
->SSN
->DATE OF BIRTH
->DRIVING LICENSE NUMBER
->ADDRESS WITH ZIP
->PHONE NUMBER, EMAIL
->EMPLOYEE DETAILS
*Price for SSN lead $2
*You can ask for sample before any deal
*If you buy in bulk, will give you discount
*Sampling is just for serious buyers
->Hope for the long term business
->You can buy for your specific states too
**Contact 24/7**
Whatsapp > +923172721122
Email > leads.sellers1212@gmail.com
Telegram > @leadsupplier
ICQ > 752822040
Dell Laptop Service center are giving repair service at the door. We should high quality Dell out of warranty Laptop Repair, removal of virus, screen removal, wireless network set up, battery removal, motherboard replacement to several other are offered at budget friendly price and it’s Negotiable. We can fix them all in time by our well experience and certified technicians. If you want to repair your laptop in front of your eyesight, than you may call us: 7217871051
ReplyDelete