C Function Declaration and Definition

C Tutorial

C Function Declaration and Definition

In C language, functions help in breaking large programs into smaller, reusable blocks.
To use functions correctly, you must understand function declaration and function definition.


What is a Function in C?

A function is a block of code that performs a specific task and can be called multiple times.


 Function Declaration (Function Prototype)

 What is Function Declaration?

A function declaration tells the compiler:

  • Function name

  • Return type

  • Parameters

 It does not contain function body.

Syntax

Example

  •  Informs compiler that add() exists somewhere

 Function Definition

 What is Function Definition?

A function definition contains:

  • Function header

  • Function body (actual code)

Syntax

Example


 Complete Program Example


 

  •  Declaration before main()
  •  Definition after main()

 Why Function Declaration Is Needed?

  •  Enables calling function before its definition
  •  Required for multi-file programs
  • Helps compiler in type checking

 Without declaration → compiler error


 Declaration vs Definition (Important)

FeatureDeclarationDefinition
Tells about functionYesYes
Contains bodyNoYes
Memory allocatedNoYes
Can be repeatedYesNo

Function Call Flow

main()function callfunction definition → return value → main()

 Multiple Declarations Allowed

  •  Multiple definitions are NOT allowed

 Function Declaration in Header Files

math_utils.h

math_utils.c

main.c

  •  Used in large projects

 Common Mistakes

  •  Missing function declaration
  •  Mismatch between declaration & definition
  •  Wrong return type
  •  Wrong parameter order

 Declaration & definition must match exactly


 Interview Questions (Must Prepare)

  1. What is function declaration?

  2. What is function definition?

  3. Why function prototype is required?

  4. Difference between declaration & definition

  5. Can we define function before main()?


 Real-Life Use Cases

  • Modular programming

  • Library development

  • Embedded systems

  • Operating systems

  • Large-scale applications


 Summary

  •  Function declaration informs compiler
  •  Function definition provides implementation
  •  Both are essential in C
  •  Very important for placements & interviews

You may also like...