---
title: "Can a function return an array"
subject: "Computer Programming"
chapter: 7
key: "semester_1/computer_programming:function-return-array"
source: https://www.bctengineer.com/q/semester_1/computer_programming/function-return-array
site: BCT Engineering
---

# Can a function return an array

Computer Programming · Chapter 7: Arrays and Strings

- Asked in: 2074 Chaitra, 4 marks
- Video: [How to return an array from a function](https://www.youtube.com/watch?v=WJO-obvMpFY) — CodeVault
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_1/computer_programming/function-return-array
- Whole chapter: https://www.bctengineer.com/md/semester_1/computer_programming/chapter-7

## Question

Can a function return an array to the calling function? Explain with example.

## Short answer

A function cannot return a whole array, but can return a pointer to the first element. That pointer must address memory that outlives the function, such as a static array or dynamically allocated memory. Returning a local array’s address is invalid.

## Answer

No. A C function cannot return a whole array by value, because an array name is not an assignable value and the return type cannot be an array type. However, it can return a **pointer** to the first element of an array, and the caller then uses that pointer like an array.

Important rule: the returned pointer must point to memory that still exists after the function ends. A normal local array is destroyed when the function returns, so it must not be returned. Safe choices:
1. A `static` array inside the function.
2. An array allocated with `malloc()`.
3. An array passed in by the caller (the function fills it).
4. An array inside a structure, since a structure can be returned by value.

**Example** (static array)

```c
#include <stdio.h>

int *makeSquares(void)
{
    static int sq[5];                 /* static: lives after the function ends */
    for (int i = 0; i < 5; i++)
        sq[i] = (i + 1) * (i + 1);
    return sq;                        /* returns address of first element */
}

int main(void)
{
    int *p = makeSquares();
    for (int i = 0; i < 5; i++)
        printf("%d ", p[i]);
    printf("\n");
    return 0;
}
```

Output:

```text
1 4 9 16 25
```

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. Can a C function return a whole array by value?
   A. Yes, using int[] as return type
   B. Yes, but only 1D arrays
   C. No, and it cannot return a pointer either
   D. No, but it can return a pointer to the first element
   Answer: D. Array types cannot be returned; a pointer may. (Key point: Can it return an array)

2. Why must a normal local array not be returned?
   A. It is too large
   B. It is destroyed when the function returns
   C. Local arrays are read-only
   D. Its pointer cannot be typed
   Answer: B. The returned pointer would point to memory that no longer exists. (Key point: Local arrays)

3. Which is a safe way to give the caller an array?
   A. Return the address of an auto array
   B. Return the array name of a local variable
   C. Declare the array after the return statement
   D. Use a static array or malloc memory
   Answer: D. Static and heap memory outlive the function call. (Key point: Safe options)

## Related in Computer Programming

- [Function returning largest of array](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Alargest-from-array-function) — Computer Programming, chapter 7
- [Sort array using a function](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Aarray-sort-function) — Computer Programming, chapter 7
- [Reverse array and display via pointer](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Apointer-reverse-array) — Computer Programming, chapter 9
- [Pass array; largest and smallest](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Apass-array-largest-smallest) — Computer Programming, chapter 7
- [Pass array; transpose 3×3 matrix](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Apass-array-transpose-3x3) — Computer Programming, chapter 7
- [Pass student array; display passed students](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Apass-whole-array-student-info) — Computer Programming, chapter 7

## Related in other subjects

- [Function template: sum and average of array](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Afunction-template-sum-average) — Object Oriented Programming, chapter 9
- [LandMeasure class and sum function](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Alandmeasure-class-sum) — Object Oriented Programming, chapter 4
- [Function template power(base, exp)](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Apower-function-template) — Object Oriented Programming, chapter 9
