L24 L26 Modular Programming
L24 L26 Modular Programming
P r o g r a m m i n g - f u n c t i o n s
M o d u l a r p r o g r a m m i n g a n d
F u n c t i o n s
L 2 2 - L 2 3
Lengthier programs
• Prone to errors
• tedious to locate and correct the errors
To overcome this
Programs broken into a number of smaller logical components,
each of which serves a specific task.
• Debugging is easier
• Build library
User-defined functions
Written by the user(programmer)
statement1;
statement2;
.
.
.
return(value_computed);
}
5/7/2022
}
printf(“hello world\n”);
return 0;
11
Function Definition and Call
// FUNCTION DEFINITION
Return type Function name Parameter List
void DisplayMessage(void)
{
printf(“Hello from function DisplayMessage\n”);
}
int main()
{
printf(“Hello from main \n”);
DisplayMessage(); // FUNCTION CALL
printf(“Back in function main again.\n”);
return 0;
5/7/2022
} CSE 1051 Department of CSE 12
Multiple Functions- An example
void First (void){ // FUNCTION DEFINITION
printf(“I am now inside function First\n”);
}
void Second (void){ // FUNCTION DEFINITION
printf( “I am now inside function Second\n”);
First(); // FUNCTION CALL
printf(“Back to Second\n”);
}
int main (){
printf( “I am starting in function main\n”);
First (); // FUNCTION CALL
printf( “Back to main function \n”);
Second (); // FUNCTION CALL
printf( “Back to main function \n”);
return 0;
5/7/2022 CSE 1051 Department of CSE 13
}
Arguments and Parameters
➢Both arguments and parameters are variables used in a program &
function.
1) local variables
2) global variables
5. If the function has no formal parameters, the list can be written as (void) or
simply ()
6. The return type is optional, when the function returns integer type data.
7. The return type must be void if no value is returned.
8. When the declared types do not match with the types in the function
definition, compiler will produce error.
int main(){
printf(“fn to display a line of stars\n”);
dispPattern();
return 0;
}
void dispPattern(void ){
int i;
for (i=1;i<=20 ; i++)
printf( “*”);
}
5/7/2022 CSE 1051 Department of CSE 24
Function with No Arguments but A return value
int readNum(void); // prototype
int main(){
int c;
printf(“Enter a number \n”);
c=readNum();
printf(“The number read is %d“,c);
return 0;
}
int readNum(){
int z;
scanf(“%d”,&z);
return(z);
}
5/7/2022 CSE 1051 Department of CSE 25
Fn with Arguments/parameters & No return values
void dispPattern(char ch); // prototype
int main(){
printf(“fn to display a line of patterns\n”);
dispPattern(‘#’);
dispPattern(‘*’);
dispPattern(‘@’);
return 0;
}
void dispPattern(char ch ){
int i;
for (i=1;i<=20 ; i++)
printf(“%c”,ch);
}
5/7/2022 CSE 1051 Department of CSE 26
Function with Arguments/parameters & One return value
Int main(){
int a,b,c;
printf(“\nEnter numbers to be added\n”);
scanf(“%d %d”,&a,&b);
c=fnAdd(a,b);
printf(“Sum is %d “, c);
return 0;
}
int fnAdd(int x, int y ){
int z;
z=x+y
return(z);
}
5/7/2022 CSE 1051 Department of CSE 27
Problems…
Write appropriate functions to
1. Find the factorial of a number ‘n’.
2. Reverse a number ‘n’.
3. Check whether the number ‘n’ is a palindrome.
4. Generate the Fibonacci series for given limit ‘n’.
5. Check whether the number ‘n’ is prime.
6. Generate the prime series using the function written for prime check,
for a given limit.