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

Experiment No 1b

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)
15 views6 pages

Experiment No 1b

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

Sant Dnyaneshwar Shikshan Sanstha's

Annasaheb Dange College of Engineering and


Technology (ADCET), Ashta

Department of CSE(IOT and CSBT)


Quality Laboratory Manual
AY 2024-25
Data Structure Lab
1ICPC202

Course Instructor -
Mrs. Prachi S. Pathak
ASSISTANT PROFESSOR
QUALITY LABORATORY MANUAL
Prepared by – Mrs. Prachi S. Pathak
Data Structure Laboratory [1ICPC202]
Second Year – AY 2024-25[Odd]
Semester]

Experiment No. 1b

Title of Experiment: To study and implement functions.

Aim of Experiment: To implement and understand the working of functions.

System Requirements:
Programming Languages: 1. C 2. C++ | Programming Editor: 1. Dev C++ 2. GCC/G++
Hardware: 1. A computer that can execute C/C++ programs. | Operating system: 1. Windows / UNIX
Operating System.

Experiment Outcomes –
1. Understand the principles and applications of functions.
2. To implement functions with argument passing.

Theory –
A function in C is a set of statements that when called perform some specific task. It is
the basic building block of a C program that provides modularity and code reusability.
The programming statements of a function are enclosed within { } braces, having certain
meanings and performing certain operations. They are also called subroutines or
procedures in other languages.
In this article, we will learn about functions, function definition. declaration, arguments
and parameters, return values, and many more.

Syntax of Functions in C
The syntax of function can be divided into 3 aspects:
1. Function Declaration
2. Function Definition
3. Function Calls

Function Declarations
In a function declaration, we must provide the function name, its return type, and the
number and type of its parameters. A function declaration tells the compiler that there
isa function with the given name defined somewhere else in the program.

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.

Page 2 of 5
QUALITY LABORATORY MANUAL
Prepared by – Mrs. Prachi S. Pathak
Data Structure Laboratory [1ICPC202]
Second Year – AY 2024-25[Odd]
Semester]

Function Declaration Note: A function in C must always be declared globally before calling it.

Function Definition
The function definition consists of actual statements which are executed when the function is
called (i.e. when the program control comes to the function). 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. Thebelow 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 Definition in C

Page 3 of 5
QUALITY LABORATORY MANUAL
Prepared by – Mrs. Prachi S. Pathak
Data Structure Laboratory [1ICPC202]
Second Year – AY 2024-25[Odd]
Semester]
Function Call
A function call is a statement that instructs the compiler to execute the function. Weuse the
function name and parameters in the function call. In the below 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 backto the main function of the program.

Working of function in C

Note: Function call is necessary to bring the program control to the function definition. If
not called, the function statements will not be executed.

Problem Statements:
1. Implement a program for an Inventory to store and display following data for daily purchase:
a. Product ID
b. Product Name
c. Purchase Date
d. Quantity
e. Product Price
f. Bill
Take user input for all, except ‘Bill’, Implement a function for calculation bill from user input values
of Quantity and price. Implement another function to calculate and display total bill amount for ‘n’
number of products.

Conclusion:
Hence, the experiment provides students with hands-on experience in implementing
functions.

Observations:
1. Modularity:
Functions allow for the modularization of code, enabling the separation of concerns and making the
code easier to read, maintain, and debug.
Each function performs a specific task, and larger problems can be solved by combining multiple
functions.

Page 4 of 5
QUALITY LABORATORY MANUAL
Prepared by – Mrs. Prachi S. Pathak
Data Structure Laboratory [1ICPC202]
Second Year – AY 2024-25[Odd]
Semester]
2. Reusability:
Once defined, a function can be reused multiple times throughout a program, reducingcode
duplication.
This also makes updates and changes easier, as the function only needs to be modified in one place.
3. Parameter Passing:
Functions can accept input parameters and return output values, allowing for dynamicand flexible
operations.
Understanding the difference between pass-by-value and pass-by-reference is crucial indetermining
how data is manipulated within functions.
4. Scope and Lifetime:
Variables declared within a function have local scope, meaning they are only accessible within that
function.
Understanding the scope and lifetime of variables helps prevent errors and conflicts in larger
programs.

References –
a. Textbook –
1. "The C Programming Language" by Brian W. Kernighan and Dennis M. Ritchie
2. "Data Structures and Algorithm Analysis in C" by Mark Allen Weiss
3. "Let Us C" by Yashavant Kanetkar

b. Online references –
1. GeeksforGeeks - Functions in C
https://www.geeksforgeeks.org/functions-in-c/

2. Programiz - Functions (Python)


https://www.programiz.com/python-programming/function

3. W3Schools - Python Functions


https://www.w3schools.com/python/python_functions.asp

4. TutorialsPoint - Functions in C
https://www.tutorialspoint.com/cprogramming/c_functions.html

Expected Oral Questions –


1. What is a function, and why is it used in programming?
2. Explain the difference between a function definition and a function call.
3. What are the different types of functions (e.g., user-defined, built-in) in the language you are using?
4. How does parameter pass work in functions? What is the difference between passing by
value andpassing by reference?
5. What is recursion, and can you give an example of a problem that can be solved using recursion?
6. How does the scope of variables work within functions?
7. What is the significance of the return type in a function?

Page 5 of 5
QUALITY LABORATORY MANUAL
Prepared by – Mrs. Prachi S. Pathak
Data Structure Laboratory [1ICPC202]
Second Year – AY 2024-25[Odd]
Semester]

FAQ’s in Interview –
1. Q: What is a function in programming?
A: A function is a block of code designed to perform a specific task, and it can
be executed bycalling its name. Functions help in organizing code, improving
readability, and reusability.
2. Q: How do you pass arguments to a function?
A: Arguments are passed to a function through its parameters, which are defined
in the functionsignature. They can be passed by value or by reference,
depending on the programming language.
3. Q: What is recursion?
A: Recursion is a programming technique where a function calls itself to solve
smaller instancesof the same problem until a base condition is met.
4. Q: What is the difference between local and global variables?
A: Local variables are declared within a function and are only accessible
within that function,whereas global variables are declared outside any
function and are accessible throughout the program.
5. Q: Can you explain the concept of a pure function?
A: A pure function is a function that, given the same input, always produces the
same output andhas no side effects (i.e., it does not modify any external state).
u can have an array of structures, where each element of the array is a structure.

Page 6 of 5

You might also like