Concept of functions

A function is a named, reusable block that takes inputs, does one job and returns a result, so the marks program stops repeating itself and main() becomes a readable table of contents.

10 min read · 10 cards · 3 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

The program that repeats itself

Look honestly at the marks program by now: the percentage formula appears when printing a marksheet, again inside the grade ladder, and again in the class summary. Three copies.

Last week the formula changed (out of 400, not 300). You fixed two copies and forgot the third. Two screens now disagree, and nobody knows which is right.

Repeated code is not extra typing; it is repeated bugs. The cure is the biggest structuring idea in C: the function.

Theory

The tiffin service

A hostel does not cook in every room. There is ONE kitchen: raw ingredients go in, a finished tiffin comes out, and nobody eating needs to know the recipe's internals. A function is that kitchen: inputs in (arguments), one job done inside, one dish out (return value). Improve the recipe once, and every meal improves, no room-by-room fixing.

Theory

Functions, formally

A function is a named block that performs one job:

float percentage(int obtained, int total) {

return (obtained * 100.0) / total;

}

Read the header: return type (float comes back), name, parameter list (what must be supplied). Using it is a call:

p = percentage(marks, 300);

You have been calling functions since day one: printf, scanf, getch, kitchens someone else built. Even main is a function, the one the OS itself calls.

Practical

One kitchen, used twice

#include <stdio.h>

float percentage(int obtained, int total) {
    return (obtained * 100.0) / total;
}

int main() {
    printf("Riya : %.2f%%\n", percentage(234, 300));
    printf("Aman : %.2f%%\n", percentage(198, 300));
    return 0;
}

This example runs in Gri-Learn on the web, where you can edit it and see the output.

Think first

Follow the jump

When main reaches percentage(234, 300), what happens step by step? Where does control GO, what travels with it, and how does main resume? Trace it before revealing.

Show the answer

main pauses mid-line. Control jumps to percentage, carrying the values 234 and 300 into obtained and total. The body computes 78.0 and return sends it back, control lands exactly where main paused, and the returned 78.0 takes the call's place inside printf.

C remembers where to resume using a stack of calls, the very LIFO structure you will formally meet in BCA304. Foreshadowing, free of charge.

Quiz

The percentage formula changes again (out of 500). In the function version of the program, how many places need editing?

  1. One: inside the percentage() function
  2. One per call: every line that calls percentage()
  3. Three, same as the copy-paste version
  4. Zero: functions update themselves
Show the answer

One: inside the percentage() function

Callers never contained the formula, only requests for it, so the recipe changes in its single kitchen and every caller is instantly correct. This is the DRY principle (Don't Repeat Yourself), and "state the advantages of functions" expects exactly this: fix once, correct everywhere, plus readability, testability, reuse.

Quiz

Which statement about main() is TRUE?

  1. main is itself a function, the one the operating system calls
  2. main is a keyword, not a function
  3. main cannot call other functions
  4. A C program may have several main functions
Show the answer

main is itself a function, the one the operating system calls

main is a genuine function with a return type (int) and a return value (0 to the OS): its only privilege is being the agreed entry point the OS invokes. It calls others freely, exactly one main may exist (Unit 1's rule), and it is not a keyword, which is why C lets you, unwisely, name a variable main inside another function.

Watch out

Where marks leak

Definition vs call: defining a function (writing the kitchen) runs NOTHING; code executes only when CALLED, exams show a defined-but-never-called function and ask what prints (answer: nothing from it). Multi-job functions: "computeAndPrintAndSave" is a design smell; one function, one job, say it in theory answers. Missing return: a function promising float must return one on every path.

Theory

main() as a table of contents

Professionally refactored, the marks program's main() will read like an index: readMarks(...); computeResults(...); printMarksheet(...), a story anyone can follow, each chapter testable alone. The rest of this unit builds that skill: ready-made function libraries (string and math), writing your own properly, two ways of passing arguments, and a function that calls itself.

Summary

Key takeaways

  • A function: named block, ONE job, inputs (parameters), one return value.
  • Calls jump in with argument values and resume exactly where they left, via a call stack.
  • Fix once, correct everywhere: repeated code is repeated bugs (DRY).
  • printf and scanf are functions; main is a function the OS calls.
  • Definition runs nothing; only calls execute the body.
  • Benefits for exams: reuse, readability, testability, teamwork.
  • Memory hook: the tiffin kitchen, one recipe, every meal improves.

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Functions and Structure

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

Concept of functions · Computer Programming and Programming Methodology (CPPM) · Gri-Learn