Skip to content
StudyFame

Functions in C

As programs grow, writing everything inside main() becomes messy. Imagine a 500-line main() where the same calculation is copied 10 times.

A function is a named block of code that does one job. You write it once and call it whenever you need it. In this lesson you will learn what functions are, why we use them, and how to write your first one.

What You Will Learn

  • What a function is.
  • Why we use functions (advantages).
  • Library functions vs user-defined functions.
  • The parts of a function: name, parameters, return type, body.
  • How calling a function works.

What is a Function in C?

Exam definition: A function is a self-contained block of statements that performs a specific task. It can take input values (parameters) and can give back a result (return value). Every C program has at least one function: main().

Memory trick: A function is like a juicer machine: you put fruit in (arguments), it does its job (body), and juice comes out (return value). You don't rebuild the machine every time; you just use (call) it.

Why do we use functions?

  • Reusability: write once, call many times.
  • Less repetition: no copy-pasting the same code.
  • Easy to read: a big program is split into small, named parts.
  • Easy to test and fix: a bug is found in one small function, not in 500 lines.
  • Teamwork: different people can write different functions.

Types of functions

TypeWritten byExamples
Library (built-in) functionsAlready written, come with Cprintf(), scanf(), sqrt(), strlen()
User-defined functionsYou, the programmerarea(), is_even(), show_menu()

Parts of a function

PartMeaningIn int add(int a, int b)
Return typeType of value it gives back (void = nothing)int
NameWhat you call itadd
ParametersInputs it receivesint a, int b
BodyThe statements inside { }{ return a + b; }

Syntax of a Function in C

return_type function_name(parameter_list)
{
    /* body */
    return value;      /* not needed if return_type is void */
}

To use it, call it by name: result = function_name(arguments);

Example 1: Your First User-Defined Function

#include <stdio.h>

int add(int a, int b)          /* function definition */
{
    return a + b;
}

int main(void)
{
    int x = add(10, 20);       /* function call */
    printf("10 + 20 = %d\n", x);
    printf("5 + 7 = %d\n", add(5, 7));
    return 0;
}

Output

10 + 20 = 30
5 + 7 = 12

Code Explanation

  1. int add(int a, int b): a function named add that takes two ints and returns an int.
  2. return a + b;: sends the sum back to whoever called it.
  3. add(10, 20): calls the function. 10 goes into a, 20 into b. It returns 30, which is stored in x.
  4. add(5, 7): the same function is reused with different values; its result goes straight into printf.

How a Function Call Works

  1. main() reaches add(10, 20) and pauses.
  2. The values 10 and 20 are copied into the parameters a and b.
  3. The body of add runs.
  4. return sends the result back, and main() continues from where it paused.
  5. The values you pass are called arguments (actual parameters); the variables that receive them are parameters (formal parameters).

Example 2: Reusing a Function (Area of Three Circles)

#include <stdio.h>

float circle_area(float r)
{
    return 3.14159f * r * r;
}

int main(void)
{
    printf("Radius 1: area = %.2f\n", circle_area(1.0f));
    printf("Radius 2: area = %.2f\n", circle_area(2.0f));
    printf("Radius 5: area = %.2f\n", circle_area(5.0f));
    return 0;
}

Output

Radius 1: area = 3.14
Radius 2: area = 12.57
Radius 5: area = 78.54

Without a function, the formula would be written three times. With a function, it is written once, and if it ever needs a change, you fix it in one place.

Important Points to Remember

  • A function is a block of code that does one specific task.
  • Two types: library (built-in) and user-defined.
  • Parts: return type, name, parameters, body.
  • Every program has main(), where execution starts.
  • Main benefit: reusability.

Common Mistakes Beginners Make

MistakeWhat happensFix
Calling a function before it is defined or declaredError or warning: implicit declarationDefine it above main() or add a prototype
Missing return in a non-void functionGarbage return valueAlways return a value
Writing a function inside another functionError: nested functions are not standard CWrite each function separately
Wrong number of argumentsError: too few/many argumentsMatch the parameter list

Functions in C Practice Questions

  1. What is a function? Give two advantages.
  2. What is the difference between library and user-defined functions?
  3. Name the four parts of a function.
  4. What is the difference between arguments and parameters?
  5. What does the return type void mean?
Show answers
  1. A block of code that does a specific task. Advantages: reusability, easier to read and debug.
  2. Library functions come with C (printf, sqrt); user-defined functions are written by the programmer.
  3. Return type, name, parameters, body.
  4. Arguments are the values passed in the call; parameters are the variables in the definition that receive them.
  5. The function returns no value.

Coding Exercise

Write a function int cube(int n) that returns n × n × n. Call it from main() for 2, 3 and 4.

Show solution
#include <stdio.h>

int cube(int n)
{
    return n * n * n;
}

int main(void)
{
    printf("2^3 = %d\n", cube(2));
    printf("3^3 = %d\n", cube(3));
    printf("4^3 = %d\n", cube(4));
    return 0;
}
2^3 = 8
3^3 = 27
4^3 = 64

Quick Quiz: Functions in C (MCQ)

  1. Which function must every C program have?

    A) printf()   B) main()   C) start()   D) scanf()

  2. sqrt() is a:

    A) User-defined function   B) Library function   C) Keyword   D) Operator

  3. In float area(float r), the return type is:

    A) area   B) r   C) float   D) void

  4. The main advantage of functions is:

    A) Slower code   B) Reusability   C) More memory   D) More typing

  5. Values passed when calling a function are called:

    A) Parameters   B) Arguments   C) Returns   D) Keywords

Show answers
  1. B: main().
  2. B: a library function.
  3. C: float.
  4. B: reusability.
  5. B: arguments (actual parameters).

Quick Revision for Exams and Interviews

Read this before your exam. The highlighted words are the answer.

Must-remember facts

FunctionBlock of code for one specific task
TypesLibrary (built-in), User-defined
PartsReturn type, name, parameters, body
Compulsory functionmain()
ArgumentsValues passed in the call (actual)
ParametersVariables in the definition (formal)
void returnReturns nothing

Frequently asked questions

What are the advantages of functions?
Reusability, less repetition, easier reading, easier debugging and teamwork.
What is the difference between actual and formal parameters?
Actual parameters are the values passed in the call; formal parameters are the variables in the function definition.
Can a function be defined inside another function in C?
No, standard C does not allow nested function definitions.
Is main() a user-defined function?
Yes: its body is written by you, but its name and role are fixed by C.

Remember

  • Function = juicer machine: fruit in, juice out.
  • Write once, call many times.

Next Topic

Next lesson: Function Declaration, Definition and Call. You will learn the three steps of using a function: declaration, definition and call.