C Programming · Unit 7
Functions and Recursion
Exam-focused notes for Functions and Recursion (C Programming, BIT102): what the TU syllabus asks and how it has actually been tested, with 6 solved past questions from this unit.
What this unit covers
- Function declaration definition and calling
- Function advantages
- Pass by value
- Pass by reference
- Call by reference for swapping
- Recursive functions
- Factorial using recursion
- External functions
- Arrays as function arguments
- Functions returning values
Call by reference for swapping
Explain call by reference of a function with suitable example. [5]
Call by reference is a method of passing arguments to a function where the address (reference) of the actual argument is passed to the formal parameter of the function. Any changes made to the formal parameter inside the function directly affect the actual ...
Full solved answer →When do we need external function?Illustrate the process of swapping the value of two variables using the concept of pass by value and pass by reference.[3+7]
--- We need external functions in the following situations: 1. Code Reusability: When the same block of code needs to be used in multiple programs or multiple source files, we define it as an external function so it can be called from anywhere without rewri...
Full solved answer →Factorial using recursion
What is recursion? Write a program to find the factorial of a given integer using recursion. [5]
Recursion is a programming technique where a function calls itself directly or indirectly to solve a problem. A recursive function solves a problem by breaking it down into smaller subproblems of the same type until it reaches a base case (a condition where...
Full solved answer →Arrays as function arguments
Write a program with a function that takes an array as an argument and returns its sum to the main function. [5]
--- --- Concept Description ------ Function argument arr[] passes the array by reference (address); size passes the count by value Return value return sum; sends the computed sum back to main() Function declaration Prototype int arraySum(int arr[], int size...
Full solved answer →Recursive functions
Explain the role of function in programming. How function is declared, defined and called in C?Write a grogram to find factorial of 'n' using recursive function.[5+5]
--- (a) Role of Functions in Programming A function is a self-contained, named block of code that performs a specific task and can be called (invoked) from anywhere in the program. Role Description ------------------- Modularity Breaks a large, complex prog...
Full solved answer →Function advantages
Discuss the advantages of using functions in programming. Differentiate between recursive function and normal function.Write a program using call by reference to swap the values of two variables.[6+4]
--- A function is a self-contained block of code that performs a specific task and can be called multiple times from different parts of a program. 1. Modularity - Functions break a large, complex program into smaller, manageable modules. - Each function han...
Full solved answer →Make Unit 7 stick
Practice BIT102 with flashcards & quizzes