Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- OK!
- 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++ ;).
- the structure of functions are like this:
- name(parameter1, parameter2, ......) {
- ....
- }
- and in brackets you put the type (int, void, float, etc...)
- an example is:
- void my_func(int a, int b);
- So, there
- I declare the function as void
- and the parameters
- as "int".
- Code:
- type name(parameter1, parameter2, ...) {
- //statements
- }
- the structure:
- type: is the data type specifier of the data returned by the function.
- name: is the random name that we give ;).
- 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.
- statements: is the body...
- lets give an example now:
- #include<iostream>
- using namespace std;
- void my_func(int a,int b) {
- int c;
- c=a+b;
- cout<<c<<endl;
- }
- int main() {
- my_func(1,2);
- my_func(2,7);
- }
- here is an example
- now we can do the same thing
- using return;
- but if we use return we can not use
- void
- so the program will become like this:
- #include<iostream>
- using namespace std;
- int my_func(int a,int b) {
- int c;
- c=a+b;
- cout<<endl;
- return c;
- }
- int main() {
- cout<<my_func(1,2);
- cout<<my_func(2,7);
- }
- Now I will tell about functions by reference.
- Some Code:
- #include<iostream>
- using namespace std;
- void my_func(int a,int b) {
- a=5;
- b=6;
- }
- int main() {
- int x=1,y=2;
- my_func(x,y);
- cout << x << endl;
- cout << y << endl;
- }
- if you run this program
- it will print:
- 1
- 2
- So, the question should be:
- why x and y don't take the value 5 and 6?
- The answer is:
- in functions we can not change the parameters. Instead we use reference operator to change parameters ;).
- Reference is used with an & (ampersand) sign.
- So, the parameters should be:
- "int &a,int &b"
- now run this:
- #include<iostream>
- using namespace std;
- void my_func(int &a,int &b) {
- a=5;
- b=6;
- }
- int main() {
- int x=1,y=2;
- my_func(x,y);
- cout << x << endl;
- cout << y << endl;
- }
- it will print:
- 5
- 6
- Here I used reference to the parameters but we can do the same thing using pointers to the parameters ;).
- So, theoretically "pointers" are same as "reference operators" ;).
- Now run this:
- #include<iostream>
- using namespace std;
- void my_func(int *a,int *b) {
- *a=5;
- *b=6;
- }
- int main() {
- int x=1,y=2;
- my_func(&x,&y);
- cout << x << endl;
- cout << y << endl;
- }
- So, it will print:
- 5
- 6
- So, both results are same but just the methods are different ;).
- ------------
- Break Starts
- ------------
- --
- ----------
- Break Ends
- ----------
- Also to make it more advance, we can give the value of the function in a pointer.
- For example,
- #include<iostream>
- using namespace std;
- int my_func(int *a,int *b) {
- *a=5;
- *b=6;
- }
- int main() {
- int x=1,y=2;
- int (*g)(int *a,int *b);
- g=my_func;
- g(&x,&y);
- cout << x << endl;
- cout << y << endl;
- }
- This is a program
- that makes a pointer,
- adds into the pointer
- the value of the function
- and after calling the function
- through the pointer,
- prints the variables.
- -- Lecture Ended --
- -- Note --
- In the next lesson we can learn about pointers (i.e. hopefully tomorrow) ;).
- ** The End ;)
- ** Teacher = "Hepic" ;).
- ** Class Moderated By "XRahat2011" ;).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement