ఏదైనా function ఏమీ రిటర్న్ చేయకపోతే దానిని void టైప్ అని అంటారు. ఉదాహరణకు:
void xyz(void) { }.
Example 1: ఈ ప్రోగ్రాంలో ఒక functionను రాసి దానిని main నుంచి call చేస్తున్నాం. రాసిన function ఒక integer(పూర్ణాంకం) తీసుకొని దాని అంకెల మొత్తాన్ని రిటర్న్ చేస్తుంది. ఒక main program దానిని call చెయ్యడం ఎలాగో చూపించడానికి రాశాం.
#include
#include
int sum_of_digits(int n)
{
int sum = 0;
while (n > 0)
{
sum += n % 10;
n = n/10;
}
return sum;
}
int main()
{
int n, s;
printf("Please enter a non-negative integer number\n");
scanf("%d", &n);
if(n < 0){
printf("The number must be non-negative!\n");
exit( -1);
}
s = sum_of_digits(n);
printf("The sum of digits of %d is %d\n", n, s);
return 0;
}
పైన ఇచ్చిన ప్రోగ్రాంను రన్ చేసినప్పుడు కంప్యూటరు మీద ఇలా ఉంటుంది.
Example 2: ఒక function ఒక విద్యార్థి మార్కులను తీసుకుని గ్రేడ్ రిటర్న్ చేయాలి. అలాగే, వేరొక function విద్యార్థి గ్రేడ్ తీసుకొని గ్రేడ్ పాయింట్లను రిటర్న్ చేయాలి. main() ప్రోగ్రాం పై రెండింటిని call చేయాలి.
ఎన్ని మార్కులు వస్తే ఏ గ్రేడ్ ఇవ్వాలి? అనేది character variables అనే పాఠంలో రాసిందే ఇక్కడ కూడా వాడుతున్నాం.
#include
char Grade(int n) /* Takes an integer and returns a character */
{
return(‘A’ + (4 - n/20) +( n ==100) );
}
int Points(char g) /* Takes a character and returns integer */
{
return(2 * ( 70-g) );
}
int main()
{
int m, p;
char v;
printf(“Enter a student mark\n”);
scanf(“%d”, &m);
v = Grade(m);/* function call*/
p = Points(v); /* function call*/
printf(“Marks=%d Grade=%c Points=%d\n”, m, v, p);
return (0);
}
పైన ఇచ్చిన ప్రోగ్రాంను రన్ చేసినప్పుడు కంప్యూటరు మీద ఇలా ఉంటుంది.
Example 3: ఏదైనా function ఒక character తీసుకొని అది ‘Vowel’ అయితే 1 లేకుంటే 0 రిటర్న్ చేయాలి. ఇక్కడ రెండు విధాలుగా రాశాం. అలాగే main program లో ఒక string ను రీడ్ చేసి దానిలో ఎన్ని ‘Vowels’ ఉన్నాయో పై functionతో కనుక్కొని ప్రింట్ చేస్తాం.
#include
#include
/* Takes a character and returns 1 if it is a vowel. Else returns 0 */
int Isvowel(char V)
{
V = toupper(V);
return((V==‘A’) || (V==‘E’) || (V==‘I’) || (V==‘O’) || (V==‘U’));
}
(లేదా)
int I is vowel(char V)
{
V = toupper(V);
switch(V)
{
case ‘A’ :
case ‘E’ :
case ‘I’ :
case ‘O’ :
case ‘U’ : return(1);
default : return(0);
}
}
int main()
{
int i, n = 0;
char x[20];
printf(“Enter a string\n”);
scanf(“%s”, x);
for(i = 0; x[i] != ‘\0’; i++)
if(I is vowel(x[i])) n++;
/* Here, we are x[i], ith character of the string x to check
whether it is vowel or not. If vowel, n value is
incremented. Thus, n value is number of vowels. */
printf(“No of Vowels in the given string=%d\n”, n);
return (0);
}
పైన ఇచ్చిన ప్రోగ్రాంను రన్ చేసినప్పుడు కంప్యూటరు మీద ఇలా ఉంటుంది.
NB Venkateswarlu
M.Tech(IIT-Kanpur)
Ph.D (BITS),PDF(UK),
Sr.Prof., CSE, AITAM, Tekkali