Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Class notes

Functions and Files in C

Rating
-
Sold
-
Pages
26
Uploaded on
16-01-2025
Written in
2022/2023

Functions and File Handling in C provides a detailed understanding of modular programming and data storage. It begins with an introduction to functions, explaining their importance in code reusability and organization. It covers function declaration, definition, and calling, along with return types, parameters, recursion, and scope of variables. The document then transitions to file handling, explaining the need for persistent data storage and the different file operations in C. It introduces file pointers and functions like fopen(), fclose(), fprintf(), fscanf(), fgets(), and fputs() for reading and writing data. Concepts like binary vs. text files, file modes, error handling, and file manipulation techniques are covered with practical examples. The document also highlights best practices for efficient file management, common pitfalls, and debugging techniques, making it a complete guide for understanding functions and file handling in C.

Show more Read less
Institution
Course

Content preview

Function: A subprogram (or) subroutine that is used to perform a specified task is called a
function. A „C‟ program is made of one or more functions, and one and only one of which must
be main( ) function. The execution of the program always starts with main( )
Advantages of functions:

1. It facilitates top-down modular programming approach as shown in the figure. In top
down modular approach, a program is divided into a main module and its related
modules. Each module can again be divided into sub modules.

Main program




Function 1 Function 2 Function3

2. The length of the source program can be reduced by using functions at appropriate
places.
3. It is easy to locate and isolate a faulty function during debugging.
4. A function may be used by many other programs.


Types of functions:
In „C‟ language, functions are classified into two types. They are
a) Library functions (or) Built-in functions
b) User defined functions
a) Library Functions: Library functions are predefined functions and supplied along with the
compiler and these can be used in any „C‟ program. They are also known as Built-in functions
and all these functions are available in „C‟ library. Ex: scanf( ), printf( ), gets( ) etc.
b) User defined Functions: The functions that are defined by the users are called as user-
defined functions. Ex: main( ) is an example of user defined function.
Elements of User defined Functions (or) function components: Every function has the
following elements associated with it.
i. Function definition (or) Function implementation
ii. Function call
iii. Function declaration (or) Function prototype
i. Function definition: The function definition is an independent program module that

, iswritten to implement the specific task.
A function definition has two parts namely
1. Function body
2. Function header
The general form of a function definition to implement two parts is given below.
return_type function_name (parameter list)
{
Local variable
declaration;Executable
statement1; Executable
statement2;
-------------------------
-------------------------
return statement;
}

* Function header: The function header consists of three parts
a) return_type
b) function_name
c) parameter(formal) list


return type: A function may or may not send back any value to the calling function. A function
type specifies the data type of value that the function is expected to return to the program calling
the function. If a function is not returning anything, then we need to specify the function type as
void.
Function-name: The function name is any valid C identifier and the name should be appropriate
to the task performed by the function.
Parameter list: The parameter list contains declaration of variables separated by commas and
surrounded by parenthesis. The parameter list declares the variables that will receive the data
sent by the calling program. They serve as input data to the function to carry out the specified
task.
* Function body: The function body contains the declarations and statements necessary for
performing the required task. The body enclosed in braces, contains three parts in the order
givenbelow.
a) Local declarations that specify the variables needed by the function
b) Function statements that perform the task of the function

, c) A return statement that returns the value evaluated by the function.
If a function does not return any value, we can omit the return statement. But the
functiontype should be specified as void.
Ex: int sum(int a, int b) /*function header*/
{
int rst;
rst=a+b; /*function
body*/return(rst);
}

ii. Function call: A function can be called by writing the function name followed by a
listof actual parameters, if any, enclosed in the parenthesis and terminated by a semicolon.
Ex: main( )
{
int z,x,y;
scanf(“%d%d”,&x,&y)
;
z=maximum(x,y); /*function call*/
printf(“z=%d”,z);
}

In the above program, the statement z=maximum(x,y); invokes the function maximum( )
with two integer parameters. Executing the function call statement causes the control to be
transferred to the first statement in the function body. When a return statement is executed in
the function body, the control is transferred back to the calling function. In the absence of a
return statement, the closing brace acts as a void return.
iii. Function declaration: All functions in a „C‟ program must be declared, before they are
invoked. A function declaration consists of four parts.
a) return_type
b) function_name
c) parameter list
d) terminating semicolon
The general form of declaring the function is return_type function_name(parameter list);
Ex: int maximum(int m, int n);

Return statement: A function may or may not send back any value to the calling function. It
can be done through the return statement. When a return statement is executed, the control is
transferred to the calling function.

Written for

Institution
Course

Document information

Uploaded on
January 16, 2025
Number of pages
26
Written in
2022/2023
Type
Class notes
Professor(s)
Vijaya anandaratnam
Contains
All classes

Subjects

$8.49
Get access to the full document:

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Get to know the seller
Seller avatar
srilathalakshmisetty04

Also available in package deal

Get to know the seller

Seller avatar
srilathalakshmisetty04 Vasireddy Venkatadri Institute of Technology
Follow You need to be logged in order to follow users or courses
Sold
-
Member since
1 year
Number of followers
0
Documents
5
Last sold
-

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions