Page 111 - 6437
P. 111
avg = (double)sum / size;
return avg;
}
When the above code is compiled together and executed, it produces the following result:
Average value is: 214.40000
Return Pointer from Functions
We have seen in the last chapter how C programming allows to return an array from a
function. Similarly, C also allows to return a pointer from a function. To do so, you would have to
declare a function returning a pointer as in the following example:
Second point to remember is that, it is not a good idea to return the address of a local
variable outside the function, so you would have to define the local variable as static variable.
int * myFunction()
{
.
#include <stdio.h>
.
#include <time.h>
.
}
/* function to generate and retrun random numbers. */
int * getRandom( )
{
static int r[10];
int i;
Now, consider the following function which will generate 10 random numbers and return
them using an array name which represents a pointer, i.e., address of first array element.
114