Difference between revisions of "C-and-C++/C2/Functions/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
Line 64: Line 64:
 
'''ret_type fun_name( )'''
 
'''ret_type fun_name( )'''
  
'''ret-type''' defines the type of data that the '''function''' returns
+
'''ret-type''' defines the type of data that the '''function''' returns.
  
'''fun_name''' defines the name of the '''function'''
+
'''fun_name''' defines the name of the '''function'''.
  
'''parameters''' is the list of '''variable''' names and their types
+
'''parameters''' is the list of '''variable''' names and their types.
  
 
|-
 
|-
 
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 6
 
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 6
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| We can specify an empty parameter list
+
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| We can specify an '''empty parameter list'''.
  
This is called as functions without arguments.
+
This is called as '''Bold text'''functions without arguments.
  
This is called as functions with arguments.
+
This is called as '''functions with arguments'''.
  
 
|-
 
|-
 
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
 
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us see a program using void  
+
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us see a program using '''void'''.
  
I have already typed the program on the editor
+
I have already typed the program on the editor.
  
So i will just open it
+
So I will just open it.
  
 
|-
 
|-
Line 90: Line 90:
  
 
'''function.c'''
 
'''function.c'''
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Please note that our filename is '''function'''
+
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Please note that our filename is '''function'''.
  
and I have saved the file with the extentsion '''.c'''
+
And I have saved the file with the extentsion '''.c'''
  
Let me explain the code now
+
Let me explain the code.
  
 
|-
 
|-
Line 100: Line 100:
  
 
'''<nowiki>#include <stdio.h></nowiki>'''
 
'''<nowiki>#include <stdio.h></nowiki>'''
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| This is our '''header file'''
+
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| This is our '''header file'''.
  
 
|-
 
|-
Line 106: Line 106:
  
 
void add()
 
void add()
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Before using any function it must be defined
+
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Before using any '''function''', it must be defined.
  
Here we have declared a''' function''' called '''add'''
+
Here we have declared a''' function''' called '''add'''.
  
Note that '''add function''' is without any '''arguments'''
+
Note that '''add function''' is without any '''arguments'''.
  
And the return type is''' void'''
+
And the return type is''' void'''.
  
 
|-
 
|-
 
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
 
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| There are two types of functions
+
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| There are two types of functions-
  
User-defined that is our add function
+
*'''User-defined''' that is our '''add function'''
  
Pre-defined that is printf and main function
+
*'''Pre-defined''' that is '''printf''' and '''main function'''
  
 
|-
 
|-
Line 128: Line 128:
  
 
int b = 3;  
 
int b = 3;  
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here we have initialized a and b by assigning them values as 2 and 3
+
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here we have initialized '''a''' and '''b''' by assigning them values as 2 and 3
  
 
|-
 
|-
Line 134: Line 134:
  
 
int c = a + b;
 
int c = a + b;
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here we have declared a variable '''c'''
+
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here we have declared a variable '''c'''.
  
Then we add the values of '''a''' and '''b'''
+
Then we add the values of '''a''' and '''b'''.
  
'''The result is stored in c'''
+
The result is stored in '''c'''.
  
 
|-
 
|-
Line 144: Line 144:
  
 
printf("Value of C is %d\n",c);
 
printf("Value of C is %d\n",c);
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Then we print the result
+
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Then we print the result.
  
 
|-
 
|-
Line 150: Line 150:
  
 
void main()
 
void main()
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| This is our main function
+
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| This is our''' main function'''.
  
 
|-
 
|-
Line 156: Line 156:
  
 
'''add();'''
 
'''add();'''
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here we call the add function
+
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here we call the '''add function'''.
  
 
The addition operation will be performed and the result will be printed.
 
The addition operation will be performed and the result will be printed.
Line 162: Line 162:
 
|-
 
|-
 
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
 
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now click on Save
+
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now click on '''Save'''.
  
