1 of 9

Functions in C Programming

MD. SAKIB HOSSAIN,

DEPT. OF CSE, VARENDRA UNIVERSITY

2 of 9

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

3 of 9

What is a Function?

  • A function is a block of code designed to perform a specific task.
  • Helps divide programs into smaller, manageable parts.
  • Benefits: easier debugging, reusable code, readable programs.

4 of 9

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

5 of 9

Types of Functions

Built-in Functions

provided by C (printf(), scanf(), sqrt())

User-defined Functions

created by programmers for specific tasks

6 of 9

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);

7 of 9

Example of a Simple Function

#include <stdio.h>

void greet() {

printf("Hello, World!\\n");

}

int main() {

greet(); // function call

return 0;

}

Output: Hello, World!

8 of 9

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;

}

  • Parameters: a and b
  • Return value: sum of a and b

9 of 9

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