
Functions in C
In C, a function is a reusable block of code that performs a specific task. It helps you organize, reuse, and modularize your program. ??
? Basic Structure of a Function
return_type (parameter_list) { // body of the function}
? Example: A Simple Function
#include // Function declarationvoid greet(); int main() { greet(); // Function call return 0;}// Function definitionvoid greet() { printf("Hello from a function!\n");}
? Types of Functions
No arguments, no return value
void sayHello();
Arguments, no return value
void greetUser(char name[]);
No arguments, return value
();
Arguments and return value
int add(int a, int add(int x, int y) { return x + y;}int main() { int result = add(3, 4); printf("Sum = %d\n", result); return 0;}
? Function Components
Part Description Declaration Tells the compiler the function exists Definition Actual code block of the function Call Where the function is used ? Why Use Functions?
Avoid code repetition
Make programs easier to read & maintain
Break down complex problems into smaller tasks