Now let us execute the program
+
Now let us execute the program.
  
 
|-
 
|-
 
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Press Ctrl, Alt and T keys simultaneously
 
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Press Ctrl, Alt and T keys simultaneously
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Please open the terminal window by pressing Ctrl, Alt and T keys simultaneously
+
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Please open the '''terminal''' window by pressing''' Ctrl, Alt''' and '''T''' keys simultaneously.
  
 
|-
 
|-
Line 196: Line 196:
 
|-
 
|-
 
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| On the editor
 
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| On the editor
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now come back to our program
+
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now come back to our program.
  
Functions contains special identifiers called as parameters or arguments
+
Functions contains special identifiers called as '''parameters''' or '''arguments'''.
  
Let us see the same example with arguments
+
Let us see the same example with '''arguments'''.
  
 
|-
 
|-
Line 206: Line 206:
  
 
int add(int a, int b)
 
int add(int a, int b)
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| I will just change a few things here
+
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| I will just change a few things here.
  
 
Type  
 
Type  
  
int add(int a, int b)
+
'''int add(int a, int b)'''
  
Here we have declared a''' function''' called '''add'''
+
Here we have declared a''' function add'''.
  
'''int a''' and '''int b''' are the '''arguments''' of the '''function add'''
+
'''int a''' and '''int b''' are the '''arguments''' of the '''function add'''.
  
 
|-
 
|-
Line 222: Line 222:
  
 
int b=3;
 
int b=3;
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us delete this
+
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us delete this.
  
No need to initialize a and b here
+
No need to initialize '''a''' and '''b''' here.
  
 
|-
 
|-
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Type
+
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Delete printf statement. 
  
int main()
+
Type  int main()
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Then type
+
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Delete the '''printf statement'''.
  
int main()
+
Type  '''int main()'''
  
 
|-
 
|-
Line 238: Line 238:
  
 
int sum;
 
int sum;
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us declare a variable sum here
+
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us declare a variable '''sum''' here.
  
type
+
Type '''int sum;'''
 
+
int sum;
+
  
 
|-
 
|-
Line 248: Line 246:
  
 
sum = add(5,4);
 
sum = add(5,4);
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| '''Then type'''
+
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Then type
  
 
'''sum = add(5,4);'''
 
'''sum = add(5,4);'''
  
Here we call the '''add function'''
+
Here we call the '''add function'''.
  
Then we pass the parameters as 5 and 4
+
Then we pass the parameters as 5 and 4.
  
5 will be stored in a and 4 will be stored in b
+
5 will be stored in '''a''' and 4 will be stored in '''b'''.
  
 
|-
 
|-
 
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
 
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| The addition operation will be performed
+
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| The addition operation will be performed
 
+
The returned value c will be stored in sum
+
  
 
|-
 
|-
Line 270: Line 266:
 
Hence type here
 
Hence type here
  
printf(“Sum is %d\n”,sum);
+
'''printf(“Sum is %d\n”,sum);'''
  
 
|-
 
|-
 
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
 
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Delete this as we have already called the function above.
+
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Delete this, as we have already called the function above.
  
 
|-
 
|-
 
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
 
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
 
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Type
 
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Type
 
+
'''
return 0;
+
return 0;'''
  
 
A '''non-void function''' must use a '''return''' statement that returns a value.
 
A '''non-void function''' must use a '''return''' statement that returns a value.
Line 290: Line 286:
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| On the terminal
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| On the terminal
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us execute the program
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us execute the program.
  
Come back to our terminal
+
Come back to our '''terminal'''.
  
 
|-
 
|-
Line 302: Line 298:
  
 
'''./fun'''
 
