Functions in C help programmers to apply modular program design. A function can be defined to accept one or more than one arguments but it can return a single value to the calling environment. However, the function can be defined to return an array of values.
In C, a function can be made to return an array by one of following methods −
- Passing an array as argument and returning the pointer
- Declaring a static array in a function and returning its pointer
- Using the malloc() function
- Embedding an array inside a struct variable and passing it to a function
We implement these methods to calculate the square, the cube, and the square root of a given number.
Passing an Array by Reference
In the following example, we declare an uninitialized array in main() and pass it to a function along with an integer. Inside the function, the array is filled with the square, cube, and square root. The function returns the pointer of this array, using which the values are access and printed in the main() function.
Example
Take a look at the example −
#include <stdio.h>#include <math.h>intarrfunction(int,float*);intmain(){int x =100;float arr[3];arrfunction(x, arr);printf("Square of %d: %f\n", x, arr[0]);printf("Cube of %d: %f\n", x, arr[1]);printf("Square root of %d: %f\n", x, arr[2]);return0;}intarrfunction(int x,float*arr){
arr[0]=pow(x,2);
arr[1]=pow(x,3);
arr[2]=pow(x,0.5);}
Output
When you run this code, it will produce the following output −
Square of 100: 10000.000000
Cube of 100: 1000000.000000
Square root of 100: 10.000000
Return a Static Array
Instead of passing an empty array from main(), we can declare an array inside the called function itself, fill it with the required values, and return its pointer. However, returning a pointer of a local variable is not acceptable, as it points to a variable that no longer exists.
Note that a local variable ceases to exist as soon as the scope of the function is over. Hence, we need to use a static array inside the called function (arrfunction) and return its pointer back to the main() function.
Example 1
Take a look at the example −
#include <stdio.h>#include <math.h>float*arrfunction(int);intmain(){int x =100, i;float*arr =arrfunction(x);printf("Square of %d: %f\n", x,*arr);printf("Cube of %d: %f\n", x, arr[1]);printf("Square root of %d: %f\n", x, arr[2]);return0;}float*arrfunction(int x){staticfloat arr[3];
arr[0]=pow(x,2);
arr[1]=pow(x,3);
arr[2]=pow(x,0.5);return arr;}
Output
When you run this code, it will produce the following output −
Square of 100: 10000.000000
Cube of 100: 1000000.000000
Square root of 100: 10.000000
Example 2
Now, consider the following function which will generate 10 random numbers and return them using an array and call this function as follows −
#include <stdio.h>/* function to generate and return random numbers */int*getRandom(){staticint r[10];int i;/* set the seed */srand((unsigned)time(NULL));for(i =0; i <10;++i){
r[i]=rand();printf("r[%d] = %d\n", i, r[i]);}return r;}/* main function to call above defined function */intmain(){/* a pointer to an int */int*p;int i;
p =getRandom();for(i =0; i <10; i++){printf("*(p + %d): %d\n", i,*(p + i));}return0;}
Output
When the above code is compiled together and executed, it produces the following output −
r[0] = 2110147662
r[1] = 1427553496
r[2] = 1243625529
r[3] = 857484361
r[4] = 513293736
r[5] = 964923407
r[6] = 36104419
r[7] = 1248464892
r[8] = 1838450240
r[9] = 2096489563
*(p + 0): 2110147662
*(p + 1): 1427553496
*(p + 2): 1243625529
*(p + 3): 857484361
*(p + 4): 513293736
*(p + 5): 964923407
*(p + 6): 36104419
*(p + 7): 1248464892
*(p + 8): 1838450240
*(p + 9): 2096489563
Using the malloc() Function
The malloc() function is available as a library function in the stdlib.h header file. It dynamically allocates a block of memory during the runtime of a program. Normal declaration of variables causes the memory to be allocated at the compile time.
void*malloc(size_t size);
The malloc() function returns a generic void pointer. To assign values of a certain data type in the allocated memory, it must be typecast to the required type. For example, to store “int” data, it must be typecast to “int *” as follows −
int*x =(int*)malloc(sizeof(int);
Example
Let us allocate a block of memory sufficient to store three float values corresponding to the square, cube and square root of a number, and return the float pointer to main(), inside which the computed values are displayed.
#include <stdio.h>#include <math.h>#include <stdlib.h>float*arrfunction(int);intmain(){int x =16, i;float*arr =arrfunction(x);printf("Square of %d: %f\n", x, arr[0]);printf("Cube of %d: %f\n", x, arr[1]);printf("Square root of %d: %f\n", x, arr[2]);return0;}float*arrfunction(int x){float*arr =(float*)malloc(3*sizeof(float));
arr[0]=pow(x,2);
arr[1]=pow(x,3);
arr[2]=pow(x,0.5);return arr;}
Output
When you run this code, it will produce the following output −
Square of 16: 256.000000
Cube of 16: 4096.000000
Square root of 16: 4.000000
Using Array Element in Struct
In this method, we will declare a struct, inside which there is a float array as its element. The called function (myfunction) declares a struct variable, populates the array element with square, cube and the square root of the argument received by it, and returns it to the main() function.
Example
Take a look at the example −
#include <stdio.h>#include <math.h>structmystruct{float arr[3];};structmystructmyfunction(int x);intmain(){int x =9;structmystruct s =myfunction(x);printf("Square of %d: %f\n", x, s.arr[0]);printf("Cube of %d: %f\n", x, s.arr[1]);printf("Square root of %d: %f\n", x, s.arr[2]);return0;}structmystructmyfunction(int x){structmystruct s1;
s1.arr[0]=pow(x,2);
s1.arr[1]=pow(x,3);
s1.arr[2]=pow(x,0.5);return s1;}
Output
When you run this code, it will produce the following output −
Square of 9: 81.000000
Cube of 9: 729.000000
Square root of 9: 3.000000
Return a String from a Function
Using the same approaches, you can pass and return a string to a function. A string in C is an array of char type. In the following example, we pass the string with a pointer, manipulate it inside the function, and return it back to the main() function.
Inside the called function, there is a local string. The string passed is concatenated with the local string before returning.
Example
Take a look at the example −
#include <stdio.h>#include <string.h>#include <stdlib.h>char*hellomsg(char*);intmain(){char* name ="TutorialsPoint";char*arr =hellomsg(name);printf("%s\n", arr);return0;}char*hellomsg(char*x){char*arr =(char*)malloc(50*sizeof(char));strcpy(arr,"Hello ");strcat(arr, x);return arr;}
Output
When you run this code, it will produce the following output −
Hello TutorialsPoint
Leave a Reply