MCS-201 · Unit 5

Functions

Section 2 of 8

Block 2 · Unit 5Functions11 min · Section 2 of 8

5.2Function Declarations and Prototypes

Covers IGNOU MCS-201 §5.3, 5.4

The compiler reads a file from top to bottom. If it meets a call to a function it has not seen yet, it cannot check the call. A prototype introduces the function in advance. This section explains declarations, prototypes and the checks they make possible.

Declaration versus definition

Declaration (prototype)Definition
ContainsReturn type, name, parameter types, then a semicolonHeader plus the body in braces
PurposeTells the compiler the function exists and how to call itProvides the actual code
How manyMay appear several timesExactly once in the program
Exampleint square(int no);int square(int no) { return no * no; }

Parameter names are optional in a prototype: int square(int); and int square(int no); are equivalent, but names make the prototype easier to understand. A function that takes no arguments is declared with void: int cal_sum(void);.

What the compiler checks

With the prototype visible, the compiler verifies each call:

  • the number of arguments,
  • the types of the arguments (converting where C allows it, such as int to float),
  • the use of the return value.

Without any declaration, a call is an error in modern C.

Where prototypes go

The usual place is near the top of the file, after the #include lines and before main. Every function below can then call the function. The header files you include, such as <stdio.h>, are collections of prototypes for library functions.

Key takeaways

  • A declaration (prototype) introduces a function; a definition supplies its body.
  • The prototype must appear before the first call, usually at the top of the file.
  • The compiler uses it to check the number and types of arguments.
  • Parameter names are optional in prototypes.

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

Which is a valid prototype for a function that takes two floats and returns a float?

Show answer

Answer: B. float avg(float, float);

A prototype has a return type, parameter types and a final semicolon; names are optional. The first option lacks the semicolon and the third lacks a return type.

Question 2 · multiple choice

How many times may a function be defined in a program?

Show answer

Answer: C. Exactly once

There must be exactly one definition; declarations may be repeated.

Question 3 · multiple choice

With the prototype int sum(int, int, int);, what happens to the call sum(1, 2)?

Show answer

Answer: D. The compiler reports too few arguments

The prototype lets the compiler detect the mismatch: “too few arguments to function ‘sum’”.

Question 4 · multiple choice

Which line of this program is the function prototype?

C · find_proto.c
#include <stdio.h>

float half(float x);

int main(void)
{
    printf("%.1f\n", half(9));
    return 0;
}

float half(float x)
{
    return x / 2;
}
Show answer

Answer: A. Line 3

Line 3 ends with a semicolon and has no body, so it is the prototype. Line 11 starts the definition and line 7 contains the call.

Question 5 · multiple choice

What does the program in question 4 print?

Show answer

Answer: C. 4.5

The prototype tells the compiler to convert the int 9 to the float 9.0, so x / 2 is real division: 4.5.

Question 6 · short answer

Explain the difference between a function declaration and a function definition, using int max(int a, int b) as the example.

Show worked answer

The declaration is int max(int a, int b);. It ends with a semicolon, has no body, and only tells the compiler that a function named max takes two int values and returns an int, so calls can be checked. The definition is int max(int a, int b) { return (a > b) ? a : b; }. It repeats the header and adds the body, which is the code that actually runs. A function may be declared many times but must be defined exactly once.

↑ ↓ move↵ openTab switch type