Nearby lessons

71 of 124

C - Function Introduction

A function is a named block of code that performs one job and can be called from anywhere. Learn why functions exist, the anatomy of a C function, and how main fits into the picture.

Why Functions Exist

Without functions, repeated logic must be copied. Here the same three lines appear twice — and would need fixing twice:

Example01
CCode Cell
1#include <stdio.h>
2 
3int main()
4{
5 int a = 5, b = 3, sum1, sum2;
6 
7 /* Repeated logic - copy 1 */
8 sum1 = a + b;
9 printf("Sum of %d and %d is %d\n", a, b, sum1);
10 
11 /* Repeated logic - copy 2 */
12 sum2 = 10 + 20;
13 printf("Sum of %d and %d is %d\n", 10, 20, sum2);
14 return 0;
15}
Output
Sum of 5 and 3 is 8
Sum of 10 and 20 is 30

The Same Program with a Function

One definition, two calls. Fix a bug once and every caller benefits:

Example02
CCode Cell
1#include <stdio.h>
2 
3void showSum(int x, int y) /* written once */
4{
5 printf("Sum of %d and %d is %d\n", x, y, x + y);
6}
7 
8int main()
9{
10 showSum(5, 3); /* called many times */
11 showSum(10, 20);
12 showSum(100, 250);
13 return 0;
14}
Output
Sum of 5 and 3 is 8
Sum of 10 and 20 is 30
Sum of 100 and 250 is 350

Anatomy of a Function

Every C function has the same five parts:

PartIn int add(int a, int b) { return a + b; }Purpose
Return typeintThe type of value sent back
NameaddHow callers refer to it
Parameters(int a, int b)The inputs it accepts
Body{ ... }The work it does
Return statementreturn a + b;Sends the result to the caller
Example03
CCode Cell
1#include <stdio.h>
2 
3int add(int a, int b) /* return type, name, parameters */
4{
5 return a + b; /* body with a return statement */
6}
7 
8int main()
9{
10 int result = add(7, 5); /* the call */
11 printf("add(7, 5) = %d\n", result);
12 return 0;
13}
Output
add(7, 5) = 12

How Control Flows

A call pauses the caller, runs the function, then resumes exactly where it left off. The print order proves it:

Example04
CCode Cell
1#include <stdio.h>
2 
3void greet(void)
4{
5 printf("2. Inside greet\n");
6 printf("3. Leaving greet\n");
7}
8 
9int main()
10{
11 printf("1. Before the call\n");
12 greet(); /* control jumps into greet */
13 printf("4. After the call\n"); /* resumes here */
14 return 0;
15}
Output
1. Before the call
2. Inside greet
3. Leaving greet
4. After the call

main Is a Function Too

In simple words: main is not special syntax — it is an ordinary function that the operating system calls to start your program. Its return 0; reports success to the OS. Everything you learn about functions applies to main as well.
Example05
CCode Cell
1#include <stdio.h>
2 
3int main(void) /* returns int, takes no arguments */
4{
5 printf("Program running\n");
6 return 0; /* 0 = success, non-zero = error */
7}
Output
Program running

Built-in and User-Defined

C ships with a standard library of ready-made functions; everything else you write yourself:

Library functionsUser-defined functions
Who wrote themThe C standard libraryYou
How to use#include the headerDefine them in your file
Examplesprintf, strlen, sqrtadd, calculateSalary
Source availableUsually notYes — it is yours
Example06
CCode Cell
1#include <stdio.h>
2#include <math.h>
3#include <string.h>
4 
5int square(int n) /* user-defined */
6{
7 return n * n;
8}
9 
10int main()
11{
12 printf("sqrt(16) = %.1f\n", sqrt(16.0)); /* library */
13 printf("strlen = %zu\n", strlen("Hello")); /* library */
14 printf("square(6) = %d\n", square(6)); /* mine */
15 return 0;
16}
Output
sqrt(16)   = 4.0
strlen     = 5
square(6)  = 36

What Functions Buy You

  • Reusability — write once, call anywhere.
  • ReadabilitycalculateTax(salary) explains itself; twenty inline lines do not.
  • Testability — a small function can be checked in isolation.
  • Maintainability — one place to fix a bug.
  • Teamwork — different people can own different functions.
  • Abstraction — callers use the name and ignore the details.
Example07
CCode Cell
1#include <stdio.h>
2 
3/* Each function does exactly one thing */
4float area(float r) { return 3.14159f * r * r; }
5float circumference(float r) { return 2 * 3.14159f * r; }
6 
7int main()
8{
9 float r = 5.0f;
10 
11 printf("Radius : %.1f\n", r);
12 printf("Area : %.2f\n", area(r));
13 printf("Circumference : %.2f\n", circumference(r));
14 return 0;
15}
Output
Radius        : 5.0
Area          : 78.54
Circumference : 31.42

One Function, One Job

The single most useful rule of function design: if you need "and" to describe what a function does, split it.

Example08
CCode Cell
1#include <stdio.h>
2 
3/* Each step is separate and independently testable */
4float celsiusToFahrenheit(float c) { return (c * 9 / 5) + 32; }
5int isFreezing(float c) { return c <= 0; }
6 
7int main()
8{
9 float temps[3] = {-5.0f, 25.0f, 0.0f};
10 int i;
11 
12 for (i = 0; i < 3; i++)
13 printf("%6.1fC = %6.1fF %s\n",
14 temps[i],
15 celsiusToFahrenheit(temps[i]),
16 isFreezing(temps[i]) ? "(freezing)" : "");
17 return 0;
18}
Output
  -5.0C =   23.0F  (freezing)
  25.0C =   77.0F
   0.0C =   32.0F  (freezing)
📝 Key Takeaways
  • A function groups code under a name so it can be reused.
  • Every C program starts execution at main().
  • A function has a return type, a name, parameters and a body.
  • Calling a function transfers control; return sends it back.
  • Functions turn one long program into small testable pieces.

🧠 Test Your Knowledge

4 Questions
Progress: 0 / 4