Function in C
A function in C is a block of code that performs a specific task. It is defined with a name, and it can take parameters (inputs) and return a value. Functions help in organizing code, making it reusable, and easier to manage.
Example:
#include <stdio.h>
// Function declaration
int add(int a, int b);
int main() {
int result = add(5, 3);
printf("Sum: %d\n", result);
return 0;
}
// Function definition
int add(int a, int b) {
return a + b;
}
In this example, the add
function takes two integers as parameters, adds them, and returns the result.
Recursive Function in C
A recursive function is a function that calls itself to solve a problem. Recursion is often used to solve problems that can be broken down into smaller, similar sub-problems. The key to a recursive function is having a base case that stops the recursion.
Example:
#include <stdio.h>
// Function declaration
int factorial(int n);
int main() {
int num = 5;
int result = factorial(num);
printf("Factorial of %d is %d\n", num, result);
return 0;
}
// Function definition
int factorial(int n) {
if (n == 0) {
return 1; // Base case
} else {
return n * factorial(n - 1); // Recursive call
}
}
In this example, the factorial
function computes the factorial of a number using recursion. The base case is when n
is 0, returning 1. Otherwise, it calls itself with n-1
until it reaches the base case.
Function passing Pointer argument in C
In C programming, passing a pointer as an argument to a function allows the function to modify the variable to which the pointer points. This is often used for passing large data structures (like arrays) efficiently, or for returning multiple values from a function. Here’s a simple example to illustrate this concept:
Example: Swapping Two Numbers
Let’s create a function that swaps two integers using pointers.
#include <stdio.h>
// Function to swap two integers
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
int main() {
int num1 = 5, num2 = 10;
printf("Before swapping: num1 = %d, num2 = %d\n", num1, num2);
// Call the swap function, passing the addresses of num1 and num2
swap(&num1, &num2);
printf("After swapping: num1 = %d, num2 = %d\n", num1, num2);
return 0;
}
Explanation
- Function Declaration and Definition:
void swap(int *a, int *b)
: This function takes two pointers to integers as arguments. The*
symbol indicates thata
andb
are pointers.- Inside the function,
*a
and*b
are used to access and modify the values of the integers to whicha
andb
point.
- Calling the Function:
- In
main()
, we have two integer variables,num1
andnum2
. - We pass the addresses of
num1
andnum2
to theswap
function using the address-of operator (&
).
- In
- Swapping the Values:
- Inside the
swap
function, the values of the integers pointed to bya
andb
are swapped using a temporary variabletemp
.
- Inside the
- Output:
- The
printf
statements before and after callingswap
show that the values ofnum1
andnum2
have been swapped.
- The
Benefits of Using Pointers
- Efficiency: Pointers allow functions to modify large data structures without copying them.
- Multiple Return Values: Functions can modify multiple variables and thus return multiple values.
- Dynamic Memory Management: Pointers are essential for dynamic memory allocation.
Summary
- Function: A reusable block of code that performs a specific task.
- Function Overloading: Not directly supported in C; simulated with different function names.
- Recursive Function: A function that calls itself to solve a problem, with a base case to terminate the recursion.