Skip to main content
C Programming Basics
CHAPTER 09 Beginner

Functions in C

Updated: May 17, 2026
5 min read

# CHAPTER 9

Functions in C

1. Introduction

Imagine building a house where you have to manufacture every screw, plank, and window from scratch. That’s what programming without functions is like. A function is a block of reusable code designed to perform a specific task. printf() and main() are functions you already use!

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Understand why functions are essential for modularity.
  • Differentiate between function declaration, definition, and call.
  • Pass parameters/arguments to functions.
  • Return values from functions.
  • Understand local vs. global scope.

3. Why Use Functions?

  • Reusability: Write once, use many times.
  • Modularity: Break a large, complex program into small, manageable chunks.
  • Maintainability: Easier to debug isolated blocks of code.

4. Creating a Function

Creating a function involves three parts:
  1. 1. Declaration (Prototype): Tells the compiler the function exists (name, return type, parameters).
  1. 2. Definition: The actual code the function executes.
  1. 3. Call: When you use the function in your program.
c
12345678910111213141516
#include <stdio.h>

// 1. Function Declaration (Prototype)
void sayHello(); 

int main() {
    // 3. Function Call
    sayHello(); 
    sayHello();
    return 0;
}

// 2. Function Definition
void sayHello() {
    printf("Hello there!\n");
}

5. Parameters and Arguments

Information can be passed to functions as parameters.
c
12345678910111213
#include <stdio.h>

// Function taking a string (character array) and an integer
void greetUser(char name[], int age) {
    printf("Hello %s, you are %d years old.\n", name, age);
}

int main() {
    // Calling with arguments
    greetUser("Alice", 25);
    greetUser("Bob", 30);
    return 0;
}

6. Return Values

Functions can send data back to where they were called using the return keyword.
c
12345678910111213
#include <stdio.h>

// 'int' means this function returns an integer
int addNumbers(int a, int b) {
    int sum = a + b;
    return sum;
}

int main() {
    int result = addNumbers(5, 10);
    printf("The sum is: %d\n", result);
    return 0;
}

7. Scope: Local vs Global Variables

  • Local Variables: Declared *inside* a function. They only exist inside that function.
  • Global Variables: Declared *outside* all functions. They can be accessed by any function.
c
1234567891011121314
#include <stdio.h>

int globalVar = 100; // Global

void myFunction() {
    int localVar = 10; // Local
    printf("Global: %d, Local: %d\n", globalVar, localVar);
}

int main() {
    myFunction();
    // printf("%d", localVar); // ERROR! localVar does not exist here.
    return 0;
}

8. Introduction to Recursion

A function that calls itself is called a recursive function. (We will cover this deeply in Chapter 18).
c
123456
void count(int n) {
    if (n == 0) return; // Base case (stopping condition)
    printf("%d ", n);
    count(n - 1); // Function calls itself
}
// Calling count(3) prints: 3 2 1

9. Memory-Level Explanation (The Call Stack)

When a function is called, the OS allocates a "Stack Frame" on the Call Stack. This frame holds the function's local variables and parameters. When the function returns, its stack frame is "popped" (destroyed), and memory is freed.
123456
Call Stack when addNumbers(5, 10) is called:
+------------------------+
| addNumbers() Frame     |  <- a=5, b=10, sum=15
+------------------------+
| main() Frame           |  <- result=?
+------------------------+

10. Common Mistakes

  • Forgetting the return statement: If a function specifies int return type, failing to return an integer causes undefined behavior.
  • Type mismatch: Passing a float into a function that expects an int.
  • Calling before declaring: If you define a function *below* main(), you MUST provide a prototype declaration *above* main().

11. Exercises

  1. 1. Write a function float celsiusToFahrenheit(float c) that converts and returns a temperature.
  1. 2. Write a function void printStars(int count) that prints a line of asterisks based on the count.
  1. 3. Write a program with a global variable, and modify it from inside a function.

12. MCQ Quiz with Answers

Question 1

What is the default return type of a function in C if none is specified (in older C)?

Question 2

What keyword is used to send a value back from a function?

Question 3

Variables declared inside a function are called:

Question 4

What is a function prototype?

Q5. Can a function return multiple values directly? a) Yes b) No Answer: b) No (you must use pointers or structures to achieve this)
Question 6

What does void mean when placed before a function name?

Question 7

What data structure does the OS use to manage function calls?

Question 8

The values passed to a function during a call are called:

Question 9

Which function is called automatically when a C program starts?

Question 10

Can a function call itself?

13. Interview Questions

  • Q: Explain the difference between pass-by-value and pass-by-reference. (Note: C only truly supports pass-by-value; we simulate reference with pointers).
  • Q: What is the Call Stack and what is a Stack Overflow?
  • Q: Why use function prototypes?

14. Summary

Functions break code into modular, reusable blocks. A function consists of a declaration, definition, and call. Functions can take arguments (inputs) and return values (outputs). Local variables exist only within their function's stack frame, which is destroyed upon return.

15. Next Chapter Recommendation

Functions often need to process large amounts of data. In Chapter 10: Arrays in C, we will learn how to store and manipulate lists of data efficiently.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·