Functions in C

360 views

Published on

Study notes on Functions

Published in: Education, Business, Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
360
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
20
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Functions in C

  1. 1. Functions in C A brief study
  2. 2. Functions• Function is a self-contained block or a sub- program of one or more statements that performs a special task when called.
  3. 3. Example of functions• Hotel management – Front Office – Reservation – Housekeeping – Telephone
  4. 4. Hotel management
  5. 5. HotelCustomer() { House_keeping(); //Function call }House_keeping() //Function definition { Cleans rooms; }
  6. 6. HotelCustomer() //calling function { House_keeping(); //Function call }House_keeping() //called function { Cleans rooms; }
  7. 7. 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);}
  8. 8. 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);}
  9. 9. Arguments/Parameters• Arguments are used mostly in functions.• it can be any input parameter which a function can use to do its work.• Example: sin(x) sin is a function x is its argument.
  10. 10. Arguments/Parameters• Actual arguments: • Arguments of calling function• Formal arguments: • Arguments of called function
  11. 11. 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
  12. 12. Argument/Parameter lista=2b=4 add(a,b); add(x,y) x=2 y=4
  13. 13. return statement• function uses return statement to return the value to the called function.• Exit from the called function. return(expression);
  14. 14. HotelCustomer() //calling function { House_keeping(); //Function call }House_keeping() //called function { Cleans rooms; return 0; }
  15. 15. HotelCustomer() //calling function { Front_office(Money); //Function call }Front_office(Money) //called function { return receipt; }
  16. 16. Types of functions1. No arguments and no return type2. No arguments and return type3. With arguments and no return type4. With arguments and return type
  17. 17. Passing arguments• The arguments passed to function can be of two types. 1. Values passed – Call by value 2. Address passed – Call by reference
  18. 18. 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); }
  19. 19. 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); }
  20. 20. 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
  21. 21. Thank you

×