0% found this document useful (0 votes)
13 views6 pages

Jcs2121 Prog in C QB U4 - New

Uploaded by

Ife Amaya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views6 pages

Jcs2121 Prog in C QB U4 - New

Uploaded by

Ife Amaya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

UNIT I

INTRODUCTION

Course Objective – COb4: To develop modular applications in C using functions and Pointers.

Course Outcome – CO1: Develop applications using Functions and Pointers.

Bloom’s Taxonomy Level - K1, K2, K3.


PART A

S.No. Question BTL


1. Define function and mention its types. K1
● A function is a block of code that performs a specific task.
● The function contains the set of programming statements enclosed by
{}. A function can be called multiple times in a program.
Types of functions:
There are 2 types of functions in C programming:
● Standard library functions (or) built-in functions
● User-defined functions
2. What is the need for functions? K1
● To reduce the complexity of large programs
● To increase the readability
● To achieve reusability
● To avoid redundancy of code
● To save Memory
3. Give examples for function declaration, function call and function K2
definition.
A function consists of the following:
i. Declaration: the function's name, return type, and parameters (if any)
ii. Definition: the body of the function (code to be executed)
iii. Call: The actual use of the function defined.
Example:
void myFunction(); // Function declaration
int main() {
myFunction(); // Function call
return 0;
}
void myFunction() { // Function definition
printf("Function was executed successfully!"); }
4. What is a function prototype? What are its types? K1
Function prototype is a function declaration statement.
Syntax : return_type function_name( parameters_list)
Example: int factorial(int);
The different types of function prototypes are:
i. Function with no return type and no arguments.
ii. Function with no return type and with arguments.
iii. Function with return type and no arguments.
iv. Function with return type and with arguments.
5. What are arguments? Compare actual argument & formal argument. K2
Arguments: Data passed from a calling function to the called function. They
act as inputs for the function. They are also called as parameters.
Actual argument: Specified in the function call statement. Used to supply the
input values to the function either by copy or reference
Formal argument: Specified in the function definition statement. It takes
either copy or address of the actual arguments
6. Differentiate call by value and call by reference. K2
Call by value Call by reference
A copy of the value is passed into An address of value is passed
the function into the function
Changes made inside the function Changes made inside the
is limited to the function only. The function validate outside of the
values of the actual parameters do function also. The values of the
not change by changing the formal actual parameters do change by
parameters. changing the formal parameters.
Actual and formal arguments are Actual and formal arguments are
created at the different memory created at the same memory
location location

7. What are the steps in writing a function in a program? K2


