0% found this document useful (0 votes)
7 views3 pages

CH 16

Functions in C are essential for breaking down large programs into manageable subprograms, making them easier to understand, test, and debug. There are two types of functions: library functions, which are predefined in the C standard library, and user-defined functions, which are created by the programmer. Functions enhance modularity, reduce redundancy, and allow for code reuse, while also requiring proper definition and calling syntax.

Uploaded by

leuvakartikey
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)
7 views3 pages

CH 16

Functions in C are essential for breaking down large programs into manageable subprograms, making them easier to understand, test, and debug. There are two types of functions: library functions, which are predefined in the C standard library, and user-defined functions, which are created by the programmer. Functions enhance modularity, reduce redundancy, and allow for code reuse, while also requiring proper definition and calling syntax.

Uploaded by

leuvakartikey
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/ 3

Chapter 16 ( FUNCTION )

Why FUNCTION is needed:- If we write our program using only main() function then it may
become large. Testing and debugging of such program may be become difficult for programmer. If program is
divided into small subprograms (functions) then it is much easier to understand, test and debug it.)

What is FUNCTION:-
 Function is a group of statements that performs a specific task . The function is also knows as
method, sub-routine or procedure.
 We are familiar with the use of main(), printf() and scanf(). They are examples of C functions. Every
executable C program has at least one main() function.
 The use of function in program makes it modular. Modularity means partitioning a complex problem
into small sub-problems which are easy to understand and maintain.
◦ Once a problem is divided into small sub-problem, we can write a function for each sub-problem.
The problem is then solved by using all functions together in a program.
 There are two categories of functions in C.
◦ Library functions or System defined functions
◦ User defined functions

Library Functions:
 The C language standard library provides many built-in functions. They are also known as system
defined functions.
◦ For example printf(), scanf(), sqrt() and cos() are examples of library functions.
 Library functions are not required to be written by normal user. These functions are available in
compiled form in C library.
 Library functions are placed in various header files.
◦ For example, sqrt() and cos() are related to mathematics hence they are placed in <math.h> header
file.
 To use any library function, we have to include its corresponding header file in our program

User Defined Functions:


 The functions developed by user are known as user defined functions.
◦ The main() is special type of user defined function in C.
◦ The program execution starts from main() function.

Advantages of Functions:-
Followings are the advantages of using functions in our program:
 Dividing program into functions makes understanding and maintenance of the program easier.
 Functions written once can be called many times in our program. This reduces the actual length of
program and thus saves memory.
 Functions help us to avoid redundant programming of similar tasks.
 Fnction written for one program may be used in other program.
 Same function can be used with different values of arguments.

Function Definition:-
 If we want to use user defined function in our program then we have to inform this to compiler .
◦ We can inform the compiler by writing function definition.
▪ A function cannot be used without its definition.
In C language. function can be defined using following syntax.
return_data_type function_name (arguments)
{
function statements,
}
Here return_data_type specifies the type of information return by the function.
 If the return value from the function is integer value then return_data_type is int.
 If function does not return any value then the return_data_type is void.
The function_name specifies the name of user defined function.
 The rules for giving name to functions are same as rules of naming variable.
◦ We should give meaningful function name.
▪ For example add () for performing addition, avg() for performing average, etc.
 The arguments show the input values provided to the function along with their data types.
◦ For more than one argument, all arguments are separated by commas in the function
definition.
 Statements between opening and closing curly brace are known as function body.
◦ Based on requirements in a program, function body may contains local variable
declarations and return statements.
Note:-
 That there is no need to define library function as they are already defined.
 All user defined functions needs to be defined as function prototype before being used in a program.
 Function prototype means declaration of a function before their use in a main () function.

Function Call:-
 Every C program starts with function main().
 From main() functions we are calling other library or user defined functions.
 To call a function, use function name with required number of parameters.

The Return Statement:-


 User defined functions may or may not return any value to the calling function.
 To return any value to the calling function, we may use return statement inside function body.
◦ The format of return statement is - return ; ог return (expression);
 The first form of return does not return any value. It returns only control back to the calling function.
 When return type of function is void then there is no need to use return statement at the end of function
body.

Scope of Variables:-
 By scope of variable we mean the part of program where the variable is accesible. In C scope of
variables are of two types:
◦ Global variables
◦ Local variables

Components of the C functions:-


Function Prototype: -
 The function prototype is needed when the function is defined after the main() in a program.
 The function prototypes are written for each user defined function in beginning of the program,
 When we call any function in our program, compiler searches for a corresponding function definition.
◦ This is done to verify whether the function call is correct or not.
◦ If function call matches the header of the function in terms of number of arguments with their data
types, it is valid otherwise compiler issues an error.

Function Arguments or Parameters:-


 The basic idea behind using arguments or parameters in functions is to pass data between the calling
and the called function.
 When the calling function sends the data to the called function it is called as argument or parameter
passing.
 The arguments used at the time of function definition are known as formal arguments.
 The call to such function needs same number of arguments to be passed to it at the time of function
call. These arguments are known as actual arguments.
◦ The formal and actual arguments should match in number, type and order.
◦ If actual arguments are more than formal arguments, the additional actual arguments are simply
discarded.
◦ When actual arguments are less than formal arguments, the unmatched formal arguments are
initialized to garbage value.
◦ Also any mismatch in data types of arguments will result in garbage initialization.

Types/Categories of Functions:-
 Depending on presence of arguments and retum value in a function, they fall under following categories
◦ Function with No Arguments and No Return Values:
▪ When function has no arguments and no return value, it falls under this category. The called
function does not receive any data value from calling function. Similarly due to no return value,
the calling function does not receive any data from called function. In short there is no data
transfer between calling and called functions.
◦ Function with Arguments and No Return Values
◦ Function with Arguments and Return Values
▪ i.e. two way communication between the calling function and the called function.

!! Vision Computer Education !!


Query to call:
[email protected]
Whatsup : +91-9409209969

You might also like