Sunteți pe pagina 1din 15

Functions

P. P. Chakrabarti

1 21-01-03 P.P.Chakrabarti, IIT Kharagpur


Query on [ppchak]$ ./a.out

switch: R
Red
[ppchak]$ ./a.out
int main() B
{ char choice; Blue
switch (choice = getchar()) { [ppchak]$ ./a.out
case ‘r’ : r
case ‘R’: printf(“Red\n”); Red
break; [ppchak]$ ./a.out
case ‘b’ : printf(“small b\n”); b
case ‘B’ : printf(“Blue\n”);
small b
break;
case ‘g’ : Blue
case ‘G’: printf(“Green\n”); [ppchak]$ ./a.out
break; k
default: printf(“Black\n”); Black
}}

2 21-01-03 P.P.Chakrabarti, IIT Kharagpur


Library functions
#include<math.h> How many of them are
int main() here?
{ float x,y,z;
scanf(“%f”, &x);
printf(“tan(asine(%f/2) = %f \n”, x, tan(asine(x/2.0)));
scanf(“%f%f%f”, &x,&y,&z);
printf(“pow(%f, pow(%f, pow(%f, 1.0/3.0))) = %f\n”,
x,y,z, pow(x, pow(y, pow (z, 1.0/3.0))));
}
compile with –lm option

3 21-01-03 P.P.Chakrabarti, IIT Kharagpur


User defined functions: Ex 1
main() float cent2fahr(float data)
{ float cent, fahr; {
scanf(“%f”,&cent); float result;
fahr = cent2fahr(cent); result = data*9/5 + 32;
printf(“%fC = %fF\n”, return result;
cent, fahr); }
}

4 21-01-03 P.P.Chakrabarti, IIT Kharagpur


How it runs:
float cent2fahr(float data) [ppchak]$ gcc test20.c
{ [ppchak]$ ./a.out
float result; 32
printf(“data = %f\n”, data); Input is 32.000000
result = data*9/5 + 32;
data = 32.000000
return result;
32.000000C = 89.599998F
printf(“result = %f\n”, result);
} [ppchak]$./a.out
main() -45.6
{ float cent, fahr; Input is -45.599998
scanf(“%f”,&cent); data = -45.599998
printf(“Input is %f\n”, cent); -45.599998C = -50.079998F
fahr = cent2fahr(cent); [ppchak]$
printf(“%fC = %fF\n”, cent, fahr);
}
5 21-01-03 P.P.Chakrabarti, IIT Kharagpur
Function Control Flow
/* print banner line */
main ()
void print_banner () {
{ print_banner {
printf(“**************\n”);
print_banner (); }
}
print_banner {
int main () print_banner ();
{ }
...
}
print_banner ();
...
print_banner ();

}
6 21-01-03 P.P.Chakrabarti, IIT Kharagpur
Function Type and Value
? A function can return a value.
? Like all values in C, a function return value has a type.
? The function has the type of its returned value.
function type
/* Get number from user */
int get_input (void)
{
int num;
printf (“Enter a number:”);
scanf (“%d”, &num);
return (num); return statement
}
returned value
7 21-01-03 P.P.Chakrabarti, IIT Kharagpur
Calling a function
? A value-returning function is called by
including it in an expression.
int main () Note : A value returning
{ function can be used
int x, y; anywhere an expression of
x = get_input() ; the same type can be used.
y = get_input() ;
printf (“ %d + %d = %d\n”,
x, y, x+y);
}

8 21-01-03 P.P.Chakrabarti, IIT Kharagpur


return
? In a value-returning function (result type is not void)
return does two distinct things :
? 1. specify the value returned by the execution of the
function
? 2. terminate that execution of the function.
? In a void function:
? return is optional at the end of the function body.
? return may also be used to terminate execution of the
function explicitly.
? No return value should appear following return.

9 21-01-03 P.P.Chakrabarti, IIT Kharagpur


void compute_and_print_itax ()
{
double income;
scanf (“%f”, &income);
Terminate function
if (income < 50000) {
execution before
printf (“Income tax = Nil\n”);
reaching the end
return;
}
if (income < 60000) {
printf (“Income tax = %f\n”, 0.1*(income-50000));
return;
}
if (income < 150000) {
printf (“Income tax = %f\n”, 0.2*(income-60000)+1000);
return ;
}
printf (“Income tax = %f\n”, 0.3*(income-150000)+19000);
}
10 21-01-03 P.P.Chakrabarti, IIT Kharagpur
Function parameters
? It is very often useful if a function can operate
on different data values each time it is called.
Such values are function (input) parameters.
? The function specifies its inputs as parameters
in the function declaration.
/* Find area of a circle with radius r */
double area (double r) parameter
{
return (3.14159*r*r) ;
}
11 21-01-03 P.P.Chakrabarti, IIT Kharagpur
Arguments
? The function call must include a matching
argument for each parameter.
? When the function is executed, the value of the
argument is substituted for the parameter.
parameter passing
int main (void)
{ ... double area (double r)
double circum; {
... return (3.14*r*r);
area1 = area(circum/2.0);
}
...
}
12 21-01-03 P.P.Chakrabarti, IIT Kharagpur
Another Example
main() int prime(int x)
{ {
int numb, flag, j=3; int i, test;
scanf(“%d”,&numb); i=2, test =0;
while (j <=numb) while ((i <= sqrt(x)) && (test
{ ==0))
flag = prime(j); { if (x%i==0) test = 1;
if (flag==0) printf(“%d is i++;
prime\n”,j); }
j++; return test;
} }
}

13 21-01-03 P.P.Chakrabarti, IIT Kharagpur


Tracking the flow of control
main() int prime(int x)
{ {
int numb, flag, j=3; int i, test;
scanf(“%d”,&numb);
i=2, test =0;
printf(“numb = %d \n”,numb);
while (j <=numb) printf(“In function, x = %d \n”,x);
{ printf(“Main, j = %d\n”,j); while ((i <= sqrt(x)) && (test ==0))
flag = prime(j); { if (x%i==0) test = 1;
printf(“Main, flag = %d\n”,flag); i++;
if (flag==0) printf(“%d is }
prime\n”,j); printf(“Returning, test = %d \n”,test);
j++; return test;
}
}
}

14 21-01-03 P.P.Chakrabarti, IIT Kharagpur


The run
[ppchak]$ gcc -lm test22.c Returning, test = 1
[ppchak]$ ./a.out Main, flag = 1
5 Main, j = 5
numb = 5 In function, x = 5
Main, j = 3
Returning, test = 0
In function, x = 3
Main, flag = 0
Returning, test = 0
5 is prime
Main, flag = 0
3 is prime [ppchak]$
Main, j = 4
In function, x = 4

15 21-01-03 P.P.Chakrabarti, IIT Kharagpur

S-ar putea să vă placă și