Sei sulla pagina 1di 11

COMPUTER PROGRAMMING

Engr. Anees Ahmed Soomro


Assistant Professor
CSE QUEST Nawabshah
INTRODUCTION TO COMPUTER PROGRAMMING

1) Functions(Predefined function and user defined function)


2) Concept of user define functions
3) Declaration of function
4) Function prototype declaration, functions that return a value
Functions
• Predefined functions are such as printf(), scanf() and getch().

Concept of user defined functions

• These functions are used to avoid unnecessary repetition of code.


• The function is created by user for specific task.
• The user defined function can be modified at any time by
programmer.
• The name of function can be given by programmer by the legal
identifier rules.
• The user defined functions are useful for software development.
• The user define functions make the programmer virtuoso and smart.
Example

Standalone Program main()


{
Line1
.
.
Line50
Code written twice
if function not used
Line1
.
.
.
line50
}
Code written only once if function used
fun()
main() {
{ line1;
fun(); .
.
.
fun(); line50;
} }
Declaration of function
void line(void);
void main(void)
{
line();
printf(“\xDB Quest Nawabshah\xDB\n”);
line();
}
void line(void)
{
int j;
for(j=1;j<=20;j++)
printf(“\xDB”);
prinft(“\n”);
}
User defined function
void message(void);
void main(void)
{
message();
}
void message(void)
{
printf(“Hello Quest”);
printf(“Hello Quest”);
printf(“Hello Quest”);
printf(“Hello Quest”);
printf(“Hello Quest”);

}
Function Prototype (Declaration)
Syntax:

• Calling a function
line();
• Function prototype (Declaration)
void line(void);
• A prototype declares a function
• A function call executes a function
• A function definition is the function itself
A sound example
Special character ‘\x7’ called Bell
void twobeep(void);
void main(void)
{
twobeep();
printf(“type any character”);
getch();
twobeep();
}
void twobeep(void)
{
long j;
printf(“\x7”);
for(j=1;j<100000;j++)
;
printf(“\x7”);
}
Function that return a value
char getlc(void);
void main(void)
{
char chlc;
printf(“Type ‘a’ for first selection,’b’ for second”);
chlc=getlc();
switch(chlc)
{
case ‘a’:
printf(“\n You typed an ‘a’”);
break;
case ‘b’:
printf(“\n You typed a ‘b’”);
break;
default:
printf(“\n you choose non-existent set”);
}
}
Continued….
char getlc(void)
{
char ch;
ch=getche();
if(ch>64 && ch>91)
ch=ch+32;
return ch
}

Potrebbero piacerti anche