Simple Tutorials for Smart Learning
Functions in C are blocks of code designed to perform a specific task, making programs modular, reusable, and easier to debug.
A function is a self-contained block of code that performs a task. It’s defined once and can be called multiple times.
return_type function_name(parameter_list) { // code block }
#include <stdio.h> void greet() { printf("Hello, World!\n"); } int main() { greet(); // Function call return 0; }
Hello, World!
printf
scanf
Parameters are placeholders for values you want to pass into the function.
void displaySum(int a, int b) { int sum = a + b; printf("Sum = %d\n", sum); }
#include <stdio.h> void displaySum(int x, int y) { printf("Sum = %d\n", x + y); } int main() { displaySum(5, 10); return 0; }
Sum = 15
The return type specifies the type of value the function gives back to the caller.
int add(int a, int b) { return a + b; }
#include <stdio.h> int add(int x, int y) { return x + y; } int main() { int result = add(3, 7); printf("Result = %d\n", result); return 0; }
Result = 10
void
int
float
char