In C programming, the communication between the calling function (usually main()) and the called function is handled through Parameters and Return Values. If a function is a machine, parameters are the raw materials you feed into it, and the return value is the finished product it hands back to you.
Parameters (also known as arguments) are variables that allow a function to receive data from the caller. They are defined within the parentheses in the function header.
Formal Parameters: These are the variables declared in the function definition. They act as placeholders for the data the function will receive.
Actual Parameters (Arguments): These are the real values or variables passed to the function when it is called.
Theory of Parameter Passing:
By default, C uses a mechanism called Call by Value. When you pass a variable to a function, the compiler creates a copy of that variable's value in a new memory location.
Because the function works on a copy, any changes made to the parameter inside the function do not affect the original variable in the calling function.
A return value is the data that a function sends back to the calling function after its execution is complete.
The return Keyword: This statement serves two purposes:
It immediately terminates the function's execution.
It specifies the value to be sent back to the caller.
Return Type: Every function must have a return type specified before its name (e.g., int, float, double, char). If no data is returned, the type is void.
Theory of Return Logic:
A function can only return one single value directly. If you need to return multiple values, you must use more advanced concepts like pointers, structures, or arrays. When a function returns a value, the function call itself is effectively replaced by that value in the original expression.
The following program demonstrates a function that takes two parameters (input) and returns the larger value (output).
#include <stdio.h> // Function Declaration // Parameters: int a, int b (Formal Parameters) // Return Type: int int findMax(int a, int b); int main() { int x = 25, y = 42; int result; // Function Call // x and y are Actual Parameters (Arguments) result = findMax(x, y); printf("The maximum of %d and %d is: %d\n", x, y, result); return 0; } // Function Definition int findMax(int a, int b) { if (a > b) { return a; // Returns value of a and exits } else { return b; // Returns value of b and exits } }
To design efficient programs, you must choose the right "shape" for your function:
| Category | Theory | Use Case |
| No Input, No Output | void func(void) - Independent tasks. | Printing a menu or a header. |
| Input, No Output | void func(int x) - The function "consumes" data. | Displaying data or saving to a file. |
| No Input, Output | int func(void) - The function "produces" data. | Generating a random number or reading a sensor. |
| Input, Output | int func(int x) - Data transformation. | Math formulas, conversions, or logic checks. |
Type Consistency: The data type of the actual parameter must match the formal parameter. If you pass a float to an int parameter, the decimal part will be truncated (lost).
The void Return: If a function is declared as void, using return 10; inside it will cause a compilation error. You can, however, use return; (without a value) to exit a void function early.
Scope: Parameters are local to the function. Once the function finishes, the memory allocated for the parameters is released.
Implicit Casting: If the return type of a function is int, but you return 3.14, the function will actually return 3.
Copyright ©2025. All Rights Reserved Emblab THE RAVE INNOVATION