Af Functions [21_01]

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

Transcript of Af Functions [21_01]

Page 1: Af Functions [21_01]

8/8/2019 Af Functions [21_01]

http://slidepdf.com/reader/full/af-functions-2101 1/15

21-01-03 P.P.Chakrabarti, IIT Kharagpur1

Functions

P. P. Chakrabarti

Page 2: Af Functions [21_01]

8/8/2019 Af Functions [21_01]

http://slidepdf.com/reader/full/af-functions-2101 2/15

21-01-03 P.P.Chakrabarti, IIT Kharagpur2

Query onswitch:

int main(){ char choice;

switch (choice = getchar()) {

case ‘r’ :case ‘R’: printf(“Red\n”);break;

case ‘b’ : printf(“small b\n”);case ‘B’ : printf(“Blue\n”);

break;case ‘g’ :case ‘G’: printf(“Green\n”);

break;default: printf(“Black\n”);

}}

[ppchak]$ ./a.outR

Red[ppchak]$ ./a.outBBlue[ppchak]$ ./a.out

rRed[ppchak]$ ./a.outbsmall b

Blue[ppchak]$ ./a.outkBlack

Page 3: Af Functions [21_01]

8/8/2019 Af Functions [21_01]

http://slidepdf.com/reader/full/af-functions-2101 3/15

21-01-03 P.P.Chakrabarti, IIT Kharagpur3

Library functions

#include<math.h>int main()

{ 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))));}

How many of them arehere?

compile with –lm option

Page 4: Af Functions [21_01]

8/8/2019 Af Functions [21_01]

http://slidepdf.com/reader/full/af-functions-2101 4/15

21-01-03 P.P.Chakrabarti, IIT Kharagpur4

User defined functions: Ex 1

main(){ float cent, fahr;

scanf(“%f”,&cent);fahr = cent2fahr(cent);printf(“%fC = %fF\n”,

cent, fahr);}

float cent2fahr(float data){

float result;result = data*9/5 + 32;return result;

}

Page 5: Af Functions [21_01]

8/8/2019 Af Functions [21_01]

http://slidepdf.com/reader/full/af-functions-2101 5/15

21-01-03 P.P.Chakrabarti, IIT Kharagpur5

How it runs:float cent2fahr(float data){

float result;

printf(“data = %f\n”, data);result = data*9/5 + 32;return result;printf(“result = %f\n”, result);

}main(){ float cent, fahr;

scanf(“%f”,&cent);printf(“Input is %f\n”, cent);fahr = cent2fahr(cent);printf(“%fC = %fF\n”, cent, fahr);

}

[ppchak]$ gcc test20.c[ppchak]$ ./a.out32

Input is 32.000000data = 32.00000032.000000C = 89.599998F[ppchak]$./a.out-45.6Input is -45.599998data = -45.599998-45.599998C = -50.079998F[ppchak]$

Page 6: Af Functions [21_01]

8/8/2019 Af Functions [21_01]

http://slidepdf.com/reader/full/af-functions-2101 6/15

21-01-03 P.P.Chakrabarti, IIT Kharagpur6

Function Control Flow /* print banner line */ void print_banner (){

printf(“**************\n”);}

int main (){

. . .print_banner ();. . .print_banner ();

}

main (){

print_banner ();

print_banner ();

}

print_banner {

}

print_banner {

}

Page 7: Af Functions [21_01]

8/8/2019 Af Functions [21_01]

http://slidepdf.com/reader/full/af-functions-2101 7/15

21-01-03 P.P.Chakrabarti, IIT Kharagpur7

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.

/* Get number from user */ int get_input (void){

int num;printf (“Enter a number:”);scanf (“%d”, &num);return (num );

}

function type

return statement

returned value

Page 8: Af Functions [21_01]

8/8/2019 Af Functions [21_01]

http://slidepdf.com/reader/full/af-functions-2101 8/15

