C Programming · Unit 7 · 5 hrs
Functions
Exam-focused notes for Functions (C Programming, CSC115): what the TU syllabus asks and how it has actually been tested, with 12 solved past questions from this unit.
What this unit covers
- Library Functions
- User defined functions
- Function prototype, Function call, and Function Definition
- Nested and Recursive Function
- Function Arguments and Return Types
- Passing Arrays to Function
- Passing Strings to Function
- Passing Arguments by Value
- Passing Arguments by Address
- Scope visibility and lifetime of a variable
- Local and Global Variable
Passing Arguments by Address
Differentiate between library function and user defined function.Write a program to swap two values using call by reference concept.[2+8]
--- (a) Difference Between Library Function and User Defined Function As per the course notes, C programs have two types of functions: Library Functions and User Defined Functions. S.N. Library Function User Defined Function --------------------------------...
Full solved answer →Nested and Recursive Function
Demonstrate the use of recursive function with a suitable example. [5]
A recursive function is a function that calls itself during its own execution. Each recursive call works on a smaller version of the problem until a base condition (terminating condition) is reached, which stops further recursion. --- Component Description ...
Full solved answer →Write a program to calculate the factorial of a given number using recursion. [5]
A recursive function is a function that calls itself. It must have: 1. Base condition (stopping condition) -- when n == 0, return 1 2. Recursive call -- each call must move closer to the base condition $$n! = \begin{cases} 1 & \text{if } n = 0 \\ n \times (...
Full solved answer →Write a program to find the sum of digits of a given integer using recursion. [5]
The sum of digits of a number can be found recursively by: - Base case: If the number is 0, return 0 - Recursive case: Return (number % 10) + sumOfDigits(number / 10) For example, for n = 123: - 123 % 10 = 3, then call sumOfDigits(12) - 12 % 10 = 2, then ca...
Full solved answer →Local and Global Variable
Write short notes on: a) Global variable b) Debugging Debugging [2.5+2.5]
--- Definition: Global variables are variables that are defined outside any function in a C program. Since they are declared outside all functions, they are accessible and can be modified by all functions in the program. Key Characteristics: - Declared outs...
Full solved answer →Write short notes on: a. Local, Global, and Static variables b. Conditional Operator [5]
--- Variables that are defined within the body of a function or a block are called local variables. Key characteristics: - Can be used only within the function or block in which they are declared - They are not accessible outside that function/block - The s...
Full solved answer →User defined functions
Define structure and nested structure. Write a program to find out whether the $n^{th}$ term of the Fibonacci series is a prime number or not. Read the value of $n$ from the user and display the result in the main function. Use separate user-defined function to (i) generate $n^{th}$ term and (ii) to check whether that number is prime or not.[3+7]
--- (a) Definitions A structure is a user-defined data type in C that allows grouping of variables of different data types under a single name. Each variable inside a structure is called a member or field. Syntax: Example: --- A nested structure is a struct...
Full solved answer →Write a program to compute the sum of first 10 even numbers using function. [5]
The first 10 even numbers are: 2, 4, 6, 8, 10, 12, 14, 16, 18, 20. A separate function evenSum() is defined to compute and return their sum, which is then called from main(). --- --- Iteration i sum (before) sum (after) count -------------------------------...
Full solved answer →Write a program using your own function to find sum of two numbers. [5]
A user-defined function is a function created by the programmer to perform a specific task. Here, we define a separate function sum() that takes two numbers as parameters, adds them, and returns the result to main(). --- --- --- Part Description ------ int ...
Full solved answer →Write a program to find product of two integers using your own function. [5]
A user-defined function is a function created by the programmer to perform a specific task. Here, we create a separate function called product() that takes two integers as parameters and returns their product. --- --- --- Part Description ------ int product...
Full solved answer →What is function? Discuss the benefits of using function. [1.5+3.5]
A function is a self-contained, named block of code that performs a specific task. It is written once and can be called (invoked) multiple times from anywhere in the program. General Syntax: Example: A C program must have at least one function: int main(), ...
Full solved answer →Function Arguments and Return Types
Why do we need a break and continue statement? Define formal argument and actual argument in function with examples. Identify and list the errors in the following code.
int main() {
int a, b, c
scanf("%d%d%d, &a, &b, &c);
sum(a, b, c);
return -1;
}
void sum(int x, int y, int z) {
int sum;
sum = a + b + c;
return sum;
}
[10]
--- The break statement is used to immediately exit from a loop (for, while, do-while) or a switch statement before its normal termination condition is reached. Without break, a loop would continue executing even when a desired condition has been met, wasti...
Full solved answer →Make Unit 7 stick
Practice CSC115 with flashcards & quizzes