Unit-5 Functions & Preprocessor Directives
Unit-5 Functions & Preprocessor Directives
main() func1()
{ {
………….. Statement
………….. Block;
func1(); }
…………
………..
return 0;
}
INTRODUCTION
It is not necessary that the main() can call only
one function, it can call as many functions as it
wants and as many times as it wants. For
example, a function call placed within a for loop,
while loop or do-while loop may call the same
function multiple times until the condition holds
true.
INTRODUCTION
It is not that only the main() can call another
functions. Any function can call any other
function. In the fig. one function calls another,
and the other function in turn calls some other
function.
INTRODUCTION
return statement;
}
FUNCTION DEFINITION
Function Header : It is a part of function definition.
It is similar to the function prototype declaration but
does not require the semicolon at the end.
#include<stdio.h>
#include<conio.h>
void sum();
void main()
{
clrscr();
sum();
getch();
}
1)No Argument No Return
void sum()
{
int a,b,c;
printf("\n Enter Value 1 :");
scanf("%d",&a);
c=a+b;
printf("\n Sum is %d",c);
}
2)No Argument With Return
This type of functions does not take any value from the calling
function. After execution, they return some value to the main
function. The data type of returned value determines the return
type of function
#include<stdio.h>
#include<conio.h>
int sum();
void main()
{
int z;
clrscr();
z=sum();
printf("\n Sum is :%d",z);
getch();
}
2)No Argument With Return
int sum()
{
int a,b,c;
printf("\n Enter Value 1 :");
scanf("%d",&a);
c=a+b;
return c;
}
3)With Argument No Return
This type of functions take some value from the calling
function (usually main()) to the called function.
However, the functions of this type does no take any
value from the called function to the calling function as
we did in previous case. The values, which are passed
with the function call are called as "parameters" or
"arguments".
#include<stdio.h>
#include<conio.h>
void sum(int a,int b);
void main()
{
int a,b;
clrscr();
printf("\n Enter Value 1 :");
scanf("%d",&a);
3)With Argument No Return
sum(a,b);
getch();
}
z=sum(a,b);
printf("\n Sum is %d",z);
getch();
}
#include<stdio.h>
int Fact(int n);
void main()
{ int num;
scanf(“%d”, &num);
printf(“\n Factorial of %d = %d”, num, Fact(num));
getch();
}
int Fact(int n)
{ int p;
if(n==1)
return 1;
else
p= (n * Fact(n-1));
return p;
}
PROS AND CONS OF RECURSION
PROS:
Recursive solutions often tend to be shorter and simpler
than non-recursive ones.
Code is clearer and easier to use
Recursion represents like the original formula to solve a
problem.
Follows a divide and conquer technique to solve
problems
PROS AND CONS OF RECURSION
CONS:
For some programmers and readers, recursion is a difficult
concept.
Aborting a recursive process in midstream is slow and
sometimes time consuming.
Using a recursive function takes more memory and time to
execute as compared to its non-recursive counter part.
It is difficult to find bugs, particularly when using global
variables
MATHEMETICAL FUNCTIONS
void main()
{
int a=-5;
clrscr();
void main()
{
char ch;
clrscr();
printf("\nEnter a character:");
scanf("%c",&ch);
if(isalpha(ch))
printf("\nCharacter is alphabetic");
else
printf("\nCharacter is not alphabetic");
fflush(stdin);
getch();
}
ISDIGIT
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
void main()
{
char ch;
printf("\nEnter a character:");
scanf("%c",&ch);
if(isdigit(ch))
printf("\nCharacter is a digit");
else
printf("\nCharacter is not a digit");
fflush(stdin);
getch();
}
ISXDIGIT
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
void main()
{
char ch;
clrscr();
printf("\nEnter a character:");
scanf("%c",&ch);
if(isxdigit(ch))
printf("\nCharacter is hexadecimal");
else
printf("\nCharacter is not hexadecimal");
fflush(stdin);
getch();
}
ISGRAPH
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
void main()
{
char ch;
clrscr();
printf("\nEnter a character:");
scanf("%c",&ch);
if(isgraph(ch))
printf("\nCharacter is not blank but printing");
else
printf("\nCharacter is blank");
fflush(stdin);
getch();
}
ISPRINT
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
void main()
{
char ch;
clrscr();
printf("\nEnter a character:");
scanf("%c",&ch);
if(isprint(ch))
printf("\nCharacter is printable");
else
printf("\nCharacter is not printable");
fflush(stdin);
getch();
}
ISPUNCT
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
void main()
{
char ch;
clrscr();
printf("\nEnter a character:");
scanf("%c",&ch);
if(ispunct(ch))
printf("\nCharacter is a punctuation");
else
printf("\nCharacter is not a punctuation");
fflush(stdin);
getch();
}
ISSPACE
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
void main()
{
char ch;
clrscr();
printf("\nEnter a character:");
scanf("%c",&ch);
if(isspace(ch))
printf("\nCharacter is blank");
else
printf("\nCharacter is not blank");
fflush(stdin);
getch();
}
ISUPPER
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
void main()
{
char ch;
clrscr();
printf("\nEnter a character:");
scanf("%c",&ch);
if(isupper(ch))
printf("\nCharacter is in uppercase");
else
printf("\nCharacter is not in uppercase");
fflush(stdin);
getch();
}
ISLOWER
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
void main()
{
char ch;
clrscr();
printf("\nEnter a character:");
scanf("%c",&ch);
if(islower(ch))
printf("\nCharacter is in lowercase");
else
printf("\nCharacter is not in lowercase");
fflush(stdin);
getch();
}
TOLOWER
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
void main()
{
char ch;
clrscr();
printf("\nEnter a character:");
scanf("%c",&ch);
printf("\nLowercase=%c",tolower(ch));
fflush(stdin);
getch();
}
TOUPPER
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
void main()
{
char ch;
clrscr();
printf("\nEnter a character:");
scanf("%c",&ch);
printf("\nUppercase=%c",toupper(ch));
fflush(stdin);
getch();
}
THE PREPROCESSOR
DIRECTIVE
INTRODUCTION
The preprocessor is a program that processes the source
code before it passes through the compiler. It operates
under the control of preprocessor directive which is
placed in the source program before the main().
The name of the macro serves as the header and the macro
body serves as the function body. The name of the macro will
then be used to replace the function call.
Function-like macros
The function-like macro includes a list of parameters.
References to such macros look like function calls. However,
when a macro is referenced, source code is inserted into the
program at compile time.
White space in between the identifier and the left parenthesis that
introduces the parameter list is not allowed.
void main(void)
{
#ifdef MACRO1 // test whether MACRO1 is defined...
printf("\nMACRO1 Defined\n");
#endif
#if condition
Controlled text1
#else
Controlled text2
#endif
CONDITIONAL DIRECTIVES
#include<stdio.h>
#define NUM 11
void main()
{
#if((NUM%2)==0)
printf("\nNumber is Even");
#else
printf("\nNumber is Odd");
#endif
}
CONDITIONAL DIRECTIVES
The #elif Directive
The #elif directive is used when there are more than two possible alternatives. The #elif
directive like the #else directive is embedded within the #if directive and has the following
syntax:
#if condition
Controlled text1
#elif new_condition
Controlled text2
#else
Controlled text3
#endif
CONDITIONAL DIRECTIVES
THE #endif DIRECTIVE
The general syntax of #endif preprocessor directive which is used to end the conditional
compilation directive can be given as:
#endif
The #endif directive ends the scope of the #if , #ifdef , #ifndef , #else , or #elif directives.