Definition:
C functions are blocks of code that perform specific tasks and can be called from other parts of a program. They provide a way to organize code into reusable modules, making the code more readable, modular, and maintainable. Functions in C can have a return type, parameters, and a body that contains the actual code to be executed.
Here is the syntax of a C function declaration:
return_type function_name(parameter1, parameter2, ...)
{
// Function body
// Code to be executed
}
Let's break down the different components of a C function:
1. Return Type: This specifies the type of data that the function will return after its execution. It can be any valid C data type, such as `int`, `float`, `void` (indicating no return value), or even a user-defined type. For example, a function that calculates the sum of two numbers and returns the result could have a return type of `int`.
2. Function Name: This is the identifier used to call the function from other parts of the program. The function name should be meaningful and follow the naming conventions of C. For example, a function that calculates the sum of two numbers could be named `calculateSum`.
3. Parameters: These are optional inputs to the function that allow you to pass values from the calling code into the function. Parameters are enclosed in parentheses after the function name, and multiple parameters are separated by commas. Each parameter consists of a type and a name, specifying the data type of the parameter and a local variable name to be used within the function. For example, a function that calculates the sum of two numbers could have parameters `int num1` and `int num2`.
4. Function Body: This is the block of code enclosed within curly braces `{}` that contains the actual implementation of the function. It consists of statements that define the behavior of the function. These statements can include variable declarations, control flow structures (e.g., if-else, loops), function calls, and any other valid C code. The function body is executed when the function is called, and it can manipulate the parameters and local variables defined within the function.
Now, let's see an example of a C function that calculates the sum of two numbers:
int calculateSum(int num1, int num2)
{
int sum = num1 + num2;
return sum;
}
In this example, the function `calculateSum` takes two integer parameters `num1` and `num2`. It calculates the sum of these two numbers and stores the result in the `sum` variable. Finally, it returns the value of `sum` as the result of the function.
Calling a Function:
To call this function from another part of the program, you would simply use the function name followed by arguments enclosed in parentheses:
int result = calculateSum(10, 20);
In this case, the function is called with the arguments `10` and `20`, and the returned value (`30`) is stored in the `result` variable.
Types of Functions:
Now, let's look at some examples of different types of functions in C:
1. Function with no parameters and no return value (void):
void sayHello()
{
printf("Hello, World!\n");
}
2. Function with parameters and a return value:
int addNumbers(int a, int b)
{
return a + b;
}
3. Function with parameters and no return value (void):
void printSum(int a, int b)
{
int sum = a + b;
printf("The sum is: %d\n", sum);
}
4. Function with a return value but no parameters:
double getPi()
{
return 3.14159;
}
Function Proto-Type:
return_type function_name(parameter1, parameter2, ...);
// Function prototype
int calculateSum(int num1, int num2);
int main()
{
int result = calculateSum(10, 20);
// Rest of the code
return 0;
}
// Function definition
int calculateSum(int num1, int num2)
{
int sum = num1 + num2;
return sum;
}
Passing Values by Reference in C:
#include <stdio.h>
// Function to double the value of the variable using a pointer
void doubleValue(int* x)
{
*x = 20; // Dereferencing the pointer to access and modify the value
}
int main()
{
int num = 10;
printf("Original value: %d\n", num);
// Pass the address of 'num' to the function
doubleValue(&num);
printf("Value after doubling: %d\n", num);
return 0;
}
Passing Arrays to Function:
#include <stdio.h>
// Function that doubles the elements of an array
void doubleArray(int arr[], int size)
{
for (int i = 0; i < size; i++)
{
arr[i] *= 2;
}
}
int main()
{
int numbers[] = { 1, 2, 3, 4, 5 };
int size = sizeof(numbers) / sizeof(numbers[0]);
printf("Original array: ");
printArray(numbers, size);
// Call the function to double the elements of the array
doubleArray(numbers, size);
printf("Modified array: ");
printArray(numbers, size);
return 0;
}
void printArray(const int arr[], int size)
{
// Function body (read-only access to the elements)
}
Recursion in C:
#include <stdio.h>
// Recursive function to calculate the sum of numbers from 1 to n
int sum(int n)
{
if (n == 1)
{
return 1; // Base case: the sum of 1 is 1
}
else
{
return n + sum(n - 1); // Recursive case: sum(n) = n + sum(n-1)
}
}
int main()
{
int num = 5;
int result = sum(num);
printf("Sum of numbers from 1 to %d is: %d\n", num, result);
return 0;
}