Programming in C
Functions
Function
A
function is a block of code that performs a specific task. It is a
self-contained unit of a program that can be called whenever required.
Advantages of
Functions
- Code Reusability: A function can be called
multiple times without rewriting the code.
- Modularity: A large program can be divided
into smaller modules.
- Easy Debugging: Errors can be identified and
corrected easily.
- Code Readability: Programs become easier to
understand.
- Code Maintenance: Changes can be made in one
function without affecting the entire program.
- Reduces Code Duplication: The same logic need
not be written repeatedly.
Types of Functions in C
C supports two major categories of
functions.
1. Library
Functions (Predefined functions) - Library functions are
predefined functions provided by the C standard library. We can use these
functions by including the appropriate header files. Example
a. printf(), scanf() from stdio.h
b. rand() from stdlib.h
2. User-Defined
Functions (Created by programmers) - User-defined functions are
functions created by the programmer to perform specific tasks. Example
a. fnAdd()
b. fnDisplay()
c. fnFactorial()
Components of
a Function
A user-defined
function generally involves three important components:
- Function Declaration (Function Prototype)
- Function Definition
- Function Call
1. Function
Declaration
A function
declaration informs the compiler about the function name, return type and
parameters before the function is used.
Syntax:
return_type
function_name(parameter_list);
2. Function
Definition
A function
definition contains the actual statements that perform the required operation.
return_type
function_name(parameters)
{
// Statements
return value;
}
3. Function
Call
A function call
transfers the program's control to the specified function.
Classification
of User-Defined Functions
Based on the
presence or absence of arguments and return values, user-defined functions are
classified into four types.
Four Types of
Functions
Type 1: No
Arguments and No Return Value
- The function does not receive any values from the
calling function.
- It does not return any value.
- It performs its task independently.
void function_name(void);
Type 2: With Arguments
and No Return Value
- The function receives values from the calling
function.
- It performs the required operation.
- It does not return any value.
void function_name(parameter_list);
Type 3: No
Arguments and With Return Value
- The function does not receive any arguments.
- It performs an operation internally.
- It returns a value to the calling function.
return_type function_name(void);
Type 4: With Arguments
and With Return Value
- The function receives arguments from the calling
function.
- It performs the required operation.
- It returns the result to the calling function.
return_type function_name(parameter_list);
Note: Arguments
and Parameters
These two terms
are closely related but have different meanings.
Parameters: Variables
declared in the function definition to receive values.
Arguments:
Actual values passed to the function during the function call.
Passing
values to a function
There are two methods
to send data from the calling function to the called function through
arguments.
1.
Call by Value
In C, arguments
are passed to functions by value by default. Call by value means that a copy of
the actual argument is passed to the function. Any changes made to the
parameters inside the function will not affect the original value stored in the
variables in the calling function.
2.
Call by Address Using Pointers
When the address
of a variable is passed to a function, the function can access and modify the value
of the original variable through a pointer.
Difference
Between Call by Value and Pointer-Based Passing
|
Call by Value |
Pointer-Based Passing |
|
Copy of the value is passed. |
Address of the variable is passed. |
|
Ordinary parameters are used. |
Pointer parameters are used. |
|
Changes do not affect original variables. |
Changes through pointers affect original variables. |
|
Separate parameter storage is used. |
Pointer refers to the original variable. |
No comments:
Post a Comment
Don't be a silent reader...
Leave your comments...
Anu