Functions in C
Functions in C are reusable blocks of code that perform a specific task. They can take parameters, return values, and help in organizing code into modular and manageable pieces.
Defining a Function
A function in C is defined using the following syntax:
return_type function_name(parameters) {
// Function body
}
Example:
#include <stdio.h>
// Function Declaration
void greet(char name[]) {
printf("Hello, %s!\n", name);
}
int main() {
greet("Alice");
return 0;
}
Function Parameters and Return Types
Functions can take parameters and return values. The return
keyword is used to return a value from a function.
#include <stdio.h>
// Function to add two numbers
int add(int a, int b) {
return a + b;
}
int main() {
int result = add(5, 10);
printf("Sum: %d\n", result);
return 0;
}
Function Prototypes
A function prototype declares the function's name, return type, and parameters before the function is defined. This allows the function to be called before its definition.
#include <stdio.h>
// Function Prototype
int multiply(int a, int b);
int main() {
int result = multiply(5, 10);
printf("Product: %d\n", result);
return 0;
}
// Function Definition
int multiply(int a, int b) {
return a * b;
}
Recursion
A function can call itself, which is known as recursion. Recursion is useful for solving problems that can be broken down into smaller, similar subproblems.
#include <stdio.h>
// Recursive function to calculate factorial
int factorial(int n) {
if (n == 0) {
return 1;
}
return n * factorial(n - 1);
}
int main() {
int result = factorial(5);
printf("Factorial: %d\n", result);
return 0;
}
Function Pointers
Function pointers allow you to store the address of a function and call it dynamically.
#include <stdio.h>
int add(int a, int b) {
return a + b;
}
int subtract(int a, int b) {
return a - b;
}
int main() {
int (*operation)(int, int); // Function pointer declaration
operation = add; // Point to the add function
printf("Sum: %d\n", operation(5, 3));
operation = subtract; // Point to the subtract function
printf("Difference: %d\n", operation(5, 3));
return 0;
}
Passing Arrays to Functions
Arrays can be passed to functions by passing a pointer to the first element of the array.
#include <stdio.h>
// Function to print an array
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
int main() {
int arr[] = {1, 2, 3, 4, 5};
printArray(arr, 5);
return 0;
}
Variable-Length Argument Lists
C supports functions with variable-length argument lists using the stdarg.h
library.
#include <stdio.h>
#include <stdarg.h>
// Function to calculate the average of a variable number of arguments
double average(int count, ...) {
va_list args;
va_start(args, count);
double sum = 0;
for (int i = 0; i < count; i++) {
sum += va_arg(args, double);
}
va_end(args);
return sum / count;
}
int main() {
printf("Average: %.2f\n", average(3, 1.0, 2.0, 3.0));
return 0;
}
Inline Functions
Inline functions are small functions that are expanded in place when called, reducing the overhead of function calls.
#include <stdio.h>
// Inline function
static inline int square(int x) {
return x * x;
}
int main() {
printf("Square: %d\n", square(5));
return 0;
}
Static Functions
Static functions are only visible within the file they are defined in, providing encapsulation.
#include <stdio.h>
// Static function
static void greet() {
printf("Hello from a static function!\n");
}
int main() {
greet();
return 0;
}
Next: Arrays