In C programming, a function is a self-contained block of code that performs a specific task. Functions are the building blocks of C, allowing for modular programming. Instead of writing a massive, single main() function, you break the program into smaller, manageable, and reusable pieces.
The primary philosophy behind functions is DRY (Don't Repeat Yourself).
Reusability: Write code once and use it many times throughout the program.
Abstraction: You don't need to know how a function works internally to use it; you only need to know its inputs and outputs (e.g., printf).
Maintainability: If a bug is found in a specific calculation, you only need to fix it in one function rather than in twenty different places.
Organization: It makes the code readable. A main() function that calls readInput(), calculateResult(), and printReport() is much easier to understand than 500 lines of raw code.
To use a user-defined function, you must understand three distinct parts:
This tells the compiler about the function's name, return type, and parameters before it is used. It acts like a "warning" to the compiler so it knows what to expect.
Syntax: return_type function_name(parameter_list);
Location: Usually placed above main().
This is the actual "body" of the function where the logic is written.
Syntax:
return_type function_name(parameter_list) { // Body of the function return value; // (if return_type is not void) }
This is how you execute the function from main() or another function.
Syntax: function_name(arguments);
Return Type: The data type of the value the function sends back to the caller (e.g., int, float, char). If the function returns nothing, use void.
Function Name: A unique identifier (follows variable naming rules).
Parameters (Formal): Variables declared in the function definition to receive values.
Arguments (Actual): The actual values or variables passed to the function during a call.
Here is a program that defines a function to calculate the area of a rectangle.
#include <stdio.h> // 1. Function Declaration (Prototype) float calculateArea(float length, float width); int main() { float l, w, area; printf("Enter length and width: "); scanf("%f %f", &l, &w); // 2. Function Call // l and w are "Arguments" area = calculateArea(l, w); printf("The area of the rectangle is: %.2f\n", area); return 0; } // 3. Function Definition // length and width are "Parameters" float calculateArea(float length, float width) { float result; result = length * width; return result; // Sends the value back to 'main' }
| Category | Description | Example |
| No Argument, No Return | Purely for display or fixed tasks. | void greet() { printf("Hello"); } |
| With Argument, No Return | Takes data and processes it (e.g., printing it). | void printSquare(int n) { printf("%d", n*n); } |
| No Argument, With Return | Generates or reads data internally. | int getAge() { int a; scanf("%d", &a); return a; } |
| With Argument, With Return | The most common type (Calculation). | int add(int a, int b) { return a + b; } |
When a function is called, the CPU pauses the current function (main) and creates a Stack Frame for the new function. This frame contains the function's local variables and parameters. Once the function reaches a return statement or the closing brace }, its stack frame is destroyed, and the CPU resumes where it left off in the calling function.
Scope: Variables defined inside a function are local to that function. main cannot see variables inside calculateArea.
The void keyword: Explain clearly that void means "nothing." If a function doesn't return a value, it must be void.
Order Matters: If you define a function below main without a declaration above main, the compiler will throw an error or a warning because it doesn't recognize the function name when it hits the call.
Copyright ©2025. All Rights Reserved Emblab THE RAVE INNOVATION