L22 L23 Modular Programming
L22 L23 Modular Programming
P r o g r a m m i n g - f u n c ti 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 ti o n s
L 2 2 - L 2 3
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)
return_type function_name(parameter_definition)
{
variable declaration;
statement1;
statement2;
.
.
.
return(value_computed);
}
11/06/2023 CSE 1001 Department of CSE 9
Defining a Function
Name (function name)
• You should give functions descriptive names
• Same rules as variable names, generally
Return type
• Data type of the value returned to the part of the program that activated (called)
the function.
Parameter list (parameter_definition)
• A list of variables that hold the values being passed to the function
Body
• Statements enclosed in curly braces that perform the function’s operations(tasks)
}
{
printf(“hello world\n”); Body
return 0;
}
11/06/2023 CSE 1001 Department of CSE 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;
}
11/06/2023 CSE 1001 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 ();
printf( “Back//toFUNCTION CALL
main function \n”);
Second ();
printf( “Back //toFUNCTION CALL
main function \n”);
return 0;
}
11/06/2023 CSE 1001 Department of CSE 13
Arguments and parameters
Both arguments and parameters are variables used in
a program & function.
Variables used in the function reference or function
call are called as arguments. These are written within
the parenthesis followed by the name of the function.
They are also called actual parameters.
Variables used in function definition are called
parameters, They are also referred to as formal
parameters.
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( “*”);
}
11/06/2023 CSE 1001 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);
}
11/06/2023 CSE 1001 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);
}