+2 votes
in Class 11 by kratos

Explain Functions with argument and with return value.

1 Answer

+3 votes
by kratos
 
Best answer

The calling function gives a function call to the called function by passing arguments or values. Then the called function takes those values and performs the calculations and returns a value, which will be sent back to the calling function. This is done using the return statement.

For example,

include

void sum(intx,int y)

{

int sum;

sum = x + y;

return(sum);

}

int main ()

{

void sum (int,int);

int a, b, sumv;

cout <<"enter the two numbers";

cin>>a>>b;

cout <<" The sum of two numbers"<<sumv;

return 0;

}

In the above example, sum() is a user defined function void sum(int x, ini y)having two arguments. The statement int a,b,sumv; declare local variables in main() function. The statement sumv=sum (a, b); is a function call and actual arguments a and b values are sent to formal arguments x and y which is the local variable to sum() function. The statement return(sum); takes the control back along with the value present in the sum variable and assign it to the sumv variable in the calling function main(). This is how the function with argument and with return value works.

...