Gonzales, Sarah Mae T. CE2: Function
Gonzales, Sarah Mae T. CE2: Function
CE2
FUNCTION
It refers to the section of a program that performs a specific task. The task assigned to a function
is performed whenever Turbo C encounters the function name.
It can often be used more than once in a program and several other program s, thereby saving
programming time.
Using functions provides a natural method for dividing programming tasks among team
programmers.
EXAMPLE:
Problem 1. design a function-oriented program that performs a printing: OFFICIAL RECEIPT. Call or
invoke this function from the main program
twice.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void asterisk(void);
main(){
clrscr();
textcolor(WHITE+BLINK);
asterisk();
gotoxy(30,3);printf(OFFICIAL RECEIPT);
asterisk();
getch();}
void asterisk(void){
textcolor(YELLOW);
printf(\n00000000000000000000000000000000
00000000000000000000000000000000000000
0000000000);
return 0;}
CE2
Problem 2. design a function-oriented program that passes the value of the parameter test. The function
multiplies test by 5 and displays the result according to the format %3.1lf.
#include<stdio.h>
main(){
double mainvar;
clrscr();
mainvar=5.0;
double testvar;{
printf(%3.1lf\n,mainvar);
testvar*=5;
getche();}
printf(%3.1lf\n,testvar);}
All the example programs given here, work only in Turbo C platform because functions like gotoxy(),
clrscr(), textcolor(), cprintf(), window(), rand(), kbhit() and delay() are not available in other platforms.
Graphics in C language
Display can be normally used in either text mode or graphic mode. In text mode the screen is divided into
80 columns and 25 rows. In case of graphic mode the total screen is divided into 640 pixels width and 480
pixels height. Latest displays are even rich in resolution.
Generally text displays are faster then graphic displays because in graphic mode we have to program
pixel by pixel. Here in this session, we are going to learn graphics in text mode. It may be help full in
developing simple applications and games.
Turbo C is rich in graphics
Functions, colors used to generate graphics are not in the original specification of C language. Graphic
libraries are available for different platforms in the market. But the turbo C is rich in graphic tools, provides
number of functions to work with graphics both in text mode and in graphic mode. Most of the text mode
graphic functions are available in conio.h
gotoxy(40,12);
printf("Hello world");
gotoxy(7,7);
printf("Hello world");
gotoxy(9,9);
printf("Hello world");
return 0;
}
Numeric Value
Symbolic Name
As background Color
As foreground color
BLACK
YES
YES
BLUE
YES
YES
GREEN
YES
YES
CYAN
YES
YES
RED
YES
YES
MAGENTA
YES
YES
BROWN
YES
YES
LIGHTGRAY
YES
YES
DARKGRAY
NO
YES
LIGHTBLUE
NO
YES
10
LIGHTGREEN
NO
YES
11
LIGHTCYAN
NO
YES
12
LIGHTRED
NO
YES
13
LIGHTMAGENTA
NO
YES
14
YELLOW
NO
YES
15
WHITE
NO
YES
128
BLINK
NO
YES
textcolor(14);
textcolor(14);
cprintf("Hello world"); /* prints the "Hello World" in yellow color */
Example:
#include<conio.h>
#include<stdio.h>
int main()
{
int i,x,y;
clrscr();
x=5;
/* setting column */
y=3;
/* setting row
for(i=1;i<=15;i++)
*/
/* changing colors */
{
textcolor(i);
gotoxy(x,y);
cprintf("Codingfox");
y++;
}
return 0;
}
/* changing row */