A function is a block of code that performs a specific task. It helps us avoid writing the same code multiple times. In this program, a user-defined function named line() is created to print two horizontal lines. The main() function calls line() before and after printing the text.
How It Works?
stdio.h and conio.h.line() that prints two horizontal lines.main() function and clear the screen using clrscr().line() function.printf().line() function again to print the lines after the text.getch().#include <stdio.h> #include <conio.h> void line() { printf("\n ------------------------------"); printf("\n ------------------------------"); } int main() { clrscr(); line(); printf("\n Hey"); printf("\n How are you guys?"); line(); getch(); }
function named star() to print a line of 30 asterisks (*). Call the function before and after displaying the message "Welcome to C Programming".star() that prints a line of stars using printf(). Call the function twice from the main() function.
#include <stdio.h> #include <conio.h> void star() { printf("\n ******************************"); } int main() { clrscr(); star(); printf("\n Welcome to C Programming"); star(); getch(); }
star() that prints a line of asterisks. The main() function calls star() before and after displaying the message, demonstrating how a function can be reused to avoid writing the same code multiple times.