0% found this document useful (0 votes)
105 views24 pages

Modular Programming (Functions) : Anand Kr. Srivastava Assistant Professor

1. Modular programming breaks a large problem into smaller pieces called modules or functions. 2. Functions allow code to be reused, helps manage complexity, and allows independent development of code. 3. The main advantages of using functions are reusability of code, reduced space and time complexity, simpler programs that are easier to edit, update, and understand.

Uploaded by

Seshu Reddy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
105 views24 pages

Modular Programming (Functions) : Anand Kr. Srivastava Assistant Professor

1. Modular programming breaks a large problem into smaller pieces called modules or functions. 2. Functions allow code to be reused, helps manage complexity, and allows independent development of code. 3. The main advantages of using functions are reusability of code, reduced space and time complexity, simpler programs that are easier to edit, update, and understand.

Uploaded by

Seshu Reddy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Modular Programming

(functions)

Anand Kr. Srivastava


Assistant Professor
Learning Objectives
 Explain the concept of modular program design
 Explain the concept of a function in C
 Explain why functions are important in programming
 Explain the structure of a function
 Return data type
 Parameters
 Apply the concept of a function to a practical problem
 Explain the Advantage of using Functions.
Modular Programming

 Break a large problem into smaller pieces


 Smaller pieces sometimes called „modules‟ or
„subroutines‟ or „procedures‟ or functions
 Why?
 Helps manage complexity
 Encourages re-use of code

 Allows independent development of code.

 How?
 First Define the different Module of a given task
independently. Then integrate them into a single
program.
Advantage of Using Modular Programming
( Function).

 Re-usability of Code: Re-usability of code without re-


defining.
 Reduce the Space Complexity as well as Time
Complexity.
 Make the Simpler Program.( Editing , Updating is
easy in Given Program).
 Easier to Understand.
Functions

 Functions are the set of instructions/lines to complete


the particular task.
 Functions are The „building blocks‟ of a program
 Any program is a combination of one or more then
one Functions(Modules).
 Following Syntax shows the existence of function
concept.
return_type function_name( parameter);
Category of Function

1. Pre-defined Function: Which are already defined


in C library. we can use these function as it is by
their name.
Ex: printf(),scanf(),getch(),clrscr(),main()……

2. User-Defined Function: These functions are


defined by user by their own task. and then user can
use. it can be combination of Predefined and user
defined.
Ex: and(),sub() , etc….
Functions - Mathematical View

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);

- Step-2 Definition of function:


Syntax: return_type Function_name(parameter)
{…………..
…………..
}
- Step-3 Calling of function:(use of function)
Syntax: Function_name(parameter);
Return types in function:

 int : when function return a integer type value to


main function.
 float: when function return a float type value to main
function.
 char: when function return a character type value to
main function.
 void: when function does not return a value to main
function.
Movement of Execution Control:

Function Declaration ; This is called Inter-communication


( Before the Calling)
between functions.
Main Body/Block Function Body/Block

void main() Jump return_type Function ()


{ {
---------------------------; ------------------------
---------------------------; --------------------------
---------------------------; --------------------------
Function call(); Back --------------------------
--------------------------
--------------------------; }
--------------------------;
--------------------------
}
Communication with data in between main () to
other function():
Case 1. main function calls a user-defined function without data.
( Zero parameter & no return type)
Case 2. main function calls a user-defined function with some value.
(transfer a value from main body to function body).-
using function parameter concept. ( parameter & no return type)
Case 3. any user defined function transfer a value to main function-
using return type concept with return statement.
( Zero parameter & some return type)
Case 4. main function send the data to function & function also send
the data to main function.
using function parameter & return type concept
( some parameter & some return type)
Case-1 ( Function have Zero parameter & No return type)
W.A.P. to Print the table of a given No

#include<stdio.h> void Table()


#include<conio.h> {
void Table(); int n;
void main() printf(“Enter the No\n”);
scanf(“%d”,&n);
{ for(int i=1;i<=10;i++)
{
Table(); int z=n*i;
printf(“%d*%d=%d”,n,i,z);
getch(); }
}
}
Case-2 ( Function have parameter & No return type)
W.A.P. to Print the table of a given No

#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:

- W.A.P Display Fibonacci Series up to 5th Level( Term) using function in


following Situation:

1. Staring values & level will be in user defined function body.


2. Staring values & level will be in main function body

-W.A.P to check weather a given no is Prime or not using function.


- W.A.P to find all Prime no in between 1 to 100 using function.
Example: Calculate Factorial of a given No.
#include<stdio.h>
#include<conio.h>
int fact(int n); Declaring the fact method( function)
void main()
{
int x;
printf(“Enter the no”);
scanf (“%d”,&x);
int z = fact(x); Calling fact();
printf(“Factorial of %d is %d”,x,z);
getch();
}

int fact( int n)


{
int f; Definition of fact function
for(int i=1;i<=n;i++)
f= f*I;
return f;
Questions:
 The keyword used to transfer control from a function back to the calling
function is
(1) switch (2).goto (3).go back (4).return

 How many times the program will print "IndiaBIX" ?


#include<stdio.h>
int main()
{ printf("IndiaBIX");
main();
return 0; }
(1)Infinite times (2)32767 times (3)65535 times (4) Till stack overflows
 Which of the following code will be infinite loop.
(1) if( ; ; ) (2). for( ; ; ) (3) while( ; ; ) (4) .All the above
 The use of „break‟ statement:
(1) to terminate the case in switch statement
(2) to force Immediate termination of a loop. (3) Both A & B. (4) None

 At the time of function calling , following things are match:


(1) Function Name ( as declared in declaration & definition)
(2)Function Parameter ( as declared in declaration & definition)
(3) return type ( as declared in declaration & definition)
(4)None (5). Both A & B (6). All A,B,C.

 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

 A function can be called in a program


(1) only one time (2)only once (3)any number of times (4)only three 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

You might also like