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

From Script | Spoken-Tutorial
Jump to: navigation, search
(Created page with ''''Title of script:''' Functions in C and C++ '''Author: '''Ashwini R. Patil '''Keywords: Functions, return statement, Video Tutorial ''' {| style="border-spacing:0;" | styl…')
 
 
(5 intermediate revisions by 3 users not shown)
Line 23: Line 23:
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| In this tutorial we will learn,
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| In this tutorial we will learn,
  
What is a function
+
*What is a '''function'''
  
Syntax of function
+
*Syntax of a '''function'''
  
Significance of a return statement
+
*Significance of '''return statement'''
  
We will do this through an example
+
We will do this through examples.
  
 
We will also see some common errors and their solutions.
 
We will also see some common errors and their solutions.
Line 40: Line 40:
 
| 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;"| To record this tutorial, I am using
 
| 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;"| To record this tutorial, I am using
  
'''Ubuntu Operating system''' version 11.10
+
*'''Ubuntu Operating system''' version 11.10
 
+
*'''gcc''' and '''g++ Compiler''' version 4.6.1
'''gcc''' and g'''++ Compiler''' version 4.6.1 on Ubuntu
+
  
 
|-
 
|-
Line 48: Line 47:
 
| 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 start with the introduction to '''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;"| Let us start with the introduction to '''functions'''
  
A '''function''' is a self-contained program executing a specific task
+
A '''function''' is a self-contained program executing a specific task.
  
Every program consists of one or more '''functions'''
+
Every program consists of one or more '''functions'''.
  
Once it is executed the control will be returned back from where it was accessed
+
Once executed, the control will be returned back from where it was accessed.
  
 
|-
 
|-
 
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 5
 
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 5
| 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 the syntax for 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;"| Let us see the syntax for '''function'''.
  
'''ret_type fun_name(parameters)'''
 
  
'''Or'''
+
'''ret-type''' defines the type of data that the '''function returns'''.
  
'''ret_type fun_name( )'''
+
'''fun_name''' defines the name of the '''function'''.
  
'''ret-type''' defines the type of data that the '''function''' returns
+
'''parameters''' is the list of '''variable''' names and their types.
 
+
'''fun_name''' defines the name of the '''function'''
+
 
+
'''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 functions without arguments.
+
And
  
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 open it.
  
 
|-
 
|-
Line 90: Line 86:
  
 
'''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;"| 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 96:
  
 
'''<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 102:
  
 
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 defined 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''' and
  
Pre-defined that is printf and main function
+
*'''Pre-defined''' that is '''printf''' and '''main function'''
  
 
|-
 
|-
Line 128: Line 124:
  
 
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 130:
  
 
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 140:
  
 
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 146:
  
 
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 152:
  
 
'''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 158:
 
|-
 
|-
 
| 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
+
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 178: Line 174:
  
 
'''./fun'''
 
'''./fun'''
| 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;"| To compile the program, 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;"| To compile, type
  
 
'''gcc function.c -o fun'''
 
'''gcc function.c -o fun'''
Line 196: Line 192:
 
|-
 
|-
 
| 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 202:
  
 
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 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 218:
  
 
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 234:
  
 
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 242:
  
 
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 262:
 
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 282:
 
|-
 
|-
 
| 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 294:
  
 
'''./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 314: Line 302:
  
 
'''Output'''
 
'''Output'''
| 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;"| 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;"|First Press '''Shift, Ctrl''' and '''S''' keys simultaneously>> 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 328:
  
 
'''<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 336:
  
 
'''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 348:
  
 
'''<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'''
  
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'''
+
Now 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 389:
 
'''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'''
+
  
To execute
+
Press''' Enter'''.
  
 
Type
 
Type
Line 421: Line 401:
  
 
'''Output'''
 
'''Output'''
| 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;"| The output is displayed as:  
  
'''The result is 9.'''
+
'''Sum is 9'''
  
 
|-
 
|-
Line 432: Line 412:
  
 
'''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 422:
 
|-
 
|-
 
| 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 433:
  
 
'''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 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;"| Compile the program as before.
  
Yes! it is working
+
Yes! it is working.
  
 
|-
 
|-
Line 500: Line 475:
  
 
'''4'''
 
'''4'''
| 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;"| Now we will 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 487:
 
|-
 
|-
 
| 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 493:
  
 
'''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 our 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 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 529:
 
| 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 545:
  
 
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 582: Line 553:
  
 
About the Spoken Tutorial Project
 
About the Spoken Tutorial Project
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Watch the video available at  
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Watch the video available at the link shown below
  
 
http://spoken-tutorial.org /What\_is\_a\_Spoken\_Tutorial
 
http://spoken-tutorial.org /What\_is\_a\_Spoken\_Tutorial
Line 596: Line 567:
 
| 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 613: Line 584:
 
It is supported by the National Mission on Education through ICT, MHRD, Government of India
 
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
+
More information on this Mission is available at the link shown below http://spoken-tutorial.org\NMEICT-Intro
  
 
|-
 
|-
Line 619: Line 590:
 
| 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.
  
 
|}
 
|}

Latest revision as of 11:55, 30 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 a function
  • Significance of return statement

We will do this through examples.

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 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 functions without arguments.

And

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 open it.

Point the cursor on the filename

function.c

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 defined 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 and
  • 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.

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, 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 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

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.

First Press Shift, Ctrl and S keys simultaneously>> 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

After sum , again type two opening angle brackets.

Within double quotes, type backslash n.

Delete this closing bracket.

Now 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

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 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.
Compile the program as before.

Yes! it is working.

Error 2

Delete

4

Now we will 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 our 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 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 the link shown below

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 the link shown below http://spoken-tutorial.org\NMEICT-Intro

This is Ashwini Patil from IIT Bombay

Thank You for joining.

Contributors and Content Editors

Ashwini, Nancyvarkey, Priyacst