3) Functions in C
Functions in C are blocks of code that can be defined and called to perform specific tasks. They promote modularity, reusability, and easier maintenance of code. A function encapsulates a series of statements and can accept input (parameters) and return output.
Table of Contents
Function Declaration and Definition
A function is declared and defined with the following structure:
return_type function_name(parameters) {
// Function body (code)
// ...
return return_value; // Optional return statement
}
return_type
: The data type of the value the function returns (usevoid
for functions that don’t return a value).function_name
: The name of the function.parameters
: Comma-separated list of input parameters the function accepts.return_value
: The value that the function returns (not needed forvoid
functions).
Example of a Simple Function
#include <stdio.h>
// Function declaration
int add(int a, int b);
int main() {
int result = add(5, 3); // Calling the function
printf("Result: %d\n", result);
return 0;
}
// Function definition
int add(int a, int b) {
return a + b;
}
Explanation
- In this example, the function
add
calculates the sum of two integers. - The function is declared at the beginning of the program with its return type and parameter list.
- The function is then defined after the
main
function. - When called in the
main
function, it returns the sum of the provided arguments.
Function Parameters
Functions can accept parameters that act as placeholders for values passed to the function when it’s called.
#include <stdio.h>
void greet(char name[]) {
printf("Hello, %s!\n", name);
}
int main() {
greet("Alice");
greet("Bob");
return 0;
}
Explanation:
- The function
greet
accepts a single parameter,name
, which is an array of characters. - When called in the
main
function, it prints a personalized greeting based on the provided name.
Function with No Return Value (void
):
A function with void
as the return type doesn’t return a value. It’s commonly used for tasks that perform actions without producing a result.
#include <stdio.h>
void printMessage() {
printf("Hello from the function!\n");
}
int main() {
printMessage(); // Calling the function
return 0;
}
Explanation:
- The
printMessage
function prints a message when called in themain
function. - Since it has a return type of
void
, it doesn’t need areturn
statement.
Function Parameters:
Variables defined inside a function are local to that function and have scope only within it.
#include <stdio.h>
void demonstrateScope() {
int localVar = 10;
printf("Local variable: %d\n", localVar);
}
int main() {
// localVar is not accessible here
demonstrateScope();
return 0;
}
Explanation:
- The variable
localVar
is defined within thedemonstrateScope
function and is only accessible within that function. - It’s not accessible in the
main
function or any other function.
Function Prototypes:
A function prototype declares the function’s existence before it’s defined, allowing you to call the function before its actual definition.
#include <stdio.h>
int add(int a, int b); // Function prototype
int main() {
int result = add(5, 3);
printf("Result: %d\n", result);
return 0;
}
int add(int a, int b) {
return a + b;
}
Explanation:
- The function prototype for
add
is declared before themain
function. - This allows you to call the
add
function in themain
function before its definition.