'''./fun'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now compile the program as before,
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now compile the program as before.
  
'''gcc function.c -o fun'''
+
Let us execute.
 
+
let us execute  
+
 
+
'''./fun'''
+
  
 
|-
 
|-
Line 316: Line 308:
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| We see the output is displayed as
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| We see the output is displayed as
  
'''The result is 9'''
+
'''Sum is 9'''
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| NOW LET US SEE HOW TO EXECUTE THE SAME PROGRAM IN C++
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now let us see how to execute the same program in '''C++'''.
  
Come back to our program
+
Come back to our program.
  
Let me change a few things here
+
Let me change a few things here.
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Go to File menu
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Press '''Shift, Ctrl''' and '''S''' keys >> type
 
+
Click on save as option
+
 
+
Type
+
  
'''cpp'''
+
'''function.cpp''' in Name field  >> Click on "Save"
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now click on '''File''' menu
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| First press '''Shift, Ctrl''' and '''S''' keys simultaneously.
  
Click on '''Save as option'''
+
Now save the file with''' .cpp''' extension.
  
Save the file''' '''with''' .cpp '''extension
+
Click on '''Save'''.
  
 
|-
 
|-
Line 344: Line 332:
  
 
'''<nowiki><iostream></nowiki>'''
 
'''<nowiki><iostream></nowiki>'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| First we will change the header file  
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| First we will change the '''header file''' as
  
 
'''<nowiki><iostream></nowiki>'''
 
'''<nowiki><iostream></nowiki>'''
Line 352: Line 340:
  
 
'''using namespace std;'''
 
'''using namespace std;'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| We will include the '''using '''statement here
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| We will include the '''using '''statement here.
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| The function declaration is same in C++  
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| The '''function declaration''' is same in '''C++'''.
  
So there is no need to change anything here
+
So there is no need to change anything here.
  
 
|-
 
|-
Line 364: Line 352:
  
 
'''<nowiki>cout<<</nowiki>'''
 
'''<nowiki>cout<<</nowiki>'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now replace the '''printf '''statement with '''cout''' statement
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now replace the '''printf '''statement with '''cout''' statement, as we use '''<nowiki>cout<< function</nowiki>''' to print a line in '''C++.'''
 
+
as we use '''<nowiki>cout<< function</nowiki>''' to print a line in C++
+
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| %d
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| %d
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| We don't need the '''format specifier''' and '''\n''' here
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| We don't need the '''format specifier''' and '''\n''' here.
  
So delete it
+
Delete the '''comma'''.
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''Type'''
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Type
  
 
'''<nowiki><<sum <<”\n”</nowiki>'''
 
