---
title: "Function returning largest of array"
subject: "Computer Programming"
chapter: 7
key: "semester_1/computer_programming:largest-from-array-function"
source: https://www.bctengineer.com/q/semester_1/computer_programming/largest-from-array-function
site: BCT Engineering
---

# Function returning largest of array

Computer Programming · Chapter 7: Arrays and Strings

- Asked in: 2070 Asar, 5 marks
- Video: [Find the Maximum Number in an Array | C Programming Example](https://www.youtube.com/watch?v=VnsmgHEZiLA) — Portfolio Courses
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_1/computer_programming/largest-from-array-function
- Whole chapter: https://www.bctengineer.com/md/semester_1/computer_programming/chapter-7

## Question

Write a program using a function that returns the largest number from an array of numbers that is passed to the function.

## Short answer

Define a function largest(int a[], int n) that sets max = a[0], then loops from index 1 to n-1 updating max when a[i] > max, and returns max. In main, input the array size and elements, call largest, and print the result.

## Answer

The function `largest(int a[], int n)` receives the array (its address) and the number of elements. It assumes the first element is the largest, compares it with each other element and returns the maximum.

```c
#include <stdio.h>

int largest(int a[], int n)
{
    int max = a[0];
    for (int i = 1; i < n; i++)
        if (a[i] > max)
            max = a[i];
    return max;
}

int main(void)
{
    int a[50], n;
    printf("How many numbers? ");
    scanf("%d", &n);
    printf("Enter %d numbers: ", n);
    for (int i = 0; i < n; i++)
        scanf("%d", &a[i]);
    printf("Largest = %d\n", largest(a, n));
    return 0;
}
```

**Sample run**

Input: `5 12 45 7 89 23`

```text
How many numbers? Enter 5 numbers: Largest = 89
```

## Check yourself

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

1. What does the function receive when an array is passed?
   A. A full copy of the array
   B. The address of the first element
   C. Only the first element's value
   D. The sum of the elements
   Answer: B. Arrays are passed by address, so the size is passed separately. (Key point: What is passed)

2. How does the function begin finding the maximum?
   A. Assume max = 0
   B. Sort the array first
   C. Assume a[n] is the largest
   D. Assume a[0] is the largest and compare with the others
   Answer: D. Starting from a[0] works even if all values are negative. (Key point: Initial assumption)

3. When is max updated in the loop?
   A. When a[i] < max
   B. When a[i] > max
   C. When a[i] == max
   D. In every iteration
   Answer: B. Only a larger value replaces the current maximum. (Key point: Update rule)

## Related in Computer Programming

- [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
- [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 2D array; largest and smallest](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Apass-2d-array-largest-smallest) — Computer Programming, chapter 7
- [Largest of n numbers in FORTRAN](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afortran-largest-n-numbers) — Computer Programming, chapter 11
- [Insert number at position in array](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afortran-insert-number-in-array) — Computer Programming, chapter 11
- [Function definition; sum of two numbers](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afunction-definition-sum-two) — Computer Programming, chapter 6

## 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
- [8086 ALP: largest and smallest, display](https://www.bctengineer.com/question.md?key=semester_4%2Fmicroprocessor%3Aalp-8086-largest-smallest-display) — Microprocessor, chapter 3
- [Prime number program: procedural and OOP](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aprime-procedural-oop) — Object Oriented Programming, chapter 1
- [C program: sum of two 8-bit BCD numbers](https://www.bctengineer.com/question.md?key=semester_6%2Fembedded_system%3A8051-bcd-sum-c) — Embedded System, chapter 9
