C PROGRAMMING(VSEC102)
Assignment No.-3
Q.1 What is a function, function declaration, function definition and function call
Function
A function in C is a block of code that performs a specific task. Functions are used to break down large
programs into smaller, reusable modules, making code more manageable, modular, and easier to debug.
Function declaration
It specifies the function's name, return type, and parameters but does not contain the function body. It tells the
compiler that the function exists.
Syntax:
return_type name_of_the_function (parameter_1, parameter_2);
The parameter name is not mandatory while declaring functions. We can also declare the function without
using the name of the data variables.
Example
int sum(int a, int b); // Function declaration with parameter names
int sum(int , int); // Function declaration without parameter names
Function definition
It includes the actual code (body) of the function that performs the specified task.
A C function is generally defined and declared in a single step because the function definition always starts
with the function declaration so we do not need to declare it explicitly.
The below example serves as both a function definition and a declaration.
return_type function_name (para1_type para1_name, para2_type para2_name)
{
// body of the function
}
Function Call
A function call is a statement that instructs the compiler to execute the function. We use the function name
and parameters in the function call.
Example
Int add=sum(10,30);
In the above example, the first sum function is called and 10,30 are passed to the sum function. After the
function call sum of a and b is returned and control is also returned back to the main function of the program.
Q. 2 Explain the difference between call by value and call by reference.
Parameter Call By Value Call By Reference
The function is invoked by passing The function is invoked by passing the
Naming the parameter’s value in Call by parameter’s reference (i.e., the location of
Convention Value, which is why it has this variables) in Call by Reference, which is
name. why it has this name.
In Call by Value, any changes
In Call by Reference, changes to the
made to the function’s argument do
Impact of function’s argument are reflected in the
not affect the passed parameter as
Changes passed parameter as both refer to the same
the value of the parameter is
location.
copied.
Type of Call by Value passes a copy of the Call by Reference passes the variable
Passing variable . itself .
In Call by Value, the actual In Call by Reference, the actual
Memory arguments and passed parameters arguments and passed parameters of a
Location of a function refer to different function refer to the same memory
memory locations. location.
Value In Call by Value, the original value In Call by Reference, the original value is
Modification is not modified. modified.
In Call by Value, the values of the
Method of In Call by Reference, the address of the
variables are passed using a simple
Passing variables is stored using pointer variables.
technique.
Q. 3 What are storage classes in C? List them.
Storage classes define the scope, lifetime, and visibility of variables in a C program. The four storage classes
are:
auto: The default storage class for local variables.
register: The register storage class stores a variable in a CPU register for faster access, typically useful for
frequently used variables.
static: A static variable retains its value across function calls and is only accessible within the file or function
where it's declared.
extern: An extern variable is defined outside any function, making it accessible across multiple files within
the same program.
Class Name of Place of Scope Default Lifetime
Class Storage Value
auto Automatic RAM Local Garbage Within a function
Value
extern External RAM Globa Zero Till the main program ends. One can
l declare it anywhere in a program.
static Static RAM Local Zero Till the main program ends. It retains the
available value between various function
calls.
register Register Register Local Garbage Within the function
Value
Q.4 What is a recursive function? Write a C program to calculate the factorial of a given number using
recursion.
A recursive function is a function that calls itself to solve a smaller instance of the problem until a base
condition is met.
#include<stdio.h>
int factorial(int n){
if(n==0)
return 1;
else
return n*factorial(n-1);
}
int main(){
int num;
printf("Enter a number:");
scanf("%d",&num);
printf("Factorial of %d is %d",num,factorial(num));
return 0;
}
Q.5 Write a function to swap two numbers using call by reference.
#include<stdio.h>
void swap(int *a,int *b)
{
int temp=*a;
*a=*b;
*b=temp;
}
int main()
{
int x=5,y=10;
printf("Before swap: x=%d, y=%d\n",x,y);
swap(&x,&y);
printf("After swap: x=%d, y=%d\n",x,y);
return 0;
}