EST102 Programming in C
MODULE IV
Working with functions-Introduction to modular programming, writing functions, formal
parameters, actual parameters, Pass by Value, Recursion, Arrays as Function Parameters
structure, union, Storage Classes, Scope and life time of variables, simple programs using
functions
MODULAR PROGRAMMING
Modular programming is the process of subdividing a computer program into
separate sub-programs. It is a software design technique that emphasizes separating
the functionality of a program into independent, interchangeable modules, such that
each contains everything necessary to execute only one aspect of the desired
functionality.
Advantages
Ease of Use :This approach allows simplicity, as rather than focusing on the
entire thousands and millions of lines code in one go, we can access it in the form
of modules.
Programming errors are easy to detect: Minimizes the risks of ending up with
programming errors and also makes it easier to spot errors, if any.
Allows re-use of codes: A program module is capable of being re-used in a
program which minimizes the development of redundant codes
Improves manageability: Having a program broken into smaller sub-programs
allows for easier management.
FUNCTION
A function is a group of statements that together perform a task. Every C
program has at least one function, which is main(), and can have additional functions.
Types of Functions
There are two types of functions in C programming:
Standard library functions
User-defined functions
STANDARD LIBRARY FUNCTIONS
IIPE 1
, EST102 Programming in C
The standard library functions are built-in functions in C programming. These
functions are defined in header files. For example,
The printf() is a standard library function to send formatted output to the
screen (display output on the screen). This function is defined in the stdio.h
header file.
The sqrt() function calculates the square root of a number. The function is
defined in the math.h header file.
The strlen() function calculates the length of a given string. The function is
defined in the string.h header file.
USER-DEFINED FUNCTIONS
Functions that we define ourselves to do certain specific task are referred as user-
defined functions.
Advantages of user-defined functions
The program will be easier to understand, maintain and debug.
Reusable codes that can be used in other programs
A large program can be divided into smaller modules. Hence, a large project can
be divided among many programmers.
Three parts of a user defined functions are:
1) Function Declaration or Prototype
2) Function Definition
3) Function Call
Function Declaration
A function prototype is simply the declaration of a function that specifies
function's name, parameters and return type. It doesn't contain function body. A
function prototype gives information to the compiler that the function may later be
used in the program.
Syntax
IIPE 2
, EST102 Programming in C
return_type function_name( parameter list );
Note:
If function definition is written after main, then only we write prototype
declaration in global declaration section
If function definition is written above the main function then ,no need to
write prototype declaration
Function Definition
A function definition in C programming consists of a function header and a function
body. Function header consist of return type,function name,arguments(parameters).
Return Type: A function may return a value. The return_type is the data type of
the value the function returns. Some functions perform the desired operations
without returning a value. In this case, the return_type is the keyword void
Function Name: The actual name of the function. The function name and the
parameter list together constitute the function signature.
Parameters: A parameter is like a placeholder. When a function is invoked, you
pass a value to the parameter. Parameters are optional; that is, a function may
contain no parameters.
Function Body: The function body contains a collection of statements that
define what the function does.
Syntax:
return_type function_name( argument list ) {
body of the function
}
Function Call
When a program calls a function, the program control is transferred to the called
function. A called function performs a defined task and when its return statement is
executed or when its function-ending closing brace is reached, it returns the program
control back to the main program.
Syntax:
functionName(parameter list);
Example:
IIPE 3
, EST102 Programming in C
int add(int,int,int); // function declaration
void main()
{
int x=10,y=20,z=30,res;
res = add(x,y,z); // function call
printf(“Result=%d”,res);
}
int add(int a, int b, int c) // function definition
{
int sum;
sum = a+ b + c;
return sum; //return statement
}
Return Statement
The return statement terminates the execution of a function and returns a value
to the calling function. The program control is transferred to the calling function after
the return statement.
In the above example, the value of the sum variable is returned to the main
function. The res variable in the main() function is assigned this value.
Formal and Actual Parameters
There are different ways in which parameters can be passed into and out of
functions. Let us assume that a function B() is called from another function A(). In this
case A is called the “caller function” and B is called the “called function or callee
function”. Also, the arguments which A sends to B are called actual arguments and the
parameters of B are called formal arguments.
Formal Parameter: A variable and its type as they appear in the prototype of the
function or method.
Actual Parameter: The variable or expression corresponding to a formal
parameter that appears in the function call in the calling environment.
In the above example, x, y and z in the main function are the actual parameter of add
function. Formal parameter of add function are a, b and c.
PASS BY VALUE
IIPE 4
MODULE IV
Working with functions-Introduction to modular programming, writing functions, formal
parameters, actual parameters, Pass by Value, Recursion, Arrays as Function Parameters
structure, union, Storage Classes, Scope and life time of variables, simple programs using
functions
MODULAR PROGRAMMING
Modular programming is the process of subdividing a computer program into
separate sub-programs. It is a software design technique that emphasizes separating
the functionality of a program into independent, interchangeable modules, such that
each contains everything necessary to execute only one aspect of the desired
functionality.
Advantages
Ease of Use :This approach allows simplicity, as rather than focusing on the
entire thousands and millions of lines code in one go, we can access it in the form
of modules.
Programming errors are easy to detect: Minimizes the risks of ending up with
programming errors and also makes it easier to spot errors, if any.
Allows re-use of codes: A program module is capable of being re-used in a
program which minimizes the development of redundant codes
Improves manageability: Having a program broken into smaller sub-programs
allows for easier management.
FUNCTION
A function is a group of statements that together perform a task. Every C
program has at least one function, which is main(), and can have additional functions.
Types of Functions
There are two types of functions in C programming:
Standard library functions
User-defined functions
STANDARD LIBRARY FUNCTIONS
IIPE 1
, EST102 Programming in C
The standard library functions are built-in functions in C programming. These
functions are defined in header files. For example,
The printf() is a standard library function to send formatted output to the
screen (display output on the screen). This function is defined in the stdio.h
header file.
The sqrt() function calculates the square root of a number. The function is
defined in the math.h header file.
The strlen() function calculates the length of a given string. The function is
defined in the string.h header file.
USER-DEFINED FUNCTIONS
Functions that we define ourselves to do certain specific task are referred as user-
defined functions.
Advantages of user-defined functions
The program will be easier to understand, maintain and debug.
Reusable codes that can be used in other programs
A large program can be divided into smaller modules. Hence, a large project can
be divided among many programmers.
Three parts of a user defined functions are:
1) Function Declaration or Prototype
2) Function Definition
3) Function Call
Function Declaration
A function prototype is simply the declaration of a function that specifies
function's name, parameters and return type. It doesn't contain function body. A
function prototype gives information to the compiler that the function may later be
used in the program.
Syntax
IIPE 2
, EST102 Programming in C
return_type function_name( parameter list );
Note:
If function definition is written after main, then only we write prototype
declaration in global declaration section
If function definition is written above the main function then ,no need to
write prototype declaration
Function Definition
A function definition in C programming consists of a function header and a function
body. Function header consist of return type,function name,arguments(parameters).
Return Type: A function may return a value. The return_type is the data type of
the value the function returns. Some functions perform the desired operations
without returning a value. In this case, the return_type is the keyword void
Function Name: The actual name of the function. The function name and the
parameter list together constitute the function signature.
Parameters: A parameter is like a placeholder. When a function is invoked, you
pass a value to the parameter. Parameters are optional; that is, a function may
contain no parameters.
Function Body: The function body contains a collection of statements that
define what the function does.
Syntax:
return_type function_name( argument list ) {
body of the function
}
Function Call
When a program calls a function, the program control is transferred to the called
function. A called function performs a defined task and when its return statement is
executed or when its function-ending closing brace is reached, it returns the program
control back to the main program.
Syntax:
functionName(parameter list);
Example:
IIPE 3
, EST102 Programming in C
int add(int,int,int); // function declaration
void main()
{
int x=10,y=20,z=30,res;
res = add(x,y,z); // function call
printf(“Result=%d”,res);
}
int add(int a, int b, int c) // function definition
{
int sum;
sum = a+ b + c;
return sum; //return statement
}
Return Statement
The return statement terminates the execution of a function and returns a value
to the calling function. The program control is transferred to the calling function after
the return statement.
In the above example, the value of the sum variable is returned to the main
function. The res variable in the main() function is assigned this value.
Formal and Actual Parameters
There are different ways in which parameters can be passed into and out of
functions. Let us assume that a function B() is called from another function A(). In this
case A is called the “caller function” and B is called the “called function or callee
function”. Also, the arguments which A sends to B are called actual arguments and the
parameters of B are called formal arguments.
Formal Parameter: A variable and its type as they appear in the prototype of the
function or method.
Actual Parameter: The variable or expression corresponding to a formal
parameter that appears in the function call in the calling environment.
In the above example, x, y and z in the main function are the actual parameter of add
function. Formal parameter of add function are a, b and c.
PASS BY VALUE
IIPE 4