Top Banner
Functions in C A brief study
21
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Functions16.12

Functions in CA brief study

Page 2: Functions16.12

Functions

• Function is a self-contained block or a sub-program of one or more statements that performs a special task when called.

Page 3: Functions16.12

Example of functions

• Hotel management

– Front Office

– Reservation

– Housekeeping

– Telephone

Page 4: Functions16.12

Hotel management

Page 5: Functions16.12

Hotel

Customer()

{

House_keeping(); //Function call

}

House_keeping() //Function definition

{

Cleans rooms;

}

Page 6: Functions16.12

Hotel

Customer() //calling function

{

House_keeping(); //Function call

}

House_keeping() //called function

{

Cleans rooms;

}

Page 7: Functions16.12

Add two numbersmain()

{

int a,b;

int c;

printf(“Enter the value of a”);

scanf(“%d”,&a);

printf(“Enter the value of b”);

scanf(“%d”,&b);

c=a+b;

printf(“Answer is:%d”,c);

}

Page 8: Functions16.12

Add two numbers using functionsmain()

{ add(x,y)

int a,b; {

int c; z=x+y;

printf(“Enter the value of a”); printf(“%d”,z);

scanf(“%d”,&a); }

printf(“Enter the value of b”);

scanf(“%d”,&b);

add(a,b);

}

Page 9: Functions16.12

Arguments/Parameters

• Arguments are used mostly in functions.

• it can be any input parameter which a function can use to do it's work.

• Example: sin(x) sin is a function

x is it's argument.

Page 10: Functions16.12

Arguments/Parameters

• Actual arguments:

• Arguments of calling function

• Formal arguments:

• Arguments of called function

Page 11: Functions16.12

Add two numbers using functionsmain() formal arguments

{ add(x,y)

int a,b; {

int c; z=x+y;

printf(“Enter the value of a”); printf(“%d”,z);

scanf(“%d”,&a); }

printf(“Enter the value of b”);

scanf(“%d”,&b);

add(a,b); //function call

} actual arguments

Page 12: Functions16.12

Argument/Parameter list

a=2

b=4

add(a,b);

add(x,y)

x=2

y=4

Page 13: Functions16.12

return statement

• function uses return statement to return the value to the called function.

• Exit from the called function.

return(expression);

Page 14: Functions16.12

Hotel

Customer() //calling function

{

House_keeping(); //Function call

}

House_keeping() //called function

{

Cleans rooms;

return 0;

}

Page 15: Functions16.12

Hotel

Customer() //calling function

{

Front_office(Money); //Function call

}

Front_office(Money) //called function

{

return receipt;

}

Page 16: Functions16.12

Types of functions

1. No arguments and no return type

2. No arguments and return type

3. With arguments and no return type

4. With arguments and return type

Page 17: Functions16.12

Passing arguments

• The arguments passed to function can be of two types.

1. Values passed – Call by value

2. Address passed – Call by reference

Page 18: Functions16.12

Call by value• main()

{ int x=50, y=70; add(x,y); }

add(int x1,int y1) { int z1; z1=x1+y1; printf(“%d”,z1); }

Page 19: Functions16.12

Call by reference• main()

{ int x=50, y=70; add(&x,&y); }

add(int *x1,int *y1) { int z1; z1=x1+y1; printf(“%d”,z1); }

Page 20: Functions16.12

Call by reference

• Address is passed using symbol ‘&’

value is accessed using symbol ‘*’

x=50 &x=2000 *x=50

y=70 &y=2008 *y=70

Page 21: Functions16.12

Thank you