'''<nowiki><<sum <<”\n”</nowiki>'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now type two opening angle brackets here  
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now, type two opening '''angle brackets''' here.
  
Then type '''sum '''two opening angle brackets again
+
After '''sum ''', again type two opening '''angle brackets'''.
  
and within the double quotes '''backslash n'''
+
Within  double quotes, type '''backslash n'''.
  
Delete this closing bracket''' '''
+
Delete this closing bracket.
  
Click on''' save'''
+
Click on''' Save'''.
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| On the terminal
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| On the terminal
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us compile the program
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us compile the program.
  
Come back to the terminal
+
Come back to the '''terminal'''.
  
 
|-
 
|-
Line 407: Line 393:
 
'''g++ function.cpp -o fun1'''
 
'''g++ function.cpp -o fun1'''
  
Here we have fun1, this is because we don't want to overwrite the output file.
+
Here we have '''fun1''', this is because we don't want to overwrite the output file '''fun'''.
  
press''' Enter'''
+
Press''' Enter'''.
 
+
To execute
+
  
 
Type
 
Type
Line 423: Line 407:
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here the output is displayed as:  
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here the output is displayed as:  
  
'''The result is 9.'''
+
'''Sum is 9'''
  
 
|-
 
|-
Line 432: Line 416:
  
 
'''x'''
 
'''x'''
 
  
  
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now we will see the common errors which we can come across.
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now we will see the common errors which we can come across.
  
Suppose here we type '''x''' in the place of 4.
+
Suppose here, we type '''x''' in the place of 4.
  
 
I will retain the rest of the code as it is.
 
I will retain the rest of the code as it is.
Line 443: Line 426:
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Click on save
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Click on save
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Save the program
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Click on '''Save'''.
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| On the terminal
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| On the terminal
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us go to the terminal
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us compile the program.
 
+
and compile the program  
+
  
 
|-
 
|-
Line 456: Line 437:
  
 
'''error'''
 
'''error'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| We see an error at line no. 11 in our '''function.cpp '''file
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| We see an error at line no. 10.
  
'''x '''undeclared
+
'''x was not declared in this scope'''.
  
This is because '''x''' is a '''character''' variable
+
This is because '''x''' is a '''character''' variable.  It was not declared anywhere.
  
And our '''add''' function has '''integer''' variable as an '''argument'''
+
And our '''add''' function has '''integer''' variable as an '''argument'''.
  
So there is a mismatch in return type and return value.
+
So, there is a mismatch in '''return type''' and '''return value'''.
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| On the editor
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| On the editor
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now let's come back to our program
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now let's come back to our program.
  
Let us fix the error
+
Let us fix the error.
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Type
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Type '''4'''
 
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Type 4 at line no. 10.
'''4'''
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Type 4 here at line no. 11
+
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Click on '''Save'''
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Click on '''Save'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Click on '''Save'''
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Click on '''Save'''.
  
Let us execute this program
+
Let us execute again.
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| On the terminal
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| On the terminal
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Come back to our terminal
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let me clear the prompt.
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us compile and execute as before
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us compile the program as before.
  
Yes! it is working
+
Yes! it is working.
  
 
|-
 
|-
Line 502: Line 481:
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us see another common error which we can come across.
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us see another common error which we can come across.
  
Suppose here we pass only 1 parameter
+
Suppose here we pass only one parameter.
  
Let us delete 4
+
Delete 4.
  
 
|-
 
|-
Line 512: Line 491:
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| On the terminal
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| On the terminal
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us compile it
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Switch to the terminal.  Let us compile.
 
+
Switch to the terminal
+
  
 
|-
 
|-
Line 520: Line 497:
  
 
'''Error'''
 
'''Error'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| We see an error at line no 11 in our '''function.cpp''' file
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| We see an error at line no 10.
  
'''too few arguments to function 'add''''
+
'''too few arguments to function 'int add (int, int)''''
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| On the editor
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| On the editor
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us switch back to the program
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Switch back to the program.
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| You can see here we have two parameters
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| You can see here we have two parameters -
  
'''int a''' and '''int b'''
+
'''int a''' and '''int b'''.
  
And here we are passing only one parameter
+
And here we are passing only one parameter.
  
Hence it is giving an error
+
Hence it is giving an error.
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Click on '''Save'''
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Click on '''Save'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us fix the error
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us fix the error.
  
Type 4 here
+
Type 4.
  
Click on '''Save'''  
+
Click on '''Save''' .
  
Switch back to the terminal  
+
Switch back to the '''terminal'''.
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| On the terminal
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| On the terminal
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us compile the program
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us execute again.
  
 
|-
 
|-
Line 556: Line 533:
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Yes it is working!
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Yes it is working!
  
Now let go through a quick summary
+
Come back to our slides.
 
+
Come back to our slides
+
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 7
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 7
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Syntax of function
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| To summarise, in this tutorial we have learnt -
 
+
*'''Function'''
Function without arguments
+
*Syntax of '''function'''
 
+
*'''Function without arguments'''
Function with arguements
+
** Eg- void add()
 
+
*Function with arguments
Significance of a return statement
+
** Eg- int add(int a, int b)
  
 
|-
 
|-
Line 574: Line 549:
  
 
Assignment
 
Assignment
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| As an assignment
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| As an assignment-
  
Write a program to calculate the square of a number using function.
+
Write a program to calculate the square of a number.
  
 
|-
 
