Modules3(Functions)
Modules3(Functions)
Functions
FUNCTIONS:
Introduction using functions,
Function definition,
function declaration,
function call,
return statement,
passing parameters to functions,
scope of variables,
storage classes,
recursive functions.
What is C function ?
• Every C program should consist of one or more functions.
Among these functions main ( ) function is compulsory. All
programs start execution from main ( ) function.
• Functions: Functions are independent program modules that
are designed to carry out a particular task.
• A large program can be divided into manageable pieces called
modules where each module does a specific task. Thus, the
functions often called modules are self-contained small
programs that carry out some specific, well defined tasks.
• Functions act like building blocks using which any desired
activity can be performed by combining one or more
function.
main ()
{ func1 ()
------------------- {
------------------- Statement block;
func1 (); }
return
} 0;
Built-in functions: These are C language functions already available with C compliers and can
be used by any programmers.
User-defined functions: These are written by programmers for their own purpose and are not
readilyavailable.
Here some of the common header files you use for the program.
Advantages of
function:
Reusability and reduction of code size:
The existing function can be re-used as building blocks to create new programs. This
results in reduced program size .These functions can be used any number of time.
Readability of the program can be increased:
Programs can be written easily and we can keep track of what each function is doing.
Modular programming approach:
A large program is divided into smaller sub programs so that each sub program
performs a specific task. This approach makes the program development more
manageable.
Easier debugging:
Using modular appoarch, localizing, locating and isolating a faulty function is much
easier.
Function sharing:
A function can be shared by many programmers.
Elements of user-defined
function:
There are three elements that are related to functions:
• Function definition
• Function call
• Function declaration/Function prototype
Function declaration:
We declare the variables before they are used the functions.
This process of declaring the function before they are used is called
function prototype.
The function prototype also known as function declaration.
A function declaration consists of four parts:
• Function type/ return type –what type of values the function will return
to the caller
• Function name
• Parameter List
• Ending with semicolon
Function Definition
The program module that is written to achieve a specific task is called function definition.
Each function definition consists of two parts. Namely:
Function header
Function body
int main() {
int num1, num2, sum;
return 0;
}
// Function definition
int add(int a, int b) {
return a + b;
}
Function Header: It consists of three parts:
Function type,
function name
list of parameters.
// Function header
int add(int a, int b);
// Function definition
int add(int a, int b) {
return a + b;
}
Function type:The function type specifies the type of value that the function is expected to return to the
callingfunction.
If the return type or function type is not specified, then it is assumed as int.
If function is not returning anything, then it is void.
Parameter
s
Actual Parameters:actual parameters (also called arguments) are the real values or variables passed to the
function when it is called.
int main() {
int x = 5;
int y = 10;
int result = add(x, y); // x and y are actual parameters (arguments)
printf("The result is %d\n", result);
return 0;
}
Formal Parameters: formal parameter refers to the variables listed in the function definition
int add(int a, int b)
// a and b are formal parameters
{
return a + b;
}
Actual parameter Formal parameter
Actual parameters are used in calling Formal parameters are used in the function header
function when a function is invoked. of a called function.
Example: Example:
C=add(a,b); int add(int m,int n)
Here a and b are actual parameters. {
Actual parameter sends values to Formal parameters receive values from the
the formal
parameters. actual parameters.
Example: Example:
C=add(4,5); int add(int m,int n)
{
}
Here,m will have the value 4 and n will have
the value 5.
Passing parameter a function:
• In pass by value the values of actual parameters are copied into formal
parameters.
• So, even if the values of the formal parameters changes in the called function, the
values of the actual parameters are not changed.
Example of Pass by value/call by
value
#include <stdio.h>
int main() {
int a = 10;
modifyValue(a); Formal parameter
printf("%d\n", a); // Output: 10
return 0;
}
In this example, even though modifyValue tries to change x, the original variable
a remains unchanged because x is a copy of a.
Pass by reference/call by reference
• When a parameter is passed by reference, the address (or reference) of the actual parameter is passed to
the function.
• This means that changes made to the parameter inside the function do affect the original value.
#include <stdio.h>
int main() {
int a = 10;
modifyValue(&a); // Passing the address of a
printf("%d\n", a); // Output: 20
return 0;
}
Storage class: