Advertisement
XRahat2011

C++ ClassRoom -- Lecture No. 2

Dec 2nd, 2012
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.54 KB | None | 0 0
  1. OK!
  2.  
  3. Using functions we can structure our programs in a more modular way, accessing all the potential that structured programming can offer to us in C++ ;).
  4. the structure of functions are like this:
  5. name(parameter1, parameter2, ......) {
  6. ....
  7. }
  8. and in brackets you put the type (int, void, float, etc...)
  9. an example is:
  10.  
  11. void my_func(int a, int b);
  12.  
  13. So, there
  14. I declare the function as void
  15. and the parameters
  16. as "int".
  17.  
  18. Code:
  19.  
  20. type name(parameter1, parameter2, ...) {
  21. //statements
  22. }
  23.  
  24. the structure:
  25. type: is the data type specifier of the data returned by the function.
  26. name: is the random name that we give ;).
  27. parameters: each parameter consists of a data type specifier followed by an identifier, like any regular variable declaration (for example: int x) and which acts within the function as a regular local variable. They allow to pass arguments to the function when it is called. The different parameters are separated by commas.
  28. statements: is the body...
  29.  
  30. lets give an example now:
  31.  
  32. #include<iostream>
  33.  
  34. using namespace std;
  35.  
  36. void my_func(int a,int b) {
  37.     int c;
  38.     c=a+b;
  39.  
  40.     cout<<c<<endl;
  41. }
  42.  
  43. int main() {
  44. my_func(1,2);
  45. my_func(2,7);
  46. }
  47.  
  48. here is an example
  49. now we can do the same thing
  50. using return;
  51. but if we use return we can not use
  52. void
  53. so the program will become like this:
  54.  
  55. #include<iostream>
  56.  
  57. using namespace std;
  58.  
  59. int my_func(int a,int b) {
  60.     int c;
  61.     c=a+b;
  62.  
  63.     cout<<endl;
  64.  
  65.     return c;
  66. }
  67.  
  68. int main() {
  69. cout<<my_func(1,2);
  70. cout<<my_func(2,7);
  71. }
  72.  
  73. Now I will tell about functions by reference.
  74.  
  75. Some Code:
  76.  
  77. #include<iostream>
  78.  
  79. using namespace std;
  80.  
  81. void my_func(int a,int b) {
  82.    a=5;
  83.    b=6;
  84. }
  85.  
  86. int main() {
  87. int x=1,y=2;
  88. my_func(x,y);
  89. cout << x << endl;
  90. cout << y << endl;
  91. }
  92.  
  93.  
  94. if you run this program
  95. it will print:
  96. 1
  97. 2
  98. So, the question should be:
  99. why x and y don't take the value 5 and 6?
  100. The answer is:
  101. in functions we can not change the parameters. Instead we use reference operator to change parameters ;).
  102. Reference is used with an & (ampersand) sign.
  103. So, the parameters should be:
  104. "int &a,int &b"
  105. now run this:
  106.  
  107. #include<iostream>
  108.  
  109. using namespace std;
  110.  
  111. void my_func(int &a,int &b) {
  112.   a=5;
  113.   b=6;
  114. }
  115.  
  116. int main() {
  117. int x=1,y=2;
  118. my_func(x,y);
  119. cout << x << endl;
  120. cout << y << endl;
  121. }
  122.  
  123.  
  124. it will print:
  125. 5
  126. 6
  127.  
  128. Here I used reference to the parameters but we can do the same thing using pointers to the parameters ;).
  129.  
  130. So, theoretically "pointers" are same as "reference operators" ;).
  131.  
  132. Now run this:
  133.  
  134. #include<iostream>
  135.  
  136. using namespace std;
  137.  
  138. void my_func(int *a,int *b) {
  139.   *a=5;
  140.   *b=6;
  141. }
  142.  
  143. int main() {
  144. int x=1,y=2;
  145. my_func(&x,&y);
  146. cout << x << endl;
  147. cout << y << endl;
  148. }
  149.  
  150. So, it will print:
  151. 5
  152. 6
  153.  
  154. So, both results are same but just the methods are different ;).
  155.  
  156. ------------
  157. Break Starts
  158. ------------
  159. --
  160. ----------
  161. Break Ends
  162. ----------
  163.  
  164. Also to make it more advance, we can give the value of the function in a pointer.
  165. For example,
  166.  
  167. #include<iostream>
  168.  
  169. using namespace std;
  170.  
  171. int my_func(int *a,int *b) {
  172.   *a=5;
  173.   *b=6;
  174. }
  175.  
  176. int main() {
  177. int x=1,y=2;
  178. int (*g)(int *a,int *b);
  179. g=my_func;
  180. g(&x,&y);
  181. cout << x << endl;
  182. cout << y << endl;
  183. }
  184.  
  185. This is a program
  186. that makes a pointer,
  187. adds into the pointer
  188. the value of the function
  189. and after calling the function
  190. through the pointer,
  191. prints the variables.
  192.  
  193. -- Lecture Ended --
  194.  
  195. -- Note --
  196. In the next lesson we can learn about pointers (i.e. hopefully tomorrow) ;).
  197.  
  198. ** The End ;)
  199. ** Teacher = "Hepic" ;).
  200. ** Class Moderated By "XRahat2011" ;).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement