In C programming, functions are blocks of code that perform a specific task. They are a fundamental building block for structuring programs. Functions in C can be classified into two main categories:
- Built-in Functions: These are functions that are predefined in C libraries. You don’t need to write them; instead, you can directly use them by including the corresponding header files.
- User-defined Functions: These are functions that you, as a programmer, define to perform specific tasks based on your program’s needs.
1. Built-in Functions
These functions are provided by the C standard library, and they simplify common tasks like input/output, mathematical operations, string manipulations, memory management, etc. Here are some examples:
- Input/Output Functions:
printf()
: Prints formatted output to the screen.scanf()
: Takes input from the user.
- Mathematical Functions:
sqrt()
: Returns the square root of a number.pow()
: Returns the value of a number raised to a power.abs()
: Returns the absolute value of a number.
- String Functions:
strlen()
: Returns the length of a string.strcpy()
: Copies one string to another.strcmp()
: Compares two strings.
- Memory Management Functions:
malloc()
: Allocates memory dynamically.free()
: Frees the dynamically allocated memory.calloc()
: Allocates memory for an array of objects and initializes them to zero.
- Other Utility Functions:
exit()
: Terminates the program.time()
: Returns the current system time
To use these functions, you generally need to include specific header files like:
#include <stdio.h> // For input/output functions
#include <stdlib.h> // For memory management functions and exit
#include <math.h> // For mathematical functions
#include <string.h> // For string functions
#include <time.h> // For time functions
2. User-defined Functions
In C, you can create your own functions to perform custom tasks. A user-defined function typically consists of:
- Function Declaration (Prototype): Specifies the function’s name, return type, and parameters.
- Function Definition: Contains the actual code to perform the task.
- Function Call: Used to invoke the function.
Syntax of a Function in C
- Function Declaration (Prototype):
return_type function_name(parameters);
Example:
int add(int, int); // Function prototype
Function Definition: The function’s implementation.
return_type function_name(parameters)
{
// Function body
}
Example:
int add(int a, int b) // Function definition
{
return a + b;
}
Function Call: Calling the function from the main()
function or any other function.
int result = add(5, 3); // Function call
Example of a User-Defined Function
#include <stdio.h>
// Function declaration (prototype)
int add(int, int);
int main() {
int a = 5, b = 3;
int sum = add(a, b); // Function call
printf(“Sum: %d\n”, sum);
return 0;
}
// Function definition
int add(int a, int b) {
return a + b;
}
Key Points About Functions in C:
- Return Type: Specifies what type of value the function will return (e.g.,
int
,float
,char
,void
). - Function Name: The name you give to the function, which you use to call it.
- Parameters (Arguments): These are the values that you pass to the function. Functions can have zero or more parameters.
- Return Statement: If the function has a return type other than
void
, you must use areturn
statement to return a value.
Types of User-Defined Functions
Functions with No Arguments and No Return Value: These functions don’t accept parameters and don’t return any value.
void greet() {
printf(“Hello, World!\n”);
}
Functions with Arguments but No Return Value: These functions take parameters but don’t return anything.
void printSum(int a, int b) {
printf(“Sum: %d\n”, a + b);
}
Functions with Arguments and a Return Value: These functions take parameters and return a value.
int multiply(int a, int b) {
return a * b;
}
Functions with No Arguments but a Return Value: These functions don’t take parameters but return a value.
int getRandomNumber() {
return rand() % 100;
}
Conclusion
In C, functions help to modularize the program, make the code more readable, reusable, and maintainable. Built-in functions are predefined in the C standard library, while user-defined functions are created by the programmer to perform specific tasks.