| 1. |
Explain the definition of the user-defined function and with the concept of arguments and return value with example. |
|
Answer» Functions with argument and with return value: The calling function gives function call to the called function by passing arguments or values. Then the called function takes those values and performs the calculations and a return a value, that value will be sent back to the calling function. This is done using return statement. For example: # include <iostream> void sum(int x,int y) { int sum; sum = x + y; return(sum); } int main() { void sum (int,int); int a,b sumv; cout <<"enter the two number"; cin>>a>>b; sumv = sum (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, int 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. |
|