|-
Line 596: Line 571:
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| The Spoken Tutorial Project Team  
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| The Spoken Tutorial Project Team  
  
Conducts workshops using spoken tutorials  
+
*Conducts workshops using spoken tutorials  
  
Gives certificates to those who pass an online test  
+
*Gives certificates to those who pass an online test  
  
 
For more details, please write to
 
For more details, please write to
Line 619: Line 594:
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| This is Ashwini Patil from IIT Bombay
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| This is Ashwini Patil from IIT Bombay
  
Thank You for joining
+
Thank You for joining.
  
 
|}
 
|}

Revision as of 17:53, 7 January 2014

Title of script: Functions in C and C++

Author: Ashwini R. Patil

Keywords: Functions, return statement, Video Tutorial


Visual Cue
Narration


Slide 1 Welcome to the spoken tutorial on Functions in C and C++
Slide 2


In this tutorial we will learn,

What is a function

Syntax of function

Significance of return statement

Few examples on functions

We will also see some common errors and their solutions.

Slide 3


To record this tutorial, I am using

Ubuntu Operating system version 11.10

gcc and g++ Compiler version 4.6.1

Slide 4 Let us start with the introduction to functions

A function is a self-contained program executing a specific task

Every program consists of one or more functions

Once executed the control will be returned back from where it was accessed

Slide 5 Let us see the syntax for function

ret_type fun_name(parameters)

Or

ret_type fun_name( )

ret-type defines the type of data that the function returns.

fun_name defines the name of the function.

parameters is the list of variable names and their types.

Slide 6 We can specify an empty parameter list.

This is called as Bold textfunctions without arguments.

This is called as functions with arguments.

Let us see a program using void.

I have already typed the program on the editor.

So I will just open it.

Point the cursor on the filename

function.c

Please note that our filename is function.

And I have saved the file with the extentsion .c

Let me explain the code.

Highlight

#include <stdio.h>

This is our header file.
Highlight

void add()

Before using any function, it must be defined.

Here we have declared a function called add.

Note that add function is without any arguments.

And the return type is void.

There are two types of functions-
  • User-defined that is our add function
  • Pre-defined that is printf and main function
Highlight

int a = 2;

int b = 3;

Here we have initialized a and b by assigning them values as 2 and 3
Highlight

int c = a + b;

Here we have declared a variable c.

Then we add the values of a and b.

The result is stored in c.

Highlight

printf("Value of C is %d\n",c);

Then we print the result.
Highlight

void main()

This is our main function.
Highlight

add();

Here we call the add function.

The addition operation will be performed and the result will be printed.

Now click on Save.

Now let us execute the program.

Press Ctrl, Alt and T keys simultaneously Please open the terminal window by pressing Ctrl, Alt and T keys simultaneously.
Type

gcc function.c -o fun

Type

./fun

To compile the program, type

gcc function.c -o fun

To execute, type

./fun

Highlight

Output

We see the output is displayed as

Sum of a and b is 5

On the editor Now come back to our program.

Functions contains special identifiers called as parameters or arguments.

Let us see the same example with arguments.

Type

int add(int a, int b)

I will just change a few things here.

Type

int add(int a, int b)

Here we have declared a function add.

int a and int b are the arguments of the function add.

Delete

int a=2;

int b=3;

Let us delete this.

No need to initialize a and b here.

Delete printf statement.

Type int main()

Delete the printf statement.

Type int main()

Type

int sum;

Let us declare a variable sum here.

Type int sum;

Highlight

sum = add(5,4);

Then type

sum = add(5,4);

Here we call the add function.

Then we pass the parameters as 5 and 4.

5 will be stored in a and 4 will be stored in b.

The addition operation will be performed.
Let us now print the result.

Hence type here

printf(“Sum is %d\n”,sum);

Delete this, as we have already called the function above.
Type

return 0;

