Functions
Functions
To overcome this
Programs are broken into a number of smaller logical components, each of
which serves a specific task.
3
Apr 6, 2025 Dept of I&CT
Advantages of modularization
• Reusability
• Debugging is easier
• Build library
Standard functions
(library functions or built-in functions)
User-defined functions
Written by the user(programmer)
return_type
function_name(parameter_definition)
{
variable declaration;
statement1;
statement2;
.
.
.
return(value_computed);
}
}
{
printf("hello world\n”); Body
}
{ printf(" Hello from the function display
message” \n”);
} Body
void main()
{ printf(“Hello from main”;
DisplayMessage(); // FUNCTION CALL
printf(“Back in function main again.\n”;
}
5. If the function has no formal parameters, the list is written as (void) but it is
optional..
6. The return type is optional, when the function returns int 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.
void dispPattern(void )
{ int i;
for (i=1;i<=20 ; i++)
printf("*”;}
void dispPattern(char c )
{ int i;
for (i=1;i<=20 ; i++)
printf(“%c”, c);
printf("\n”;}
Addition program