MODULE 4—WORKING WITH
FUNCTIONS
1
MODULAR PROGRAMMING
The process of identifying the tasks and various sub-tasks involved in the program
design is called modular programming.
A module is self contained and intended to perform a single task. It will be
independent. It will be relatively short.
There may be large no of program modules and sub-modules in a complex program.
Every program has a special module called main module from where the program
execution begins. The main module performs the most fundamental tasks in the
program.
When the program is coded, main module becomes the main program and all other
modules may be called sub-programs.
A statement in main program calls the sub-module into action. Once the sub-module
has completed the task, execution goes back to the main module.
2
ADVANTAGES OF MODULAR PROGRAMMING
Program readability is improved.
It is easier to design, code and test the program in modules than all at once.
Different modules of a complex program can be designed by different
programmers.
A single module can be used in more than one place in the program thereby reducing
the coding job.
Modules can be reused by creating library of such modules.
3
FUNCTIONS
A function is a group of statements that together perform a task. It is a self
contained program segment.
Every C program has at least one function, which is main(), and all the most trivial
programs can define additional functions
A function will carry out its intended action whenever it is called from some other
portion of the program. Once it complete the actions, control will be returned to the
point at which function was accessed.
A function declaration tells the compiler about a function's name, data type, and
parameters. A function definition provides the actual body of the function.
The C standard library provides numerous built-in functions that your program can
call. For example, strcat().
We can also make user defined functions. We’ll see how….
4
FUNCTIONS
Library functions
Predefined functions
part of header file (such as MATH.h) which is called during
runtime
given by developers
Name of function can't be changed
eg: printf, scanf
User defined functions
function which are created by user as per his own requirements
name of function is decided by user
name of function can be changed any time
5
USER DEFINED FUNCTION
Defining a Function
The general form of a function definition in C programming language is as follows −
Data_type function_name( list of arguments)
Argument declarations
{
body of the function
}
6
FUNCTION EXAMPLE
/* function returning the max between two numbers */
int max(int num1, int num2)
{ int result; /* local variable declaration */
if (num1 > num2)
result = num1;
else
result = num2;
return (result);
}
7
FUNCTION DEFINITION
Function definition consists of three main components
A heading or first line which has function datatype, function name,
followed by optional list of arguments enclosed in parentheses.
A list of argument declarations, when arguments are included in heading.
A compound statement which comprises the body of the function.
General form of first line is
data-type function-name (list of formal arguments)
The data-type specifies the type of value that function will return through
function name using the return statement.
When function does not return a value, the data type is to be specified as
void.
8
List of arguments is a comma separated list of variable names that will
receive the values of arguments when the function is called.
Eg: float interest (p,n,r)
Argument declaration immediately follow the first line of the function
definition.
eg: float interest (p,n,r)
float p,r;
int n;
OR float interest( float p, float r, int n)
Next component of function definition is the body of the function. It is a
compound statement that defines the action to be taken by the function.
When a return statement is encountered, control is transferred to the calling
portion of the program.
9
10
return STATEMENT
return statement- It is used for 2 uses.
It can be used for an immediate exit from function it is in.
Second, it can be used to return a value.
return(expression);
The value of the expression is returned to the calling portion of the program.
A return statement can be written even without an expression.
If the expression is omitted, return causes control to go back to the calling portion
without transfer of any information.
11
CALLING A FUNCTION
When a function is defined in a program, it can be accessed from anywhere
within that program.
Accessing a function is also known as function call.
A function call consists of the function name followed by a list of arguments
enclosed in parenthesis separated by commas.
The function call can appear as a simple expression statement or as an
operand within an expression.
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, it returns the program control back to the main
program.
For eg. X=power(a,b);
reverse(k);
12
#include <stdio.h>
/* function declaration */
int max(int num1, int num2);
int main ()
{ /* local variable definition */
int a = 100;
int b = 200;
int ret; /* calling a function to get max value */
ret = max(a, b);
printf( "Max value is : %d\n", ret );
return 0;
}
/* function returning the max between two numbers */
int max(int num1, int num2)
{ /* local variable declaration */
int result;
if (num1 > num2)
result = num1;
else
result = num2;
return result;
}
13
OUTPUT:
We have kept max() along with main() and compiled the source code. While running
the final executable, it would produce the following result −
Max value is : 200
14
FUNCTION ARGUMENTS OR PARAMETERS
Actual parameter/argument:
The arguments that appear on a function call are known as actual arguments or
actual parameters.
Formal parameter/argument:
The arguments that appear in function declaration are formal arguments.
When a function call is executed, control is transferred to the function and the
values of actual arguments are copied to the respective formal arguments
15
FUNCTION CALLING
[Link]. Call Type & Description
1 Call by valueThis method copies the actual value of an argument into the formal
parameter of the function. In this case, changes made to the parameter inside the
function have no effect on the argument.
2 Call by reference This method copies the address of an argument into the formal
parameter. Inside the function, the address is used to access the actual argument
used in the call. This means that changes made to the parameter affect the
argument.
By default, C uses call by value to pass arguments. In general, it means the code within a function cannot alter the
arguments used to call the function. 16
#include<stdio.h> Example for pass by value
int main()
{
int org = 10;
int change(int);
printf("\nThe original value is %d“,org);
printf("\nThe value returned by function is %d“, change(org));
printf("\ nThe value after function execution is %d“,org);
}
/*
OUTPUT
int change(int a) The original value is 10.
The value returned by function is 100.
{ The value after function execution is 10.*/
a = a * a;
return a;
}
17
int main() Example for pass by reference
{
int org = 10;
int change(int &);
printf("\nThe original value is %d“,org);
printf("\nThe value returned by function is %d“, change(org));
printf("\ nThe value after function execution is %d“,org);
}
int change(int &a)
/*
{ OUTPUT
The original value is 10.
a = a * a; The value returned by function is 100.
return a; The value after function execution is 100.
*/
}
18
FUNCTION DECLARATION
A function declaration tells the compiler about a function name and how to call the
function. The actual body of the function can be defined separately.
A function declaration has the following parts −
data_type function_name( parameter list );
For the above defined function max(), the function declaration is as follows −
int max(int num1, int num2);
Parameter names are not important in function declaration only their type is required,
so the following is also a valid declaration −
int max(int, int);
19
FORWARD DECLARATION
There are some situations when the function call has to be made before function
definition.
In such cases, there must be a function declaration in the calling portion of the
program or prior to that.
This is called forward declaration.
The declaration informs the compiler that a function will be accessed before it is
defined.
20
Illustration of how one function calls other functions.
21
Q. Write a C program to determine value of binomial coefficient nCr using a
function to evaluate factorials.
22
Q. Write a C program to read an integer number and reverse it using a function.
23