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. Declaration (Prototype): Tells the compiler the function exists (name, return type, parameters).
- 2. Definition: The actual code the function executes.
- 3. Call: When you use the function in your program.
c
5. Parameters and Arguments
Information can be passed to functions as parameters.
c
6. Return Values
Functions can send data back to where they were called using thereturn keyword.
c
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
8. Introduction to Recursion
A function that calls itself is called a recursive function. (We will cover this deeply in Chapter 18).
c
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.10. Common Mistakes
-
Forgetting the return statement: If a function specifies
intreturn type, failing toreturnan integer causes undefined behavior.
-
Type mismatch: Passing a
floatinto a function that expects anint.
-
Calling before declaring: If you define a function *below*
main(), you MUST provide a prototype declaration *above*main().
11. Exercises
-
1.
Write a function
float celsiusToFahrenheit(float c)that converts and returns a temperature.
-
2.
Write a function
void printStars(int count)that prints a line of asterisks based on the count.
- 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?
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?