C Prog - Functions

1,408 views

Published on

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,408
On SlideShare
0
From Embeds
0
Number of Embeds
225
Actions
Shares
0
Downloads
154
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

C Prog - Functions

  1. 1. C Programming - FunctionsOrganized By: Vinay Arora Assistant Professor, CSED Thapar University, Patiala
  2. 2. Main program & its sub parts Function is also known as sub-program or module. Function is a self contained block of statements that perform coherent task of some kind. Vinay Arora CSED
  3. 3. Program-1 #include<stdio.h> #include<conio.h> void main() { clrscr(); printf("n Message for You"); printf("n Department of Civil Engineering"); getch(); } Vinay Arora CSED
  4. 4. Program-1 (output) Vinay Arora CSED
  5. 5. Program-2 #include<stdio.h> #include<conio.h> void main() { clrscr(); message(); printf("n Department of Civil Engineering"); getch(); } message() { printf("n Message for You"); } Vinay Arora CSED
  6. 6. Program-2 (output) Vinay Arora CSED
  7. 7. Program-3 #include<stdio.h> #include<conio.h> void message(); void main() { clrscr(); message(); printf("n Department of Civil Engineering"); getch(); } void message() { printf("n Message for You"); } Vinay Arora CSED
  8. 8. Program-3 (output) Vinay Arora CSED
  9. 9. Program-4 #include<stdio.h> void civil() #include<conio.h> { printf("nCivil Engg."); void civil(); } void computer(); void electronics(); void computer() { void main() printf("nComputer Engg."); { } clrscr(); void electronics() civil(); { computer(); printf("nElectronics Engg."); electronics(); } printf("nnDepartments at Thapar"); getch(); } Vinay Arora CSED
  10. 10. Program-4 #include<stdio.h> void civil() #include<conio.h> { printf("nCivil Engg."); void civil(); } void computer(); void electronics(); void computer() { void main() printf("nComputer Engg."); { } clrscr(); void electronics() civil(); { computer(); printf("nElectronics Engg."); electronics(); } printf("nnDepartments at Thapar"); getch(); } Vinay Arora CSED
  11. 11. Program-4 (output) Vinay Arora CSED
  12. 12. Program-5 #include<stdio.h> void civil() #include<conio.h> { printf("nCivil Engg."); void civil(); computer(); void computer(); } void electronics(); void computer() void main() { { printf("nComputer Engg."); clrscr(); electronics(); } civil(); void electronics() printf("nnDepartments at Thapar"); { printf("nElectronics Engg."); getch(); } } Vinay Arora CSED
  13. 13. Program-5 #include<stdio.h> void civil() #include<conio.h> { printf("nCivil Engg."); void civil(); computer(); void computer(); } void electronics(); void computer() void main() { { printf("nComputer Engg."); clrscr(); electronics(); } civil(); void electronics() printf("nnDepartments at Thapar"); { printf("nElectronics Engg."); getch(); } } Vinay Arora CSED
  14. 14. Program-5 (output) Vinay Arora CSED
  15. 15. Program-6 #include<stdio.h> void civil() #include<conio.h> { printf("nCivil Engg."); void civil(); } void main() { clrscr(); civil(); civil(); printf("nnDepartments at Thapar"); getch(); } Vinay Arora CSED
  16. 16. Program-6 #include<stdio.h> void civil() #include<conio.h> { printf("nCivil Engg."); void civil(); } void main() { clrscr(); civil(); civil(); printf("nnDepartments at Thapar"); getch(); } Vinay Arora CSED
  17. 17. Program-6 (output) Vinay Arora CSED
  18. 18. Program-7 #include<stdio.h> #include<conio.h> void main() { int a,b,c; clrscr(); printf("Enter Numbers for Additionn"); printf("Enter 1st Number:t"); scanf("%d",&a); printf("Enter 2nd Number:t"); scanf("%d",&b); c=a+b; printf("Result after Addition is:%d",c); getch(); } Vinay Arora CSED
  19. 19. Program-7 (output) Vinay Arora CSED
  20. 20. Program-8 #include<stdio.h> void sum() #include<conio.h> { int a,b,c; void sum(); printf("Enter Numbers for Additionn"); void main() printf("Enter 1st Number:t"); { scanf("%d",&a); clrscr(); printf("Enter 2nd Number:t"); sum(); scanf("%d",&b); getch(); } c=a+b; printf("Result after Addition is:%d",c); } Vinay Arora CSED
  21. 21. Program-8 (output) Vinay Arora CSED
  22. 22. Program-9 #include<stdio.h> void sum(int x,int y) #include<conio.h> { int z; void sum(); z=x+y; void main() printf("Result after Addition is:%d",z); { } int a,b,c; clrscr(); printf("Enter Numbers for Additionn"); printf("Enter 1st Number:t"); scanf("%d",&a); printf("Enter 2nd Number:t"); scanf("%d",&b); sum(a,b); getch(); } Vinay Arora CSED
  23. 23. Program-9 (output) Vinay Arora CSED
  24. 24. Program-10 #include<stdio.h> int sum(int x,int y) #include<conio.h> { int z; int sum(); z=x+y; void main() return (z); { } int a,b,c; clrscr(); printf("Enter Numbers for Additionn"); printf("Enter 1st Number:t"); scanf("%d",&a); printf("Enter 2nd Number:t"); scanf("%d",&b); c=sum(a,b); printf("Result after Addition is:%d",c); getch(); } Vinay Arora CSED
  25. 25. Program-10 (output) Vinay Arora CSED
  26. 26. Program-11 #include<stdio.h> int increment(int x) #include<conio.h> { x++; int increment(); return (x); void main() } { int a,b,c; clrscr(); printf("Enter Numbert"); scanf("%d",&a); c=increment(a); printf("Result after Increment will be:%d",c); getch(); } Vinay Arora CSED
  27. 27. Program-11 (output) Vinay Arora CSED
  28. 28. Program-12 #include<stdio.h> #include<conio.h> void display(); void main() { int a=20; clrscr(); display(a); getch(); } void display(int x) { int y=27; printf("Value passed from main function is %dn",x); printf("Value present in display function is %d",y); } Vinay Arora CSED
  29. 29. Program-12 (output) Vinay Arora CSED
  30. 30. Program-13 #include<stdio.h> void display() #include<conio.h> { int x=25; void display(); void main() printf("Value of x in display function is %d",x); { } int x=20; clrscr(); printf("Value of x in main function %dn",x); display(); getch(); } Vinay Arora CSED
  31. 31. Program-13 (output) Vinay Arora CSED
  32. 32. Program-14 #include<stdio.h> void display(int x) #include<conio.h> { x=77; void display(); void main() printf("Value of x in display function is %d",x); { } int x=20; clrscr(); printf("Value of x in main function %dn",x); display(); getch(); } Vinay Arora CSED
  33. 33. Program-14 (output) Vinay Arora CSED
  34. 34. Program-15 #include<stdio.h> int increment(int a) #include<conio.h> { a++; void display(); return (a); void main() } { int a,b; int decrement(int a) clrscr(); { a--; printf("Enter Numbert"); return (a); scanf("%d",&a); } b=increment(a); printf("nValue after increment is %dt",b); b=decrement(a); printf("nValue after decrement is %dt",b); getch(); } Vinay Arora CSED
  35. 35. Program-15 (output) Vinay Arora CSED
  36. 36. Program-17 #include<stdio.h> #include<conio.h> int increment(); void main() { int i=10,j=20,k=30; clrscr(); printf("%dt%dt%dn",i,j,k); printf("%dt%dt%dn",i,j); printf("%dn",i,j); getch(); } Vinay Arora CSED
  37. 37. Program-17 (output) Vinay Arora CSED
  38. 38. Program-18 #include<stdio.h> float square(float x) #include<conio.h> { float y; float square(float); y=x*x; void main() return(y); { } float a,b; clrscr(); printf("Enter any Number:"); scanf("%f",&a); b=square(a); printf("Square of number entered is %ft",b); getch(); } Vinay Arora CSED
  39. 39. Program-18 (output) Vinay Arora CSED
  40. 40. Program-19 #include<stdio.h> int factorial(int x) #include<conio.h> { int f=1,i; void main() { for(i=x;i>=1;i--) int a,fact; f=f*i; clrscr(); return(f); printf("Enter any Number:"); } scanf("%d",&a); fact=factorial(a); printf("Factorial Value is = %dn",fact); getch(); } Vinay Arora CSED
  41. 41. Program-19 (output) Vinay Arora CSED
  42. 42. Program-20 (Recursion in C) #include<stdio.h> int rec(int x) #include<conio.h> { int f; int rec(int); void main() if(x==1) { return(1); int a,fact; else clrscr(); f=x*rec(x-1); printf("Enter any Number:"); return(f); scanf("%d",&a); } fact=rec(a); printf("Factorial Value is = %dn",fact); getch(); } Vinay Arora CSED
  43. 43. Recursion Vinay Arora CSED
  44. 44. Program-20 (output) Vinay Arora CSED
  45. 45. Program-21 #include<stdio.h> #include<conio.h> void main() { int i; clrscr(); for(i=0;i<=50000;i++) printf("%dn",i); getch(); } Vinay Arora CSED
  46. 46. Program-21 (output) Vinay Arora CSED
  47. 47. Program-22 #include<stdio.h> void val() #include<conio.h> { i=100; int i=0; printf("Vals i=%dn",i); void val(); i++; } void main() { clrscr(); printf("Mains i=%dn",i); i++; val(); printf("Mains i=%dn",i); val(); getch(); } Vinay Arora CSED
  48. 48. Program-22 (output) Vinay Arora CSED
  49. 49. Program-23 #include<stdio.h> #include<conio.h> void main() { int count=5; clrscr(); printf("Count=%dn",count); count--; if(count>0) main(); else exit(); getch(); } Vinay Arora CSED
  50. 50. Program-23 (output) INFINITE LOOP Vinay Arora CSED
  51. 51. Program-24 #include<stdio.h> int g(int x) #include<conio.h> { int v=1; int g(int); int b=3; void main() { v+=x; int i,j; return(v+x+b); clrscr(); } for(i=1;i<5;i++) { j=g(i); printf("%dn",j); } getch(); } Vinay Arora CSED
  52. 52. Program-24 (output) Vinay Arora CSED
  53. 53. Program-25 #include<stdio.h> #include<conio.h> int x=10; void main() { int x=20; clrscr(); { int x=30; printf("%dn",x); } printf("%dn",x); getch(); } Vinay Arora CSED
  54. 54. Program-25 (output) Vinay Arora CSED
  55. 55. Program-26 #include<stdio.h> void func() #include<conio.h> { int i,j,k; void func(); i=j=k=0; void main() { i++;j++;k++; clrscr(); printf("%d%d%dn",i,j,k); func(); } func(); getch(); } Vinay Arora CSED
  56. 56. Program-26 (output) Vinay Arora CSED
  57. 57. Thnx… Vinay Arora CSED

×