Function
-
Upload
sarah-mae-gonzales -
Category
Documents
-
view
1 -
download
0
description
Transcript of Function
![Page 1: Function](https://reader035.fdocuments.us/reader035/viewer/2022072002/563db97b550346aa9a9dbd03/html5/thumbnails/1.jpg)
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(“\n00000000000000000000000000000000000000000000000000000000000000000000000000000000”);
return 0;}
![Page 2: Function](https://reader035.fdocuments.us/reader035/viewer/2022072002/563db97b550346aa9a9dbd03/html5/thumbnails/2.jpg)
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>
double mainvar;
void test (testvar)
double testvar;{
testvar*=5;
printf(“%3.1lf\n”,testvar);}
main(){
clrscr();
mainvar=5.0;
printf(“%3.1lf\n”,mainvar);
getche();}
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
![Page 3: Function](https://reader035.fdocuments.us/reader035/viewer/2022072002/563db97b550346aa9a9dbd03/html5/thumbnails/3.jpg)
GONZALES, SARAH MAE T. CE2
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;
}
![Page 4: Function](https://reader035.fdocuments.us/reader035/viewer/2022072002/563db97b550346aa9a9dbd03/html5/thumbnails/4.jpg)
GONZALES, SARAH MAE T. CE2
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
0 BLACK YES YES
1 BLUE YES YES
2 GREEN YES YES
3 CYAN YES YES
4 RED YES YES
5 MAGENTA YES YES
6 BROWN YES YES
7 LIGHTGRAY YES YES
8 DARKGRAY NO YES
9 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
![Page 5: Function](https://reader035.fdocuments.us/reader035/viewer/2022072002/563db97b550346aa9a9dbd03/html5/thumbnails/5.jpg)
GONZALES, SARAH MAE T. CE2
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 */
![Page 6: Function](https://reader035.fdocuments.us/reader035/viewer/2022072002/563db97b550346aa9a9dbd03/html5/thumbnails/6.jpg)
GONZALES, SARAH MAE T. CE2
{
textcolor(i); /* setting text color */
gotoxy(x,y);
cprintf("Codingfox");
y++; /* changing row */
}
return 0;
}