MODULE 3
1. Define Function. Explain the different components of the function with syntax[June/July 2023]
OR
Define Function. With an example discuss Function Declaration, Function definition, and
Function call[Nov/Dec 2023]
OR
Define Function. Explain the categories of User defined Functions[Jan/Feb 2023]
“The set of instructions that performs some specific, well-defined task is called as a
Function.”
or
“Function is a small program or program segment that carryout some specific well-defined
tasks”.
Advantages of Functions
i. Reduces the Complexity.
When a program contains more number of instructions (complex program), then such large
program can be divided into number of sub programs called as functions.
ii. Improves the Readability.
iii. Easy to debug the errors.
iv. Reusability: The set of instructions specifying the task performed by the function is written just once
but it can be used any number of times.
v. Easy to do the modifications.
Elements or Components of User-Defined Functions
The three elements of user-defined functions are shown below:
i. Function Prototype/Declaration
ii. Function Definition
iii. Function Call
1.Function Prototype/Declaration
As we normally declare the variables before they are used, the functions also should be
declared before they are used.
The process of declaring the functions before they are used (or called) in the program is
called Function Prototype.
The function prototype is also called as Function declaration.
It is same as the Function Header but terminated with semicolon.
It does not contain the body of the function.
Syntax
return_type function_name(parameter list);
Where,
return_type: This is the data type of the value that the function is expected to return (int, float,
double, char).
function_name: It is the name of the function. It can be any valid Identifier.
parameter list: The parameters are the list of variables enclosed within parenthesis. Variables are
separated by comma.
Ex: int add(int a, int b);
The function prototype contains the following information in a single line terminated by semicolon:
The type of the value returned by the function.
The name of the function.
The number of parameters passed to that function.
The type of each parameter.
2.Function Definition
The program module that is written to achieve a specific task is called function definition.
Each function definition consists of two parts:
Function Header
Function Body
Syntax: return_type function_name (parameter list)
{
declaration part;
executable part;
return statement;
}
return_type:
This is the data type of the value that the function is expected to return (int, float, double,
char).
If the function is not returning any value, then we need to specify the return type as ‘void’.
The default return value of any function is integer.
function_name:
It is the name of the function. It can be any valid Identifier.
Parameters:
The parameters are the list of variables enclosed within parenthesis. Variables are separated by
comma.
Data type of parameters
Ex: int add ( int a, int b);
Parameter name
return type Function name
Function Body
Declaration part: All the variables used in the function body should be declared in this part.
Executable par: This part contains the statements or instructions that perform the specified
activity.
return statement: It is a keyword used to return the control to the calling function (main)
with/without a value.
Syntax:
1.
return; // Returns control without sending any value to main program
2. return expression; // Returns control by sending value to main program
Ex: int add(int a,int b)
{
int sum; //variable declaration
sum=a+b; //executable statement
return sum; //return statement
}
3.Function Call
Once the function is defined, it has to be called so as to achieve the task. This method of calling a
function to achieve a specified task is called Function Call.
The function can be called by writing the name of the function and passing the appropriate
number of arguments.
The number of arguments in the function call and number of parameters in the function
definition must match.
Also the order of arguments in the function call and parameters in the function definition must
match.
Example: add(m,n);
2. Define function? Develop a C program to perform the addition of two numbers using function
#include<stdio.h>
int add(int a,int b); /*Function Prototype*/
int add(int a,int b) 2
{
int sum;
sum=a+b; 3
return sum; 4
}
void main()
{
int result;
result=add(10,20); 1
printf(“sum=%d\n”,result); 5
getch();
}
1. The function add() is called with two arguments 10 and 20.
2. The control is transferred to the function add() and the values 10 and 20 are received using variables a
and b.
3. The result is computed by adding 10 and 20.
4. The result is returned to the main() and will be copied into the variable result.
5. The result is displayed on the screen.
The ‘main()’ is always the “Calling Function”.
The ‘add()’ function is called “Called Function”.
3. What is Recursion? Write a ‘C’ program using recursion to find the factorial of an interger
number.[ Jan/Feb 2023]
OR
3. Define Recursion. Mention the base properties of recursive function.[Nov/Dec 2023]
“The process in which a function calls itself again and again is called as Recursion”.
A function which calls itself again and again is called as Recursive function.
While using recursion, user need to be careful to define exit condition from function; otherwise it
will go in infinite loop.
Recursive functions are very useful to solve many mathematical problems like to calculate factorial
of a number, generating Fibonacci series, etc.
Recursive solution has two major cases, they are:
1. Base case: In this problem is simpleto be solved directly without making any further calls to the
same function.
2. Recursive case: In this problem is first divided into sub parts. Second the function calls with
subparts of problem obtained in the first step. Third the result is obtained by combining the
solution of simpler subparts.
Syntax:
Void main() int recursion ()
{ {
recursion ( ); recursion ( );
} }
Example: Program to calculate factorial of a given number.
#include<stdio.h>
int factorial(int n)
{
if(n==1)
return 1;
else
return (n*fact(n-1)); }
void main()
Output:
{ int n,fact;
Enter a number=5
printf(“Enter a number=”);
Factorial of given number =120
scanf(“%d”,&n);
fact=factorial(n);
printf(“\nFactorial of given number=%d”,fact);
}
4. Explain different storage classes in C with the suitable Example [Jan/Feb 2023]
A storage class represents the visibility and a location of a variable. It tells from what part of code we can
access a variable. A storage class in C is used to describe the following things:
The variable scope.
The location where the variable will be stored.
The initialized value of a variable.
A lifetime of a variable.
Types of Storage Classes in C
There are total four types of standard storage classes. The table below represents the storage classes in C.
Storage class Purpose
auto It is a default storage class.
extern It is a global variable.
It is a local variable which is capable of returning a
static value even when control is transferred to the function
call.
register It is a variable which is stored inside a Regist
1. Auto Storage Class in C
The variables defined using auto storage class are called as local variables. Auto stands for
automatic storage class.
A variable is in auto storage class by default if it is not explicitly specified.The scope of an
auto variable is limited with the particular block only.
Once the control goes out of the block, the access is destroyed. This means only the block in
which the auto variable is declared can access it.
A keyword auto is used to define an auto storage class. By default, an auto variable contains a
garbage value.
Example: auto int age;
2.Extern Storage Class in C
Extern stands for external storage class.
Extern storage class is used when we have global functions or variables which are shared between
two or more files.
Keyword extern is used to declaring a global variable or function in another file to provide the
reference of variable or function which have been already defined in the original file.
The variables defined using an extern keyword are called as global variables. These variables are
accessible throughout the program.
The extern variable cannot be initialized it has already been defined in the original file.
Example:extern void display();
3.Static Storage Class in C
The static variables are used within function/ file as local static variables. They can also be used as
a global variable
Static local variable is a local variable that retains and stores its value between function calls or
block and remains visible only to the function or block in which it is defined.
Static global variables are global variables visible only to the file in which it is declared.
Example: static int count = 10;
4.Register Storage Class in C
The register storage class when you want to store local variables within functions or blocks in CPU
registers instead of RAM to have quick access to these variables.
The keyword register is used to declare a register storage class. The variables declared using register
storage class has lifespan throughout the program.
Example: register int age;
It is similar to the auto storage class. The variable is limited to the particular block.
The only difference is that the variables declared using register storage class are stored inside CPU
registers instead of a memory. Register has faster access than that of the main memory.
The variables declared using register storage class has no default value. These variables are often
declared at the beginning of a program.
5.List and explain the different types of Function calls with Example[June/July 2023]
OR
Distinguish between Call by Value and Call by Reference using Suitable Example.[Jan/Feb 2023]
OR
Discuss the various ways of passing parameters to the functions.
There are mainly two parameter passing mechanisms in ‘C’ functions. They are:
i. Call by Value or Pass by Value
ii. Call by Reference or Pass by Reference
i. Call by Value or Pass by Value
Calling (Invoking) a function by passing values or variables to the function is called as call by
value.
The values of actual parameters are copied into formal parameters.
Formal parameters contain only the copy of actual parameters. So, even if the values of the
formal parameters changes in the called function, the values of the actual parameters are not
changed.
ii. Call by Reference or Pass by Reference
Call by address is done indirectly by passing the address of the variables in the function call and
changing the value of the variable through its address.
In the called function, the formal parameters should be declared as pointers with the same type as
the actual parameters.
The addresses of actual parameters are copied into formal parameters. Using these addresses the
values of the actual parameters can be changed.
In call by reference if any change in formal parameters imply then there is a change in
actual parameters.
Example Program: Write a C program to add two numbers using call by reference.
#include<stdio.h>
void add (int *a, int *b) // Formal parameters *a = 10, *b =20
{
int sum;
sum = *a + *b; Output:
return sum; sum=30
}
void main()
{
int m=10,n=20, res;
res = add(&m, &n); // Actual parameter m=10, n =20
printf(“result = %d \n”, res);
}
Example Program: Write a C program to swap two numbers using call by reference.
#include<stdio.h>
void swap(int *a, int *b)
{
int temp;
temp=*a;
*a=*b; Output:
*b=temp; m = 20
} n = 10
void main()
{
int m=10,n=20;
swap(&m,&n);
printf(“m=%d\n n=%d\n”,m,n);
}
6. Define Array. Explain how an array can be declared and initialized with Example[June /July
2023]
OR
Array in C can be defined as a method of clubbing multiple entities of similar type into a larger
group. These entities or elements can be of int, float, char, or double data type or can be of user-defined
data types too like structures.
Ex: int A[5] ; // Array of 5 Integers
A[0] 10
A[1] 20
A[2] 30
A[3] 40
A[4] 50
The Elements in the Array A can be accessed using the common name A, but with
different index.
The Element ‘10’ is called 0th Element and it can be accessed using the Subscript 0 (called
Index‘0’) along with name of the array ‘A’.
1. Declaration of Single dimensional arrays
As we declare the variables before they are used in a program, an array must also be declared
and defined before it is used using following syntax.
Syntax
data_type array_name[size];
Where,
data_type: data type can be int, float, char etc.
array_name: name of the array which is a valid C variable.
size: it is the number of elements in the array.
Complete declaration ends with Semicolon.
Ex: int Marks[5];
Declares Marks as an array consisting of 5 elements of integer data type.
Ex: int Marks[5];
1000 35 Marks[0]
data type array_name 1002 45 Marks[1]
1004 65 Marks[2]
1006 55 Marks[3]
char name[5]; 1008 75 Marks[4]
Array name
Memory location
5 memory locations are reserved. sizeof(int) is 2 bytes, 2*5=10 bytes are reserved.
5 memory locations are reserved. sizeof(char) is 1 bytes 1*5=5 bytes are reserved.
2. Initialization of Single dimensional arrays
Once an array is declared it must be initialized with some values using Initialization.
“Process of assigning the values to the individual elements of an array is called asInitialization of
an array”.
Syntax: data_type array_name[size]={v1,v2, ----,vn};
data_type: it can be int, float, char etc.
array_name: it is the name of the array.
size: it is the number of elements in the
array.
v1,v2, ---- ,vn are the values and should be enclosed within ‘{‘ and ‘}’ separated by commas.
Ex: 1. int a[5] = {10, 20, 30, 40, 50};
The compiler allocates 5 memory locations and these locations are initialized with the integer
values in the order specified.
a[0] 10
a[1] 20
a[2] 30
a[3]
40
a[4]
50
2. int a[5] = {10, 20};
10 20 0 0 0
a[0] a[1] a[2] a[3] a[4]
When the numbers of initial values are lesser than declared array size then those many elements
will be initialized and other memory locations are automatically initialized to 0’s, in case of
numeric arrays.
It is ‘partial array initialization’.
7. Explain with example how an array can be passed as parameter to the Function.[June/July 2023]
OR
Explain how value of individual array can be passed into a function with example.
Like variables of other data types, we can also pass an array to a function.
In some situations, you may want to pass individual elements of the array; while in other
situations, you may want to pass the entire array.
1. Passing Individual elements
• The individual elements of an array can be passed to a function by passing either their data
values or addresses.
• Individual elements can be passed in the same manner as we pass variables of any other data
type.
• The condition is just that the data type of the array element must match with the type of the
function parameter.
• In the above example, only one element of the array is passed to the called function.
• This is done by using the index expression. Here, arr[3] evaluates to a single integer value.
• The called function does not know whether a normal integer variable is passed to it or an array
value is passed.
2. Passing Addresses
• Like ordinary variables, we can pass the address of an individual array element by preceding the
indexed array element with the address operator.
• Therefore, to pass the address of the fourth element of the array to the called function, and write
&arr[3],in the called function, the value of the array element must be accessed using the indirection
(*) operator
3. Passing the Entire Array
• In C the array name refers to the first byte of the array in the memory.
• The address of the remaining elements in the array can be calculated using the array name and the
index value of the element.
• Therefore, when we need to pass an entire array to a function, we can simply pass the name of the
array.
• A function that accepts an array can declare the formal parameter in either of the two following
ways. func(int arr[]); or func(int *arr);
• When we pass the name of an array to a function, the address of the zeroth element of the array is
copied to the local pointer variable in the function.
• When a formal parameter is declared in a function header as an array, it is interpreted as a pointer to
a variable and not as an array.
• With this pointer variable you can access all the elements of the array by using the expression:
array_name + index.
• So for a function that accepts an array as parameter, the declaration should be as follows. func(int
arr[], int n); or func(int *arr, int n);
• It is not necessary to pass the whole array to a function,pass a part of the array known as a sub-array.
• A pointer to a sub-array is also an array pointer.
• For example, if we want to send the array starting from the third element then we can pass the
address of the third element and the size of the sub-array, i.e., if there are 10 elements in the array,
and we want to pass the array starting from the third element, then only eight elements would be
part of the sub-array.
• So the function call can be written as func(&arr[2], 8);
8. Write a ‘C’ Program using recursive function to print ‘n’ Fibonacci numbers.[Jan/Feb 2023]
9.Write a C program to sort the given set of elements using Bubble sort technique.[June/July 2023]
10.Write a C program to search for a key element in an array using Linear Search method[Jan/Feb
2023]
10.Write a C program to search for a key element in the given sorted array using Binary Search
method[Nov/Dec 2023]