Loading content...
Loading content...
A function is a named block of code that performs a specific task. Functions help us divide a large program into smaller and manageable parts.
A function can:
Accept input values (parameters).
Perform a specific task.
Return a value if needed.
Functions make programs easier to read, maintain, and reuse.
Without functions, we may have to write the same code multiple times. Functions allow us to write the logic once and use it whenever required.
Reduce code duplication.
Improve code readability.
Make programs easier to maintain.
Help in debugging and testing.
Promote code reusability.
Divide large programs into smaller modules.
#include <stdio.h>
// Function definition
int square(int x)
{
return x * x;
}
int main()
{
int result = square(5);
printf("Square of 5 is: %d", result);
return 0;
}Square of 5 is: 25In the above example, the square() function receives a number, calculates its square, and returns the result.
When a function is called:
Control moves to the function.
The function executes its statements.
If a return statement exists, a value is returned.
Control returns to the calling function.
return_type function_name(parameter_list)
{
// function body
}The return type specifies the type of value returned by the function.
Examples:
int
float
char
double
voidUse void when the function does not return anything.
The function name identifies the function.
Example:
calculateSum()
printMessage()
square()A function name follows the same naming rules as variables.
Parameters are input values passed to a function.
Example:
int add(int a, int b)Here, a and b are parameters.
The function body contains the statements that execute when the function is called.
Example:
{
return a + b;
}A function declaration informs the compiler about the function before it is used.
It specifies:
Function name
Return type
Parameters
A declaration does not contain the function body.
int add(int a, int b);This statement only tells the compiler that a function named add exists.
A function definition contains the actual implementation of the function.
int add(int a, int b)
{
return a + b;
}This code tells the compiler what the function actually does.
If a function is defined after the main() function, the compiler must know about it before it is called.
For this reason, we use function declarations.
#include <stdio.h>
int add(int, int);
int main()
{
printf("%d", add(5, 3));
return 0;
}
int add(int a, int b)
{
return a + b;
}Without the declaration, the compiler may generate an error.
A function is executed by calling its name followed by parentheses.
#include <stdio.h>
int add(int a, int b)
{
return a + b;
}
int main()
{
int result = add(5, 3);
printf("The sum is: %d", result);
return 0;
}The sum is: 8Functions can be called multiple times from different parts of a program.
Functions in C are divided into two main categories.
Library functions are predefined functions provided by C.
Examples:
printf()
scanf()
sqrt()
strlen()These functions are available through header files.
Example:
#include <stdio.h>
#include <math.h>User-defined functions are created by programmers according to program requirements.
Example:
int multiply(int a, int b)
{
return a * b;
}User-defined functions are commonly classified into four types.
This function neither accepts input nor returns output.
#include <stdio.h>
void greet()
{
printf("Welcome to C Programming");
}
int main()
{
greet();
return 0;
}This function accepts input values but does not return anything.
#include <stdio.h>
void displaySum(int a, int b)
{
printf("Sum = %d", a + b);
}
int main()
{
displaySum(5, 3);
return 0;
}This function does not accept input but returns a value.
#include <stdio.h>
int getNumber()
{
return 100;
}
int main()
{
int num = getNumber();
printf("%d", num);
return 0;
}This is the most commonly used type of function.
It accepts input and returns a result.
#include <stdio.h>
int multiply(int a, int b)
{
return a * b;
}
int main()
{
int result = multiply(5, 4);
printf("%d", result);
return 0;
}20Whenever a function is called, memory is allocated for that function in a special area called the Function Call Stack.
The stack stores:
Parameters
Local variables
Return address
When the function finishes execution, its memory is automatically released.
This process helps manage memory efficiently.
Reduce code duplication.
Improve program readability.
Simplify debugging.
Make programs modular.
Enable code reuse.
Improve maintainability.
Every function call requires additional memory.
Frequent function calls may slightly reduce performance.
Too many small functions can make code harder to follow.
Sharing data between functions may require parameters or global variables.
Give meaningful names to functions.
Keep functions small and focused on a single task.
Avoid writing very long functions.
Use parameters instead of global variables whenever possible.
Add comments when necessary.
A function is a reusable block of code that performs a specific task.
Functions help reduce code duplication and improve readability.
Every function has a return type, name, parameters, and body.
Function declarations tell the compiler about a function.
Function definitions contain the actual implementation.
Functions can be library functions or user-defined functions.
User-defined functions can be categorized based on arguments and return values.
Functions make programs modular, organized, and easier to maintain.