Modular Programming (Functions) : Anand Kr. Srivastava Assistant Professor
Modular Programming (Functions) : Anand Kr. Srivastava Assistant Professor
(functions)
How?
First Define the different Module of a given task
independently. Then integrate them into a single
program.
Advantage of Using Modular Programming
( Function).
f ( x) x 2 2 x 3
What is f(2)?
f (2) (2) 2 2(2) 3 4 4 3 11
f (2) is 11
Returned
X Function value
2 f ( x) 11
How to use predefined Function:
In C programming Language, Predefined functions
are defined in corresponding Header files( .h files).
When we use the predefined function, we have to
add the corresponding header file for their definition
with following syntax:
#include<header file Name>
Ex: #include<stdio.h> for
printf(),scanf()….predefined functions.
When compiler get any function in the execution of program,
it jumps on the definition of function( in case of predefined
function, it jumps into the corresponding header file).
How to use user defined Function:
- Step-1 Declaration of function:
Syntax: return_type Function_name(parameter);
#include<stdio.h>
#include<conio.h>
void Table(int x);
void Table(int x)
void main()
{
{ for(int i=1;i<=10;i++)
int n; {
printf(“Enter the No\n”); int z=x*i;
scanf(“%d”,&n); printf(“%d*%d=%d”,x,i,z);
}
}
Table(n);
getch();
}
Case-3 ( Function have parameter & some return type)
W.A.P. to calculate the factorial of a given No
#include<stdio.h>
#include<conio.h>
int Fact();
int Fact()
void main()
{
{ int n,z=1;
int n; printf(“Enter the No\n”);
printf(“Enter the No\n”); scanf(“%d”,&n);
scanf(“%d”,&n);
for(int i=1;i<=n;i++)
{
int y=Fact(); z=z*i;
}
printf(“Factorial is %d”,y); return z;
getch(); }
}
Case-4 ( Function have parameter & some return type)
W.A.P. to calculate the factorial of a given No
#include<stdio.h>
#include<conio.h>
void Table(int x);
int Fact(int x)
void main()
{
{ int z=1;
int n;
printf(“Enter the No\n”); for(int i=1;i<=n;i++)
scanf(“%d”,&n); {
z=z*i;
}
int y=Table(n); return z;
}
printf(“factorial is %d”,y);
getch();}
Exercise:
Any C program
(1) must contain at least one function
(2) need not contain ant function
(3) needs input data
(4) none of the above
The purpose of main function is
(1) to stop program execution
(2)to stop algorithm
(3)to start program execution
(4)to start algorithm
int cal_sum(int a,int b);in the above example int at the beginning indicates
(1) name of function
(2)return type of function
(3)both function arguments are integer
(4)none of the above
The purpose of return statement is
(1)to return control back to calling function
(2)to return control and value back to calling function
(3)to return void
(4)to return value to the calling function
The statement used to send back any value to the calling function is
(1) continue
(2)exit
(3)break
(4)return
Any program in c contains atleast
(1) three functions
(2) two functions
(3) one function
(4) zero functions
Actual and formal parameters must agree in
(1) names and data types
(2)number of arguments and data types
(3)names and number of arguments
(4)data types
void funct(void)
the above function declaration indicates
(1) it returns nothing and no arguments
(2)it returns nothing and had arguments
(3)it returns a value and had arguments
(4)it returns a value and noa rguments
If the number of actual arguments are not matching with formal arguments
then (1) no error (2)compiler error (3)logical error (4)syntax error
The names of actual parameters and formal parameters
(1) almost same (2)should be same (3)always same (4)need not be same
Any function can be called from any other function. this statement is
(1)neither true nor false (2)true (3)false (4)true some times
Recursion means
(1) fuction calling a same function (2)fuction with out a return value
(3)function calling a function (4)passing a function to a function
Thanks