A function in C is a self-contained block of code that performs a specific task.
It can take input values (called parameters), perform computations, and optionally return a result.
🧠 Syntax of a Function:
return_type function_name(parameter_list) {
// function body
// optional return statement
}🔍 Example:
int add(int a, int b) {
return a + b;
}You can call it from main() like:
int result = add(5, 3); // result = 8❓ Why Use Functions? – The Need for Functions
Purpose | Explanation |
|---|---|
✅ Modularity | Break large programs into smaller, manageable parts |
🔁 Reusability | Write once, use multiple times without rewriting code |
🧪 Easier Testing | Each function can be tested separately (unit testing) |
📚 Better Readability | Program becomes easier to understand, especially in large projects |
🔄 Avoid Repetition | Common logic (like calculations or printing) can be reused with one call |
🔄 Divide and Conquer | Complex logic can be broken into logical steps handled by different functions |
🏗️ Types of Functions in C:
Library Functions – Built-in (e.g.,
printf(),scanf(),sqrt())User-defined Functions – Created by the programmer for specific tasks
<div class="infographic-cftrace">
<style>
.infographic-cftrace {
--ink: #1a1a1a;
--ink-soft: #5c5c5c;
--paper: #ffffff;
--paper-alt: #f0ece4;
--accent: #2563eb;
--accent-soft:#e3ebfd;
--line: #ddd6c9;
--code-bg: #1e1e2e;
--code-text: #d4d4e0;
--syn-keyword: #c586c0;
--syn-func: #dcdcaa;
--syn-string: #ce9178;
--syn-comment: #6a9955;
--ok: #7fdb8a;
--ok-soft: #1f3a24;
}
.infographic-cftrace[data-theme="dark"] {
--ink: #e8e6e1;
--ink-soft: #9a968c;
--paper: #121212;
--paper-alt: #1e1e26;
--accent: #5b8def;
--accent-soft:#202942;
--line: #2c313d;
--code-bg: #0d0d14;
--code-text: #d4d4e0;
}
.infographic-cftrace {
box-sizing: border-box;
max-width: 760px;
margin: 0 auto;
background: var(--paper);
color: var(--ink);
font-family: 'Inter', -apple-system, sans-serif;
padding: 0 clamp(1rem, 4vw, 2.25rem) 1.25rem;
}
.infographic-cftrace *, .infographic-cftrace *::before, .infographic-cftrace *::after {
box-sizing: border-box;
}
.infographic-cftrace h1 {
font-family: 'Fraunces', Georgia, serif;
font-weight: 600;
font-size: clamp(1.3rem, 3.2vw, 1.6rem);
margin: 1.1rem 0 0.2rem;
}
.infographic-cftrace .subhead { color: var(--ink-soft); font-size: 0.85rem; margin-bottom: 0.85rem; }
.infographic-cftrace .code-panel {
background: var(--code-bg);
color: var(--code-text);
border-radius: 8px;
padding: 0.65rem 0.9rem;
font-family: 'IBM Plex Mono', monospace;
font-size: 0.78rem;
line-height: 1.5;
margin-bottom: 0.85rem;
}
.infographic-cftrace .code-line {
display: block;
white-space: pre;
border-radius: 4px;
padding: 0.05rem 0.4rem;
margin: 0 -0.4rem;
transition: background 0.3s, box-shadow 0.3s;
}
.infographic-cftrace .code-line.is-highlight {
background: rgba(91, 141, 239, 0.22);
box-shadow: inset 3px 0 0 var(--accent);
}
.infographic-cftrace .syn-keyword { color: var(--syn-keyword); }
.infographic-cftrace .syn-func { color: var(--syn-func); }
.infographic-cftrace .syn-string { color: var(--syn-string); }
.infographic-cftrace .syn-comment { color: var(--syn-comment); }
.infographic-cftrace .controls { display: flex; align-items: center; gap: 0.5rem; margin-bottom: 0.85rem; flex-wrap: wrap; }
.infographic-cftrace .ctrl-btn {
all: unset; cursor: pointer;
font-family: 'IBM Plex Mono', monospace;
font-size: 0.76rem; font-weight: 600;
padding: 0.38rem 0.85rem;
border-radius: 999px;
border: 1px solid var(--line);
background: var(--paper-alt);
color: var(--ink);
}
.infographic-cftrace .ctrl-btn.primary { background: var(--accent); color: #fff; border-color: var(--accent); }
.infographic-cftrace .ctrl-btn:disabled { opacity: 0.5; cursor: default; }
.infographic-cftrace .layout {
display: grid;
grid-template-columns: 1fr 210px;
gap: 1rem;
align-items: start;
}
@media (max-width: 620px) {
.infographic-cftrace .layout { grid-template-columns: 1fr; }
}
/* step list with spine */
.infographic-cftrace .flow { position: relative; padding-left: 2.6rem; }
.infographic-cftrace .flow-spine {
position: absolute; left: 1.05rem; top: 1.2rem; bottom: 1.2rem; width: 2px; background: var(--line);
}
.infographic-cftrace .flow-dot {
position: absolute; left: 1.05rem; top: 1.2rem; width: 10px; height: 10px; margin-left: -4px;
border-radius: 50%; background: var(--accent); box-shadow: 0 0 0 4px var(--accent-soft);
transition: top 0.5s cubic-bezier(.4,0,.2,1); opacity: 0;
}
.infographic-cftrace .flow-dot.is-visible { opacity: 1; }
.infographic-cftrace .flow-step { position: relative; margin-bottom: 0.32rem; cursor: pointer; }
.infographic-cftrace .step-icon {
position: absolute; left: -2.6rem; top: 0.22rem; width: 1.9rem; height: 1.9rem;
border-radius: 50%; background: var(--paper); border: 2px solid var(--line);
font-size: 0.85rem; display: flex; align-items: center; justify-content: center;
transition: border-color 0.25s, background 0.25s, transform 0.35s;
}
.infographic-cftrace .flow-step.is-active .step-icon {
border-color: var(--accent); background: var(--accent-soft); animation: cft-pop 0.5s ease;
}
@keyframes cft-pop { 0% { transform: scale(1); } 40% { transform: scale(1.22); } 100% { transform: scale(1); } }
.infographic-cftrace .step-box {
padding: 0.4rem 0.65rem; border: 1px solid var(--line); border-radius: 7px;
font-size: 0.78rem; transition: border-color 0.25s, background 0.25s;
}
.infographic-cftrace .flow-step.is-active .step-box { border-color: var(--accent); background: var(--accent-soft); }
.infographic-cftrace .step-box .mini {
margin-top: 0.1rem; font-family: 'IBM Plex Mono', monospace; font-size: 0.72rem; color: var(--accent); font-weight: 600;
min-height: 1em;
}
/* call stack visual */
.infographic-cftrace .stack-panel {
position: sticky; top: 1rem;
border: 1px dashed var(--line);
border-radius: 10px;
padding: 0.6rem;
min-height: 150px;
display: flex;
flex-direction: column-reverse;
gap: 0.4rem;
}
.infographic-cftrace .stack-title {
font-size: 0.68rem; font-weight: 700; text-transform: uppercase; letter-spacing: 0.04em;
color: var(--ink-soft); text-align: center; margin-bottom: 0.2rem;
}
.infographic-cftrace .frame {
border-radius: 7px;
padding: 0.45rem 0.6rem;
font-family: 'IBM Plex Mono', monospace;
font-size: 0.72rem;
background: var(--paper-alt);
border: 1px solid var(--line);
transition: border-color 0.3s, background 0.3s, box-shadow 0.3s;
}
.infographic-cftrace .frame .fname { font-weight: 700; margin-bottom: 0.15rem; }
.infographic-cftrace .frame .fline { color: var(--ink-soft); min-height: 1em; }
.infographic-cftrace .frame.is-active {
border-color: var(--accent); background: var(--accent-soft);
box-shadow: 0 3px 10px -4px rgba(0,0,0,0.25);
}
.infographic-cftrace .frame.frame-add {
opacity: 0; transform: translateY(10px) scale(0.94);
transition: opacity 0.35s ease, transform 0.35s ease;
max-height: 0; padding-top: 0; padding-bottom: 0; overflow: hidden; margin: 0;
}
.infographic-cftrace .frame.frame-add.is-present {
opacity: 1; transform: translateY(0) scale(1); max-height: 5rem; padding: 0.45rem 0.6rem; margin: 0;
}
.infographic-cftrace .frame.frame-add.is-returning {
animation: cft-return 0.6s ease forwards;
}
@keyframes cft-return {
0% { opacity: 1; transform: translateY(0) scale(1); }
100% { opacity: 0; transform: translateY(-14px) scale(0.9); }
}
.infographic-cftrace .fly-value {
position: absolute;
font-family: 'IBM Plex Mono', monospace;
font-weight: 700;
font-size: 0.95rem;
color: var(--ok);
left: 50%;
transform: translateX(-50%);
opacity: 0;
pointer-events: none;
}
.infographic-cftrace .fly-value.is-flying {
animation: cft-fly 0.9s ease forwards;
}
@keyframes cft-fly {
0% { opacity: 1; top: 70%; }
100% { opacity: 0; top: -10%; }
}
.infographic-cftrace .takeaway { font-size: 0.82rem; color: var(--ink-soft); margin-top: 1.1rem; border-top: 1px solid var(--line); padding-top: 0.85rem; }
.infographic-cftrace .takeaway strong { color: var(--ink); }
@media (prefers-reduced-motion: reduce) {
.infographic-cftrace * { transition: none !important; animation: none !important; }
}
</style>
<h1>📞 Function Call Trace</h1>
<p class="subhead">Watch control actually leave <code style="font-family:'IBM Plex Mono',monospace;">main()</code>, run inside <code style="font-family:'IBM Plex Mono',monospace;">add()</code>, and come back.</p>
<div class="code-panel">
<span class="code-line" data-line="l1"><span class="syn-keyword">int</span> <span class="syn-func">add</span>(<span class="syn-keyword">int</span> a, <span class="syn-keyword">int</span> b) {</span>
<span class="code-line" data-line="l2"> <span class="syn-keyword">int</span> sum = a + b;</span>
<span class="code-line" data-line="l3"> <span class="syn-keyword">return</span> sum;</span>
<span class="code-line" data-line="l4">}</span>
<span class="code-line"> </span>
<span class="code-line" data-line="l6"><span class="syn-keyword">int</span> <span class="syn-func">main</span>() {</span>
<span class="code-line" data-line="l7"> <span class="syn-keyword">int</span> result = <span class="syn-func">add</span>(3, 4);</span>
<span class="code-line" data-line="l8"> <span class="syn-func">printf</span>(<span class="syn-string">"%d"</span>, result);</span>
<span class="code-line" data-line="l9"> <span class="syn-keyword">return</span> 0;</span>
<span class="code-line" data-line="l10">}</span>
</div>
<div class="controls">
<button class="ctrl-btn primary" id="cft-play">▶ Play</button>
<button class="ctrl-btn" id="cft-restart">⟲ Restart</button>
</div>
<div class="layout">
<div class="flow" id="cft-flow">
<div class="flow-spine"></div>
<div class="flow-dot" id="cft-dot"></div>
<div class="flow-step" data-step="0">
<div class="step-icon">🏁</div>
<div class="step-box">main() is running<div class="mini"></div></div>
</div>
<div class="flow-step" data-step="1">
<div class="step-icon">📞</div>
<div class="step-box">Calls add(3, 4)<div class="mini">control jumps out of main()</div></div>
</div>
<div class="flow-step" data-step="2">
<div class="step-icon">🚪</div>
<div class="step-box">Enters add()<div class="mini">a = 3, b = 4</div></div>
</div>
<div class="flow-step" data-step="3">
<div class="step-icon">🧮</div>
<div class="step-box">Runs the body<div class="mini">sum = 3 + 4 → 7</div></div>
</div>
<div class="flow-step" data-step="4">
<div class="step-icon">↩️</div>
<div class="step-box">return sum;<div class="mini">sends 7 back, exits add()</div></div>
</div>
<div class="flow-step" data-step="5">
<div class="step-icon">✅</div>
<div class="step-box">Back in main()<div class="mini">result = 7</div></div>
</div>
<div class="flow-step" data-step="6">
<div class="step-icon">🖨️</div>
<div class="step-box">printf runs<div class="mini">prints 7</div></div>
</div>
</div>
<div class="stack-panel" id="cft-stack" style="position:relative;">
<div class="frame frame-main">
<div class="fname">main()</div>
<div class="fline">result = <span id="cft-result">?</span></div>
</div>
<div class="frame frame-add" id="cft-addframe">
<div class="fname">add(a=3, b=4)</div>
<div class="fline">sum = <span id="cft-sum">?</span></div>
</div>
<div class="fly-value" id="cft-fly">7</div>
</div>
</div>
<p class="takeaway"><strong>Every call works the same way:</strong> a new frame is pushed for the function being called, it does its work, and <code style="font-family:'IBM Plex Mono',monospace;">return</code> pops that frame and hands the value back down.</p>
</div>
<script>
(function () {
const wrapper = document.currentScript.closest('.infographic-cftrace')
|| document.querySelector('.infographic-cftrace');
const bodyAttr = document.body.getAttribute('data-darkmode');
let dark;
if (bodyAttr === 'true') { dark = true; }
else if (bodyAttr === 'false') { dark = false; }
else {
const match = document.cookie.match(/(?:^|;\s*)darkMode=([^;]+)/);
dark = match ? match[1] === 'true' : false;
}
if (dark) wrapper.setAttribute('data-theme', 'dark');
function $(sel) { return wrapper.querySelector(sel); }
const steps = Array.from(wrapper.querySelectorAll('#cft-flow .flow-step'));
const dot = $('#cft-dot');
const flowList = $('#cft-flow');
const playBtn = $('#cft-play');
const restartBtn = $('#cft-restart');
const codeLines = wrapper.querySelectorAll('.code-panel .code-line[data-line]');
const addFrame = $('#cft-addframe');
const mainFrame = wrapper.querySelector('.frame-main');
const sumEl = $('#cft-sum');
const resultEl = $('#cft-result');
const flyEl = $('#cft-fly');
// step -> which code line(s) to highlight
const LINE_MAP = { 0: 'l6', 1: 'l7', 2: 'l1', 3: 'l2', 4: 'l3', 5: 'l7', 6: 'l8' };
function highlightLine(lineId) {
codeLines.forEach(l => l.classList.toggle('is-highlight', l.dataset.line === lineId));
}
function moveDotTo(step) {
const icon = step.querySelector('.step-icon');
const top = icon.offsetTop + icon.offsetHeight / 2 - flowList.offsetTop;
dot.style.top = top + 'px';
dot.classList.add('is-visible');
}
function resetStack() {
addFrame.classList.remove('is-present', 'is-returning', 'is-active');
mainFrame.classList.remove('is-active');
sumEl.textContent = '?';
resultEl.textContent = '?';
flyEl.classList.remove('is-flying');
}
function applyStep(index) {
const step = steps[index];
steps.forEach(s => s.classList.remove('is-active'));
step.classList.add('is-active');
moveDotTo(step);
highlightLine(LINE_MAP[index]);
mainFrame.classList.remove('is-active');
addFrame.classList.remove('is-active');
if (index === 0) {
mainFrame.classList.add('is-active');
} else if (index === 1) {
mainFrame.classList.add('is-active');
} else if (index === 2) {
addFrame.classList.add('is-present', 'is-active');
addFrame.classList.remove('is-returning');
} else if (index === 3) {
addFrame.classList.add('is-present', 'is-active');
sumEl.textContent = '7';
} else if (index === 4) {
addFrame.classList.add('is-present', 'is-active', 'is-returning');
flyEl.classList.remove('is-flying');
void flyEl.offsetWidth;
flyEl.classList.add('is-flying');
} else if (index === 5) {
addFrame.classList.remove('is-present', 'is-returning', 'is-active');
mainFrame.classList.add('is-active');
resultEl.textContent = '7';
} else if (index === 6) {
mainFrame.classList.add('is-active');
}
}
let idx = -1;
let timer = null;
let playing = false;
function stop() {
playing = false;
clearTimeout(timer);
playBtn.disabled = false;
playBtn.textContent = '▶ Play';
}
function advance() {
idx++;
if (idx >= steps.length) { stop(); return; }
applyStep(idx);
if (idx >= steps.length - 1) stop();
else timer = setTimeout(advance, 1300);
}
function play() {
if (playing) return;
if (idx >= steps.length - 1) restart();
playing = true;
playBtn.disabled = true;
playBtn.textContent = '⏸ Playing…';
advance();
}
function restart() {
clearTimeout(timer);
playing = false;
idx = -1;
playBtn.disabled = false;
playBtn.textContent = '▶ Play';
steps.forEach(s => s.classList.remove('is-active'));
dot.classList.remove('is-visible');
resetStack();
codeLines.forEach(l => l.classList.remove('is-highlight'));
}
steps.forEach((step, i) => {
step.addEventListener('click', () => {
clearTimeout(timer);
playing = false;
playBtn.disabled = false;
playBtn.textContent = '▶ Play';
idx = i;
// replay stack state from step 0 up to i so pushes/pops are consistent
resetStack();
for (let s = 0; s <= i; s++) applyStep(s);
});
});
playBtn.addEventListener('click', play);
restartBtn.addEventListener('click', restart);
restart();
})();
</script>🧱 Function Structure in a C Program:
#include <stdio.h>
// Function Declaration
int square(int n);
// Main Function
int main() {
int result = square(4);
printf("Square = %d", result);
return 0;
}
// Function Definition
int square(int n) {
return n * n;
}📌 Terminology:
Term | Meaning |
|---|---|
Function Call | Invoking a function (e.g., |
Parameters | Input values passed to a function |
Arguments | Actual values used during function call |
Return Value | Output/result that function sends back (optional) |
🧮 Real-Life Analogy:
Think of a function like a coffee machine:
You give it inputs: water and coffee beans (parameters)
It processes them internally (function body)
You get output: coffee (return value)