C Programming - Functions - Discussion

5. 

Functions can be called either by value or reference

[A]. True
[B]. False

Answer: Option A

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.


Nick said: (Mar 12, 2013)  
What is call by reference?

Nikhil Laad said: (Oct 21, 2013)  
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 Mukhopadhyay said: (Mar 20, 2015)  
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.

Rupam said: (Mar 20, 2015)  
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.

Kumar Harsh said: (Aug 7, 2015)  
In call by reference is not possible.

Dhnesh said: (Nov 2, 2015)  
Call by value and call by address can be used in the same function.

Diego said: (Feb 22, 2017)  
Is it mean that the ARGUMENTS of a function can be called, not the function itself?

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.