C Programming - Functions - Discussion

Discussion Forum : Functions - True / False Questions (Q.No. 5)
5.
Functions can be called either by value or reference
True
False
Answer: Option
Explanation:

True, A function can be called either call by value or call by reference.

Example:

Call by value means c = sub(a, b); here value of a and b are passed.

Call by reference means c = sub(&a, &b); here address of a and b are passed.

Discussion:
7 comments Page 1 of 1.

Nikhil laad said:   1 decade ago
The above explanation is incorrect. There are 3 calls(call by value, call by address, call by reference). In call by address, address of variables is passed, and they are accepted by the pointers.But in call by reference, function call is same as call by value.

Explanation program:

#include<stdio.h>
using namespace std;
void swap(int,int);
void swapa(int *,int *);
void swapr(int &,int &);
main()
{
int a=10,b=20;
swap(a,b);
swapa(&a,&b);//call by address
swapr(a,b);
}
void swap(int a,int b)
{
int temp;
temp=a;
a=b;
b=temp;
printf("a=%d,b=%d\n",a,b);
}
void swapa(int *a,int *b)
{
int temp;
temp=*a;
*a=*b;
*b=temp;
printf("a=%d,b=%d\n",*a,*b);
}
void swapr(int &a,int &b)
{
int temp;
temp=a;
a=b;
b=a;
printf("a=%d,b=%d\n",a,b);
}

Rupam said:   9 years ago
The basic difference between by value and by reference is the creation of new variables. In case of by value, each time you passed actual parameter to calling function, a brand new copies of variables are created for formal parameters in called function. But in case of by reference, no new variable (no new memory allocation) should be created.

Rupam Mukhopadhyay said:   9 years ago
There is no concept of call by reference in C. There is no option of creating reference variables is C. All the C program support is by value.

Diego said:   7 years ago
Is it mean that the ARGUMENTS of a function can be called, not the function itself?

Dhnesh said:   9 years ago
Call by value and call by address can be used in the same function.

Kumar harsh said:   9 years ago
In call by reference is not possible.

Nick said:   1 decade ago
What is call by reference?

Post your comments here:

Your comments will be displayed after verification.