A non-void function must use a return statement that returns a value.

Click on Save Click on Save
On the terminal Let us execute the program.

Come back to our terminal.

Type

gcc function.c -o fun

Type

./fun

Now compile the program as before.

Let us execute.

Highlight

Output

We see the output is displayed as

Sum is 9

Now let us see how to execute the same program in C++.

Come back to our program.

Let me change a few things here.

Press Shift, Ctrl and S keys >> type

function.cpp in Name field >> Click on "Save"

First press Shift, Ctrl and S keys simultaneously.

Now save the file with .cpp extension.

Click on Save.

Type

<iostream>

First we will change the header file as

<iostream>

Type

using namespace std;

We will include the using statement here.
The function declaration is same in C++.

So there is no need to change anything here.

Type

cout<<

Now replace the printf statement with cout statement, as we use cout<< function to print a line in C++.
 %d We don't need the format specifier and \n here.

Delete the comma.

Type

<<sum <<”\n”

Now, type two opening angle brackets here.

After sum , again type two opening angle brackets.

Within double quotes, type backslash n.

Delete this closing bracket.

Click on Save.

On the terminal Let us compile the program.

Come back to the terminal.

Type

g++ function.cpp -o fun1


Type

./fun1

Type

g++ function.cpp -o fun1

Here we have fun1, this is because we don't want to overwrite the output file fun.

Press Enter.

Type

./fun1

Highlight

Output

Here the output is displayed as:

Sum is 9

Errors


Type

x


Now we will see the common errors which we can come across.

Suppose here, we type x in the place of 4.

I will retain the rest of the code as it is.

Click on save Click on Save.
On the terminal Let us compile the program.
Highlight


error

We see an error at line no. 10.

x was not declared in this scope.

This is because x is a character variable. It was not declared anywhere.

And our add function has integer variable as an argument.

So, there is a mismatch in return type and return value.

On the editor Now let's come back to our program.

Let us fix the error.

Type 4 Type 4 at line no. 10.
Click on Save Click on Save.

Let us execute again.

On the terminal Let me clear the prompt.
Let us compile the program as before.

Yes! it is working.

Error 2

Delete

4

Let us see another common error which we can come across.

Suppose here we pass only one parameter.

Delete 4.

Click on Save Click on Save
On the terminal Switch to the terminal. Let us compile.
Highlight

Error

We see an error at line no 10.

too few arguments to function 'int add (int, int)'

On the editor Switch back to the program.
You can see here we have two parameters -

int a and int b.

And here we are passing only one parameter.

Hence it is giving an error.

Click on Save Let us fix the error.

Type 4.

Click on Save .

Switch back to the terminal.

On the terminal Let us execute again.
Yes it is working!

Come back to our slides.

Slide 7 To summarise, in this tutorial we have learnt -
  • Function
  • Syntax of function
  • Function without arguments
    • Eg- void add()
  • Function with arguments
    • Eg- int add(int a, int b)
Slide 10

Assignment

As an assignment-

Write a program to calculate the square of a number.

Slide 11

About the Spoken Tutorial Project

Watch the video available at

http://spoken-tutorial.org /What\_is\_a\_Spoken\_Tutorial

It summarises the Spoken Tutorial project

If you do not have good bandwidth, you can download and watch it

Slide 12

Spoken Tutorial Workshops

The Spoken Tutorial Project Team
  • Conducts workshops using spoken tutorials
  • Gives certificates to those who pass an online test

For more details, please write to

contact@spoken-tutorial.org

Slide 13


Acknowledgement

Spoken Tutorial Project is a part of the Talk to a Teacher project

It is supported by the National Mission on Education through ICT, MHRD, Government of India

More information on this Mission is available at: http://spoken-tutorial.org\NMEICT-Intro

This is Ashwini Patil from IIT Bombay

Thank You for joining.

Contributors and Content Editors

Ashwini, Nancyvarkey, Priyacst