Function Declaration (Prototype declaration):
Every user-defined function must be declared before the main().
Function Call:
The user-defined functions can be called inside any functions like main() or
user-defined functions.
Function Definition:
The function definition block is used to define the user-defined functions with
statements.
8. Is it better to use a macro or a function? Justify. K5
● Macros are more efficient and faster than functions, because their
corresponding code is inserted directly at the point where the macro is
called. There is no overhead involved in using a macro like there is in
placing a call to function. However, macros are generally small and
cannot handle large, complex coding constructs.
● In case of large, complex constructs are handled, functions are more
suited. Additionally, macros are expanded inline, which means the
code is replicated for each occurrence of a macro.
9. State the advantages of user defined functions over pre-defined function. K1
● A user defined function allows the programmer to define the exact
function of the module as per requirement. This may not be the case
with predefined function. It may or may not serve the desired purpose
completely.
● A user defined function gives flexibility to the programmer to use
optimal programming instructions, which is not possible in predefined
function.
10. What is meant by recursive function? K1
If a function calls itself again and again, then that function is called recursive
function.
Example:
void fn( )
{
fn( ); // Recursive call - function calls itself
}
int main()
{
fn( ); // Function is called from main
}
11. Write the advantages and disadvantages of recursion. K2
Advantages:
● Recursion makes the program elegant and cleaner.
● All algorithms can be defined recursively which makes it easier to
visualize and prove.
Disadvantages:
● If the speed of the program is vital then, you should avoid using
recursion.
● Recursions use more memory and are generally slow. Instead, you can
use loop.
12. Define pointers. K1
A pointer in C language is a variable which stores the address of another
variable. This variable can be of type int, char, array, function, or any other
pointer.
Consider the following example to define a pointer which stores the address of
an integer.
int n = 10, *p;
p = &n; // Variable p of type pointer is pointing to the address of the variable n
of type integer.
13. What are the advantages of pointers? K2
● Saves memory space.
● Used for dynamic memory allocation
● Faster execution
● Used to pass array of values to a function as a single argument.
14. How can be pointers used? K2
● Define a pointer variable.
● Assign the address of a variable to a pointer.
● Finally, access the value at the address available in the pointer variable.
This is done by using the unary operator * that returns the value of the
variable located at the address specified by its operand.
15. What are & and * operators? Give examples. K2
Address of operator (&)
The & is a unary operator that returns the memory address of its operand.
Example: int *ptr, count; ptr = &count;
The memory address of the variable ‘count’ is placed into ‘ptr’.
Value at address operator (*)
The * is a unary operator. It gives the value stores at a particular address. It is
also called “indirection operator”.
Example: int a, *ptr, count; ptr = &count; a = *ptr;
If ‘ptr’ contains the memory address of the variable ‘count’, then preceding
assignment statement can place the value of ‘count’ into ‘a’.
16. Write the disadvantages of pointers. K2
● Uninitialized pointer may cause segmentation faults.
● Dynamically allocated block needs to be freed explicitly, otherwise it
leads to memory leak.
● If pointer are updated with incorrect values, it may lead to memory
corruption.
17. What is a null pointer? Give example. K1
● A null pointer is a special pointer that does not point anywhere. It does not
hold the address of any object or function.
● A pointer which is said to be null pointer when its right value is zero (0). A
null pointer can never point to valid data.
● Example: int *a, *b; a = b = 0;
18. What is pointer arithmetic & list the arithmetic operations performed on K1
pointers?
Pointer arithmetic is the set of valid arithmetic operations that can be performed
on pointers. The pointer variables store the memory address of another variable.
It doesn’t store any value.
Following arithmetic operations are possible on pointers in C language:
● Increment (++)
● Decrement (--)
● Addition (+)
● Subtraction (-)
● Comparison
19. Differentiate between pointers and arrays. K4
Pointers Arrays
It is a variable that stores the address It is the collection of elements of the
of another variable. same data type
It is declared as -: It is declared as -:
*var_name data_type var_name[size]
We can create a pointer to an array. We can create an array of pointers.
A pointer variable can store the An array can store a number of
address of only one variable at a elements the same size as the size of
time. the array variable.
Pointer allocation is done during Array allocation is done during
runtime. compile runtime.
The size of a pointer in C can be During runtime, the size of an array
resized according to user in C cannot be resized according to
requirements. user requirements.

20. How to perform increment and decrement operations in pointers? K2


Incrementing Pointer
If we increment a pointer by 1, the pointer will start pointing to the immediate
next location.
We can traverse an array by using the increment operation on a pointer which
will keep pointing to every element of the array, perform some operation on
that, and update itself in a loop.
new_address= current_address + i * size_of(data type)
Decrementing pointer
Like increment, we can decrement a pointer variable. If we decrement a pointer,
it will start pointing to the previous location. The formula of decrementing the
pointer is given below:
new_address= current_address - i * size_of(data type)
21. Addition of two pointers impossible – justify. K6
Addition of two pointers operations cannot be performed on 2 pointers in C.
Addition of two pointers will add two addresses and might give an address
which might be so large that it is outside the range of 32 bit or 64 bit system
of addresses in contiguous memory locations. So, it is not a valid operation,
whereas you can add numeric values to the pointer to make it point to an
address block which is that numeric value*size of (data type) number of
blocks away, so adding a number to a pointer is valid, while addition of
pointers is not.

PART B
S.No. Question BT
L
1. What is function in C? Explain the steps in writing a function in C program K2
with an example program.
2. a) Classify the function prototypes with example C program for each. K2
(8 marks)
b) Illustrate the difference between user defined and built-in functions. K3
(8 marks)
3. Explain the concept of pass by value and pass by reference. Also write a C K3
program to swap the content of two values using pass by reference.
4. Explain recursive function with syntax and an example program. (8 marks) K2
Write a C program to find the factorial of a number using recursion. (8 marks) K3
5. a) Write a C program to implement tower of Hanoi puzzle. (8 marks) K3
b) Write a C program to reverse a string using recursion. (8 marks) K3
6. a) Explain about pointers and write the use of pointers with suitable example. K2
(8 marks)
b) Create a C program to find the sum of two numbers using pointers. K5
(8 marks)
7. Explain in detail about pointer arithmetic with example. K2
8. Explain and illustrate how an array can be accessed using pointers. (8 marks) K2
Design a C program to find the sum of array elements using pointers. K5
(8 marks)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy