---
title: "Switch versus user defined function"
subject: "Computer Programming"
chapter: 5
key: "semester_1/computer_programming:switch-vs-user-function"
source: https://www.bctengineer.com/q/semester_1/computer_programming/switch-vs-user-function
site: BCT Engineering
---

# Switch versus user defined function

Computer Programming · Chapter 5: Control statements

- Asked in: 2067 Asar, 4 marks
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_1/computer_programming/switch-vs-user-function
- Whole chapter: https://www.bctengineer.com/md/semester_1/computer_programming/chapter-5

## Question

State with example, how switch( ) function differs from user defined function in computer programming language C.

## Short answer

switch is a built‑in statement for multi‑way branching based on a value, cannot be called, and does not promote code reuse. A user‑defined function is a separately written, callable block of code that can accept arguments and return a value, supporting modularity.

## Answer

| `switch` | User-defined function |
|---|---|
| A selection **statement** of the language (keyword) | A block of code written by the programmer, with a name |
| Chooses one of many blocks depending on a value | Performs a specific task and can return a value |
| Cannot be called, and takes no arguments | Is called from other places and can take arguments |
| Written inside a function (such as `main`) | Written separately and defined once |
| Does not support code reuse | Supports code reuse and modularity |

```c
#include <stdio.h>
int square(int n)          /* user-defined function */
{
    return n * n;
}
int main(void)
{
    int choice = 2;
    switch (choice)        /* switch is a statement, not a function */
    {
        case 1: printf("One\n"); break;
        case 2: printf("Square of 6 = %d\n", square(6)); break;
    }
    return 0;
}
```
Output:
```text
Square of 6 = 36
```

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. What is switch in C?
   A. A selection statement (keyword) of the language
   B. A library function
   C. A user-defined function
   D. A preprocessor directive
   Answer: A. switch is a language statement, not a function. (Key point: Nature of switch)

2. Which can be called from other places and take arguments?
   A. The switch statement
   B. A user-defined function
   C. Both of them
   D. Neither of them
   Answer: B. Functions are called with arguments; switch cannot be called. (Key point: Calling)

3. Which supports code reuse and modularity?
   A. switch statement
   B. Both equally
   C. User-defined function
   D. Neither
   Answer: C. A function is defined once and called many times. (Key point: Reuse)

4. In the example, choice = 2 prints what?
   A. One
   B. Square of 6 = 12
   C. Nothing
   D. Square of 6 = 36
   Answer: D. Case 2 calls square(6), which returns 36. (Key point: Example output)

## Related in Computer Programming

- [Significance of user defined functions](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Asignificance-user-defined-functions) — Computer Programming, chapter 6
- [Define programming language](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Adefine-programming-language) — Computer Programming, chapter 1, asked 3×
- [How a function is defined in C](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afunction-definition-in-c) — Computer Programming, chapter 6
- [Computer program and programming](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Adefine-program-programming) — Computer Programming, chapter 2
- [Function definition; sum of two numbers](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afunction-definition-sum-two) — Computer Programming, chapter 6
- [Palindrome string using function](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Apalindrome-string-function) — Computer Programming, chapter 7

## Related in other subjects

- [Convert user-defined type to basic type](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Auser-to-basic-conversion) — Object Oriented Programming, chapter 5
- [Higher-level language versus OO approach](https://www.bctengineer.com/question.md?key=semester_8%2Fmultimedia_system%3Ahll-vs-oo-approach) — Multimedia System, chapter 7
- [Series programming of a third-order transfer function](https://www.bctengineer.com/question.md?key=semester_6%2Fdigital_control_system%3Aseries-programming-z-plus-1) — Digital Control System, chapter 4, asked 2×
- [Ladder programming of a third-order transfer function](https://www.bctengineer.com/question.md?key=semester_6%2Fdigital_control_system%3Aladder-z3-4z2) — Digital Control System, chapter 4, asked 2×
