Function as mentioned earlier can return some value. The number of values that can be returned by a function is restricted to 1 in C. If a function calculates more number of output values, you can use a structure/union to hold each of them and return it. The type of value returned by the function must be defined in the function prototype and definition. To illustrate function with return value a simple program is presented below.
- #include<stdio.h>
- int add(int a, int b);
- void main()
- {
- int sum = 0;
- sum = add(23, 35);
- printf("Sum : %d", sum);
- }
- int add(int a, int b)
- {
- int c = a + b;
- return c;
- }
In the above example, the function
'add()'
takes two arguments of integer type and returns an integer value. Notice the return statement in the 'add()'
function which returns the value to the caller.
No comments:
Post a Comment