GONZALES, SARAH MAE T.
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 is also a subprogram (subroutine) that performs specific tasks
TWO TYPES OF FUNCTION
1. Standard function
These refer the predefined functions or built-in functions of the Turbo C system compiler. This kind of
functions are numerous, in fact the majority of the statements used and applied in Turbo C programs are
standard functions.
2. Program defined functions: Void
The keyword void indicates that the method does not return any value.
ADVANTAGES OF USING FUCTION
Functions fit naturally with a top-down approach.
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.
Functions can be tested individually.
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;}
GONZALES, SARAH MAE T.
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();
void test (testvar)
mainvar=5.0;
double testvar;{
printf(%3.1lf\n,mainvar);
testvar*=5;
getche();}
printf(%3.1lf\n,testvar);}
Syntax of gotoxy, text color, text background in turbo c
The main purpose of introducing graphics at this stage is to demonstrate how important it is to learn
functions.
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
Controlling the cursor
gotoxy() is a function used to send the cursor to the specified coordinates in the active window. Here the
first coordinate is the column (x) and the second coordinate is the row (y). Here it accepts relative
coordinates to the current active window
gotoxy(40,12);
It sends the cursor to the 40th column and 12th row.
Example:
#include<conio.h>
#include<stdio.h>
int main()
{
clrscr();
gotoxy(5,5);
/* sending cursor to 5th column and 5th row */
printf("Hello world");
gotoxy(7,7);
/* sending cursor to 7th column and 7th row */
printf("Hello world");
gotoxy(9,9);
/* sending cursor to 9th column and 9th row */
printf("Hello world");
return 0;
}
Working with colors in Turbo C (Text mode):
Turbo C supports the following colors in text mode. All colors can be used to set foreground color but, only
some of them can be used as background color.
Either numeric value or symbolic name can be used to set the color. All these colors are defined as
constants using enum type colors in conio.h
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
Setting the foreground (text) color
textcolor() is the function used to set the foreground (text) color in the active window. We can use either
numeric value or symbolic name to set the text color.
textcolor(RED); /* set the foreground (text) color to red */
textcolor(14);
/* change the foreground (text) color to yellow */
Printing colored text
Generally the printf() statement prints the text at the active cursor position only with the default foreground
color white. Where as cprintf() helps to print the text with the active window color.
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);
/* setting text color */
gotoxy(x,y);
cprintf("Codingfox");
y++;
}
return 0;
}
/* changing row */