Functions in C Programming
MD. SAKIB HOSSAIN,
DEPT. OF CSE, VARENDRA UNIVERSITY
Agenda
01
What is a Function?
02
Anatomy of a Function
03
Types of Functions
04
Function Declaration & Definition
05
Examples (Simple & With Parameters)
06
Advantages of Functions
What is a Function?
Anatomy of a Function
Syntax: return_type function_name(parameters) { // body of function }
Return type
type of value returned (int, void, etc.)
Function name
unique identifier
Parameters
input values (optional)
Function body
statements executed by the function
Types of Functions
Built-in Functions
provided by C (printf(), scanf(), sqrt())
User-defined Functions
created by programmers for specific tasks
Function Declaration vs Definition
Declaration (Prototype):
tells compiler about function
int add(int a, int b);
Definition:
actual code of function
int add(int a, int b) { return a + b; }
Calling a function: using its name in
main or other functions
int result = add(5, 3);
Example of a Simple Function
#include <stdio.h>
void greet() {
printf("Hello, World!\\n");
}
int main() {
greet(); // function call
return 0;
}
Output: Hello, World!
Function with Parameters and Return Value
#include <stdio.h>
int sum(int a, int b) {
return a + b;
}
int main() {
int result = sum(5, 10);
printf("Sum = %d\\n", result);
return 0;
}
Advantages of Using Functions
Reusability
Write once, use multiple times
Organization
Keeps code structured and neat
Easier debugging
Smaller functions are easier to test
Readability
Programs are easier to understand