0% found this document useful (0 votes)
30 views

Module III

The document discusses modular programming concepts and functions in C programming. It explains that modular programming separates a program into independent and interchangeable modules. It then defines functions, their declaration and definition, parameter passing techniques of call by value and call by reference, and categories of functions.

Uploaded by

ishorishore
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Module III

The document discusses modular programming concepts and functions in C programming. It explains that modular programming separates a program into independent and interchangeable modules. It then defines functions, their declaration and definition, parameter passing techniques of call by value and call by reference, and categories of functions.

Uploaded by

ishorishore
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

MODULE III

Explain modular programming concepts to solve programs effectively.


Modular programming is defined as a software design technique
that focuses on separating the program functionality into independent,
interchangeable modules. Each of them contains everything needed to
execute only one aspect of functionality.

Advantages of modular programming

The following are advantages of modular programming -

o Code is easier to read


o Code is easier to test
o Reusability
o Faster fixes
o Low-risk update
o Easy collaboration.

FUNCTION
A function is a subprogram written for a particular task. Every C
program has at least one function, which is main(), and all the most
trivial programs can define additional functions.A
function declaration tells the compiler about a function's name, return
type, and parameter list. A function definition provides the actual body
of the function.
Classification of function
C function can be classified into two major categories
1.Library functions:These are built in functions used to perform well
defined tasks.eg printf(),scanf(),strcpy(),sqrt() etc.
2.user defined functions: A programmer create the function according to
their programming requirements.
Defining a Function
The general form of a function definition in C programming language is
as follows −
return_type function_name( parameter list )
{
body of the function
}
A function definition in C programming consists of a function
header and a function body. Here are all the parts of a function −
 Return Type − A function may return a value.
The return_type is the data type of the value the function
returns.
 Function Name − This is the actual name of the function.
 Parameters − When a function is invoked, you pass a value to the
parameter. This value is referred to as actual parameter or
argument. The parameter list refers to the type, order, and number
of the parameters of a function.
 Function Body − The function body contains a collection of
statements that define what the function does.

Example

include<stdio.h>
int main()
{
int num1, num2, s;

printf("\nEnter the two numbers : ");


scanf("%d %d", &num1, &num2);

//Call Function Sum With Two Parameters


s = sum(num1, num2);

printf("nAddition of two number is : ");


return (0);
}
// function
int sum(int num1, int num2)
{
int add;
add = num1 + num2;
return (add);
}

Function Declarations

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 −


return_type function_name( parameter list );
eg.
int sum(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 sum(int, int);

Function Arguments
If a function is to use arguments, it must declare variables that accept the
values of the arguments. These variables are called the formal
parameters of the function.eg.void sum(int x,int y)//x,y are called
formal argument/parameters.
{
}
When a function is called the parameters that are passed to the function
are called actual parameters.eg void main()
{
Sum(a,b)//a,b are actual parameters.
}
While calling a function, there are two ways in which arguments can be
passed to a function –
Parameter passing techniques
1.call by value
2.call by reference
The call by value method of passing arguments to a function copies the
actual value of an argument into the formal parameter of the function.
Here the values of actual arguments are copies to the formal variables.so
the actual parameters are not changed.
#include<stdio.h>
void sum(int x,int y)
{ int s;
s=x+y;
printf("sum=%d \n", s);
}
int main()
{ int a,b;
Printf(“enter the two numbers”);
Scanf(“%d%d”,&a,&b);
sum(a,b);//passing value in function
return 0;
}
Call by reference
This method of passing arguments to a function 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.
#include<stdio.h>
void sum(int *x,int *y)
{ int s;
s=*x+*y;
printf("sum=%d \n", s);
}
int main()
{ int a,b;
Printf(“enter the two numbers”);
Scanf(“%d%d”,&a,&b);
sum(&a,&b);//passing reference in function
return 0;
}

Category of function

 Functions without arguments and without return values


 Functions without arguments and with return values
 Functions with arguments and without return values
 Functions with arguments and with return values

You might also like