0
0
Cprogramming~5 mins

Function prototypes

Choose your learning style9 modes available
Introduction

Function prototypes tell the computer about a function before it is used. This helps avoid mistakes and makes the program easier to understand.

When you want to use a function before writing its full code.
When you want to organize your program by separating function declarations and definitions.
When you want the compiler to check if you use the function correctly.
When you write big programs with many functions in different files.
Syntax
C
return_type function_name(parameter_type1, parameter_type2, ...);

The prototype ends with a semicolon.

It shows the function name, return type, and parameter types but not the function body.

Examples
This prototype says there is a function named add that takes two integers and returns an integer.
C
int add(int, int);
This prototype says there is a function named printMessage that takes no parameters and returns nothing.
C
void printMessage(void);
This prototype says there is a function named average that takes three floats and returns a float.
C
float average(float, float, float);
Sample Program

This program declares the function multiply before main using a prototype. Then it defines the function after main. The program multiplies 4 and 5 and prints the result.

C
#include <stdio.h>

// Function prototype
int multiply(int a, int b);

int main() {
    int result = multiply(4, 5);
    printf("4 times 5 is %d\n", result);
    return 0;
}

// Function definition
int multiply(int a, int b) {
    return a * b;
}
OutputSuccess
Important Notes

Function prototypes help the compiler check if you call functions with the right number and type of arguments.

If you forget a prototype, the compiler may assume the function returns an int, which can cause errors.

Using prototypes makes your code easier to read and maintain.

Summary

Function prototypes tell the compiler about functions before they are used.

They include the function name, return type, and parameter types but no body.

Prototypes help avoid errors and improve code organization.