21-01-03 P.P.Chakrabarti, IIT Kharagpur8

Calling a function

? A value-returning function is called byincluding it in an expression.

int main (){

int x, y;x = get_input() ;y = get_input() ;printf (“ %d + %d = %d\n”,

x, y, x+y);}

Note : A value returningfunction can be usedanywhere an expression of the same type can be used.

Page 9: Af Functions [21_01]

8/8/2019 Af Functions [21_01]

http://slidepdf.com/reader/full/af-functions-2101 9/15

21-01-03 P.P.Chakrabarti, IIT Kharagpur9

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 thefunction? 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.

Page 10: Af Functions [21_01]

8/8/2019 Af Functions [21_01]

http://slidepdf.com/reader/full/af-functions-2101 10/15

21-01-03 P.P.Chakrabarti, IIT Kharagpur10

void compute_and_print_itax (){

double income;scanf (“%f”, &income);if (income < 50000) {

printf (“Income tax = Nil\n”);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);

}

Terminate functionexecution beforereaching the end

Page 11: Af Functions [21_01]

8/8/2019 Af Functions [21_01]

http://slidepdf.com/reader/full/af-functions-2101 11/15

21-01-03 P.P.Chakrabarti, IIT Kharagpur11

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 parametersin the function declaration.

/* Find area of a circle with radius r */ double area ( double r ){

return (3.14159*r*r) ;}

parameter

Page 12: Af Functions [21_01]

8/8/2019 Af Functions [21_01]

http://slidepdf.com/reader/full/af-functions-2101 12/15

21-01-03 P.P.Chakrabarti, IIT Kharagpur12

Arguments? The function call must include a matching

argument for each parameter.?

When the function is executed, the value of theargument is substituted for the parameter.

int main (void){ . . .

double circum;. . .area1 = area (circum/2.0 );. . .

}

double area ( double r ){

return (3.14*r*r);}

parameter passing

Page 13: Af Functions [21_01]

8/8/2019 Af Functions [21_01]

http://slidepdf.com/reader/full/af-functions-2101 13/15

21-01-03 P.P.Chakrabarti, IIT Kharagpur13

Another Example

main(){

int numb, flag, j=3;scanf(“%d”,&numb);while (j <=numb)

{flag = prime(j);

if (flag==0) printf(“%d isprime\n”,j); j++;}

}

int prime(int x){

int i, test;i=2, test =0;while ((i <= sqrt(x)) && (test==0))

{ if (x%i==0) test = 1;

i++;}return test;

}

Page 14: Af Functions [21_01]

8/8/2019 Af Functions [21_01]

http://slidepdf.com/reader/full/af-functions-2101 14/15

21-01-03 P.P.Chakrabarti, IIT Kharagpur14

Tracking the flow of controlmain(){

int numb, flag, j=3;scanf(“%d”,&numb);printf(“numb = %d \n”,numb);while (j <=numb){ printf(“Main, j = %d\n”,j);

flag = prime(j);printf(“Main, flag = %d\n”,flag);

if (flag==0) printf(“%d isprime\n”,j);

j++;}

}

int prime(int x){

int i, test;

i=2, test =0;printf(“In function, x = %d \n”,x);while ((i <= sqrt(x)) && (test ==0)){ if (x%i==0) test = 1;

i++;

}printf(“Returning, test = %d \n”,test);return test;

}

Page 15: Af Functions [21_01]

8/8/2019 Af Functions [21_01]

http://slidepdf.com/reader/full/af-functions-2101 15/15

21-01-03 P.P.Chakrabarti, IIT Kharagpur15

The run

[ppchak]$ gcc -lm test22.c[ppchak]$ ./a.out5numb = 5Main, j = 3In function, x = 3Returning, test = 0

Main, flag = 03 is primeMain, j = 4In function, x = 4

Returning, test = 1Main, flag = 1Main, j = 5In function, x = 5Returning, test = 0Main, flag = 05 is prime[ppchak]$