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

From Script | Spoken-Tutorial
Jump to: navigation, search
Line 1: Line 1:
{| border=1
+
{| border = 1
|| ''Time'''
+
 
|| '''Narration'''
+
|'''Time'''
 +
 
 +
|'''Narration'''
  
  
 
|-
 
|-
 
| 00.01
 
| 00.01
| Welcome to the spoken tutorial on '''Functions in C and C++'''
+
|Welcome to the spoken tutorial on Functions in C and C++
  
 
|-
 
|-
|00.07
+
| 00.06
| In this tutorial we will learn,
+
|In this tutorial we will learn,  
  
 
|-
 
|-
|00.10
+
| 00.09
| What is a function
+
|What is a function
  
 
|-
 
|-
|00.12
+
| 00.11
| Syntax of a function
+
|Syntax of a function
  
 
|-
 
|-
|00.15
+
| 00.13
| Significance of a return statement
+
|Significance of return statement
  
 
|-
 
|-
|00.18
+
| 00.16
| Few  example on functions.
+
| We will do this through examples.  
  
 
|-
 
|-
|00.20
+
| 00.18
| We will also see some common errors and their solutions.
+
|We will also see some common errors and their solutions.  
  
 
|-
 
|-
|00.25
+
| 00.22
| To record this tutorial, I am using'''Ubuntu Operating system''' version 11.10
+
| To record this tutorial, I am using,
  
 
|-
 
|-
|00.33
+
| 00.25
| '''gcc''' and g'''++ Compiler''' version 4.6.1
+
|Ubuntu Operating System version 11.10
  
 
|-
 
|-
|00.40
+
|00.30
|Let us start with the introduction to '''functions'''
+
| gcc and g++ Compiler version 4.6.1 .
 +
 
  
 
|-
 
|-
|00.43
+
|00.35
| A '''function''' is a self-contained program executing a specific task
+
|Let us start with the introduction to functions
  
 
|-
 
|-
|00.50
+
|00.39
| Every program consists of one or more '''functions'''
+
|A function is a self-contained program executing a specific task.
  
 
|-
 
|-
|00.56
+
| 00.44
| Once executed the control will be returned back from where it was accessed
+
|Every program consists of one or more functions.
  
 
|-
 
|-
| 01.03
+
| 00.49
| Now we will  see the syntax for the  function
+
|Once executed, the control will be returned back from where it was accessed.
  
 
|-
 
|-
|01.18
+
| 00.55
| ''ret-type''' defines the type of data that the '''function''' returns
+
|Let us see the syntax for function.
  
 
|-
 
|-
|01.12
+
| 00.59
|fun_name''' is the name of the '''function'''
+
|ret-type defines the type of data that the function returns.
  
 
|-
 
|-
|01.16
+
| 01.05
|'''parameters''' is the list of '''variable''' names and their types
+
|fun_name defines the name of the function.  
|-
+
|01.20
+
|Another syntax for functions  is '''ret_type function name  an empty parameter list
+
  
  
 
|-
 
|-
|01.30
+
|01.09
| This is called as functions without arguments.
+
|parameters is the list of variable names and their types.  
  
 
|-
 
|-
|01.35
+
| 01.14
| And This is called as functions with arguments.
+
|We can specify an empty parameter list.  
  
 
|-
 
|-
| 01.40
+
| 01.17
| Let us move on to  our program
+
|This is called as functions without arguments.
  
 
|-
 
|-
|01.43
+
| 01.21
| I have already typed the program on the editor
+
|And this is called as functions with arguments.
  
 
|-
 
|-
|01.46
+
|01.27
| Let me open it
+
|Let us see a program using void.
  
 
|-
 
|-
|01.50
+
|01.29
| Note that our filename is ''' void function.c ''' In this program we will calculate the sum of two numbers  using function..  
+
|I have already typed the program on the editor.  
 +
 
 
|-
 
|-
|02.03
+
|01.32
|Let me explain the code now.
+
|So I will open it.  
  
  
 
|-
 
|-
| 02.06
+
|01.35
| This is our '''header file'''
+
|Note that our filename is function.
  
 
|-
 
|-
| 02.09
+
| 01.38
| Before using any function it must be defined
+
|And I have saved the file with the extentsion .c
  
 
|-
 
|-
|02.14
+
| 01.43
| Here we have declared a''' function''' called '''add'''
+
|Let me explain the code.
 +
 
  
 
|-
 
|-
|02.18
+
| 01.45
| Note that '''add function''' is without any '''arguments'''
+
|This is our header file.
  
 
|-
 
|-
|02.22
+
|01.47
| And the return type is''' void'''
+
|Before using any function, it must be defined.
  
 
|-
 
|-
| 02.25
+
|01.51
| There are two types of functions
+
|Here we have defined a function called add.
  
 
|-
 
|-
|02.27
+
|01.54
| First User-defined  function that is our add function and
+
|Note that add function is without any arguments.
  
 
|-
 
|-
|02.33
+
|01.58
| Pr-defined function that is printf and main function
+
|And the return type is void.
  
 
|-
 
|-
| 02.39
+
| 02.01
|   Here we have initialized a and b by assigning them values  2 and 3
+
|There are two types of functions-
 +
 
  
 
|-
 
|-
| 02.47
+
| 02.03
| Then  we have declared  a variable '''c'''
+
| User-defined that is our add function and
  
 
|-
 
|-
|02.51
+
| 02.06
| we add the values of '''a''' and '''b'''
+
| Pre-defined that is printf and main function
 +
 
  
 
|-
 
|-
|02.53
+
| 02.12
| '''The result is stored in c'''
+
| Here we have initialized a and b by assigning them values as 2 and 3
  
 
|-
 
|-
| 02.57
+
| 02.19
|   Then we print the result
+
| Here we have declared a variable c.
  
 
|-
 
|-
| 03.00
+
| 02.21
| This is our main function
+
|Then we add the values of a and b.
  
 
|-
 
|-
| 03.03
+
| 02.24
|Inside the main function, we call the add function
+
|The result is stored in c.
  
 
|-
 
|-
|03.07
+
| 02.27
| The addition operation will be performed and the result will be printed.
+
|Then we print the result.  
  
 
|-
 
|-
| 03.13
+
| 02.29
| Now click on Save
+
| This is our main function.
  
 
|-
 
|-
|03.15
+
| 02.32
| Let us execute the program
+
|Here we call the add function.
  
 
|-
 
|-
| 03.17
+
| 02.34
| Please open the terminal window by pressing Ctrl, Alt and T keys simultaneously on your keyboard
+
|The addition operation will be performed and the result will be printed.
  
 
|-
 
|-
| 03.28
+
| 02.39
| To compile  type
+
| Now click on Save.
  
 
|-
 
|-
|03.29
+
| 02.42
| '''gcc void function.c -o void '' and press enter
+
|Let us execute the program.
  
 
|-
 
|-
| 03.40
+
| 02.45
| To execute, type
+
|Please open the terminal window by pressing Ctrl, Alt and T keys simultaneously.
 +
 
 +
|-
 +
| 02.53
 +
|To compile, type gcc  function dot c  hyphen o  fun
  
'''./void'''
 
  
 
|-
 
|-
| 03.45
+
| 03.00
| The output is displayed as'''Sum of a and b is 5'''
+
|To execute, type ./fun
 +
 
  
 
|-
 
|-
|03.50
+
| 03.05
| Now come back to our program
+
|We see the output is displayed as Sum of a and b is 5
 +
 
  
 
|-
 
|-
|03.53
+
| 03.10
| Functions contains special identifiers called as parameters or arguments
+
|Now come back to our program.
  
 
|-
 
|-
|04.00
+
| 03.13
| Now we will  see the same example with arguments
+
|Functions contains special identifiers called as parameters or arguments.
  
 
|-
 
|-
| 04.03
+
| 03.20
| |I will  change a few things here. Press ''shift'' ''Ctrl'' & ''S'' key  simultaneously on your keyboard.
+
|Let us see the same example with arguments.  
  
 
|-
 
|-
|04.14
+
| 03.23
| Now save the file as'' Function.c'' .Click on ''Save''.
+
|I will change a few things here.  
 +
 
 
|-
 
|-
|04.24
+
| 03.27
| Replace the void key word  with ''int''  and within the brakets type '''int a, int b'''
+
|Type int add(int a, int b)
 +
 
 +
 
 
|-
 
|-
|04.34
+
| 03.32
| Click on save
+
|Here we have declared a function add.
  
  
 
|-
 
|-
|04.37
+
| 03.36
| Here '''int a'''  and '''int b''' are the '''arguments''' of the '''function add'''
+
|int a and int b are the arguments of the function add.  
type int sum;
+
|-
+
| 04.44
+
| Now  delete this
+
  
 
|-
 
|-
|04.47
+
| 03.41
| No need to initialize a and b here. Now replace the  void keyword  again with the ''int '' keyword  and click on save
+
|Let us delete this.  
  
 +
|-
 +
| 03.42
 +
|No need to initialize a and b here.
  
 
|-
 
|-
| 04.58
+
| 03.46
| Let us declare a variable sum here
+
|Delete the printf statement.
  
 
|-
 
|-
|05.01
+
| 03.49
| type int sum and the semicolon;
+
|Type int main()
 +
 
  
 
|-
 
|-
| 05.05
+
| 03.52
|Press enter
+
| Let us declare a variable sum here.
 +
 
  
 
|-
 
|-
|05.06
+
| 03.54
|And type  '''sum = add, opening bracket, closing bracket, within the bracket (5,4) at the end type a semicolon;'''
+
|Type int sum;  
 +
 
  
 
|-
 
|-
|05.19
+
| 03.57
| Here we call the '''add function'''
+
|Then type sum = add(5,4);
 +
 
  
 
|-
 
|-
|05.22
+
| 04.03
| Then we pass the arguments as 5 and 4
+
|Here we call the add function.
  
 
|-
 
|-
|05.26
+
| 04.05
| 5 will be stored in a and 4 will be stored in b
+
|Then we pass the parameters as 5 and 4.
  
 
|-
 
|-
| 05.31
+
| 04.10
| | The addition operation will be performed
+
|5 will be stored in a and 4 will be stored in b.
  
 
|-
 
|-
|05.34
+
| 04.14
| The returned value c will be stored in sum.  
+
|The addition operation will be performed.  
|-
+
| 05.38
+
|  Now delete this '''add'''  as we have already called the function above
+
  
 
|-
 
|-
| 05.44
+
| 04.18
| And Type
+
|Let us now print the result.
  
 
|-
 
|-
|05.45
+
| 04.20
| return 0; Now click on save
+
|Hence type here
  
 
|-
 
|-
|05.51
+
| 04.21
| A '''non-void function''' must use a '''return''' statement that returns a value.
+
|printf(“Sum is %d\n”,sum);
  
 
|-
 
|-
| 05.58
+
| 04.27
| Let us execute the program
+
|Delete this, as we have already called the function above.
  
 
|-
 
|-
|06.00
+
| 04.32
| Come back to a  terminal
+
|Type return 0;
 +
 
  
 
|-
 
|-
| 06.03
+
| 04.36
| Type '''gcc function.c -o fun''' and press enter
+
|A non-void function must use a return statement that returns a value.  
  
  
 
|-
 
|-
|06.13
+
| 04.41
|To execute
+
|Click on Save
  
'''./fun'''  press enter
+
|-
 +
| 04.43
 +
|Let us execute the program.  
  
 
|-
 
|-
| 06.19
+
| 04.45
| the output is displayed as
+
|Come back to our terminal.
  
 
|-
 
|-
|06.21
+
| 04.48
| '''Sum of a & b is 9'''
+
|Now compile the program as before.
  
 
|-
 
|-
| 06.25
+
| 04.50
|NOW  WE  WILL EXECUTE THE SAME PROGRAM IN C++
+
|Let us execute.
 +
 
  
 
|-
 
|-
|06.29
+
| 04.52
| Come back to our program. I will edit the same code, again press ''Shift''''Ctrl'' & ''S'' key simultaneously on your keyboard
+
|The output is displayed as Sum is 9
  
  
 +
|-
 +
| 04.57
 +
|Now let us see how to execute the same program in C++.
  
 
|-
 
|-
|06.41
+
| 05.02
| Now Save the file''' '''with an extension ''' .cpp ''' and click on save
+
|Come back to our program.
  
 
|-
 
|-
| 06.47
+
| 05.04
| Let us change the header file as ''iostream''
+
|Let me change a few things here.
 +
 
  
 
|-
 
|-
| 06.52
+
| 05.07
| Now include the '''using '''statement. Click on save
+
|First press Shift, Ctrl and S keys simultaneously.  
  
 
|-
 
|-
| 07.00
+
| 05.12
|   The function declaration is same in C++
+
|Now save the file with .cpp extension.
  
 
|-
 
|-
|07.04
+
| 05.18
| So there is no need to change anything here
+
|Click on Save.
  
 
|-
 
|-
| 07.07
+
| 05.19
| Now replace the '''printf '''statement with  the '''cout''' statement
+
|First we will change the header file as <iostream>
  
  
 
|-
 
|-
| 07.13
+
| 05.24
| Delete  the '''format specifier''' and '''\n'''
+
|We will include the using statement here.
  
 
|-
 
|-
|07.16
+
| 05.28
| Delete the comma
+
|The function declaration is same in C++.
  
 
|-
 
|-
| 07.17
+
| 05.32
| Type two opening angle brackets. Delete the closing  bracket here  
+
|So there is no need to change anything here.
  
 
|-
 
|-
|07.23
+
| 05.37
| Again type  two opening angle brackets
+
|Now replace the printf statement with the cout statement, as we use cout<< function to print a line in C++.
  
 
|-
 
|-
|07.25
+
| 05.48
| and within the double quotes  type '''backslash n'''
+
|We don't need the format specifier and \n here.
 +
 
  
 
|-
 
|-
|07.29
+
| 05.52
| We use the cout function to print the line in C++ 
+
|Delete the comma.
 +
 
 
|-
 
|-
| 07.34
+
| 05.54
| Now Click on''' save'''
+
|Now, type two opening angle brackets
  
 
|-
 
|-
| 07.37
+
| 05.58
| Let us exeute  the program
+
|After sum , again type two opening angle brackets.
  
 
|-
 
|-
|07.39
+
| 06.03
| Come back to our terminal
+
|Within double quotes, type backslash n.
  
 
|-
 
|-
| 07.42
+
| 06.07
| To compile,  type '''g++ function.cpp -o fun1'''
+
|Delete this closing bracket.  
  
 
|-
 
|-
|07.52
+
| 06.09
| Here we have fun1, because we don't want to overwrite the output parameter fun for the  file function.c .
+
|Now Click on Save.  
  
 
|-
 
|-
|08.02
+
| 06.11
| Now press''' Enter'''
+
|Let us compile the program.
  
 
|-
 
|-
|08.05
+
| 06.14
| To execute
+
|Come back to the terminal.
  
 
|-
 
|-
|08.06
+
| 06.16
| Type'''./fun1''' And press enter
+
|Type g++ function dot cpp hyphen o fun1  
 +
 
  
 
|-
 
|-
| 08.12
+
| 06.23
| The output is displayed as:
+
|Here we have fun1, this is because we don't want to overwrite the output file fun.
+
 
 +
 
 
|-
 
|-
|08.14
+
| 06.31
| '''Sum of a & b is 9.'''
+
|Press Enter.  
  
 
|-
 
|-
| 08.16
+
| 06.34
|we can see that  the output is similar to our c code
+
|Type ./fun1
 +
 
 +
 
 
|-
 
|-
|08.20
+
| 06.38
| Let us  see some  common errors which we can come across.
+
|The output is displayed as: Sum is 9
 +
 
 
|-
 
|-
|08.24
+
| 06.42
| Come back to our program.
+
|Now we will see the common errors which we can come across.  
  
 
|-
 
|-
|08.26
+
| 06.47
| Suppose here at line no-11 . I will type '''x''' in the place of 4.
+
|Suppose here, we type x in place of 4.  
  
 
|-
 
|-
|08.32
+
| 06.51
| I will retain the rest of the code as it is.
+
|I will retain the rest of the code as it is.  
  
 
|-
 
|-
| 08.36
+
| 06.55
|Now click on   Save
+
|Click on Save.
 +
|-
 +
| 06.58
 +
|Let us compile the program.
  
 
|-
 
|-
| 08.38
+
| 07.02
|Let us execute the  program
+
|We see an error at line no. 10.
 +
 
 
|-
 
|-
|08.40
+
| 07.06
| Come back to  our terminal.
+
|x was not declared in this scope.  
 +
 
 
|-
 
|-
|08.44
+
| 07.09
| Let us  compile  as  before
+
|This is because x is a character variable.
  
 
|-
 
|-
| 08.48
+
| 07.13
| We see an error
+
|It was not declared anywhere.
 +
 
 
|-
 
|-
|08.50
+
| 07.15
| '''x '''was not declared  in this scope. come back to our program
+
|And our add function has integer variable as an argument.
  
 
|-
 
|-
|08.54
+
| 07.21
| This is because '''x''' is a '''character''' variable
+
|So, there is a mismatch in return type and return value.
  
 
|-
 
|-
|08.58
+
| 07.25
| And our '''add''' function has '''integer''' variable as an '''argument'''
+
|Now come back to our program.
  
 
|-
 
|-
|09.04
+
| 07.27
| So there is a mismatch in return type and return value.
+
|Let us fix the error.  
  
 
|-
 
|-
| 09.08
+
| 07.30
| Now Let us fix the error
+
|Type 4 at line no. 10.
  
 +
|-
 +
| 07.32
 +
|Click on Save.
  
 +
|-
 +
| 07.35
 +
|Let us execute again.
  
 
|-
 
|-
|09.10
+
| 07.37
| Type 4 here. Click on '''Save'''
+
|Let me clear the prompt.  
  
 
|-
 
|-
|09.15
+
| 07.40
| Let us execute
+
|Compile the program as before.
  
 
|-
 
|-
|09.17
+
| 07.42
| Come back to our terminal. Let me clear the prompt.
+
|Yes! it is working.  
  
 
|-
 
|-
|09.21
+
| 07.45
| Let us compile as before, execute as before
+
|Now we will see another common error which we can come across.
  
 
|-
 
|-
|09.27
+
| 07.50
| Yes! it is working
+
|Suppose here we pass only one parameter. 
  
 
|-
 
|-
| 09.29
+
| 07.55
|now we will see another common error .Come back to our program
+
|Delete 4.  
  
 
|-
 
|-
|09.34
+
| 07.56
| here we  will  pass only 1 argument
+
|Click on Save
  
 
|-
 
|-
|09.39
+
| 07.58
| delete 4
+
|Switch to the terminal.
  
 
|-
 
|-
| 09.40
+
| 08.00
| Now  Click on '''Save''' .
+
| Let us compile.  
  
 
|-
 
|-
| 09.43
+
| 08.03
| Let us see, what happens come back to our terminal.
+
|We see an error at line no 10.  
  
 
|-
 
|-
| 09.47
+
| 08.05
| Let us compile as before
+
|too few arguments to function 'int add (int, int)'
  
 +
|-
 +
| 08.11
 +
|Switch back to our program.
  
  
 
|-
 
|-
| 09.49
+
| 08.14
|We see error too few arguments to few functions  int  'add''''
+
|You can see here we have two parameters
  
 +
|-
 +
| 08.19
 +
|int a and int b.
  
 
|-
 
|-
| 09.54
+
| 08.22
| Come back to our  program
+
|And here we are passing only one parameter.
  
 +
|-
 +
| 08.25
 +
|Hence it is giving an error.
  
 
|-
 
|-
|09.56
+
| 08.27
| You can see here we have two argument '''int a''' and '''int b'''
+
|Let us fix the error.
  
 
|-
 
|-
|10.03
+
| 08.29
| And here we are passing only one argument.
+
|Type 4.  
  
 
|-
 
|-
|10.06
+
| 08.31
| Hence it is giving an error
+
|Click on Save .
  
 
|-
 
|-
|10.09
+
| 08.34
| Let us fix the error
+
|Switch to the terminal.
  
 
|-
 
|-
|10.10
+
| 08.36
| Type 4 ,click on save
+
|Let us execute again.
  
 
|-
 
|-
|10.13
+
| 08.39
| Let us execute again
+
|Yes it is working!
  
 
|-
 
|-
|10.16
+
| 08.42
|Compile as before , execute as before.
+
|Come back to our slides.  
 +
 
  
 
|-
 
|-
| 10.21
+
| 08.44
| Yes it is working!Now come back to our slide
+
|To summarise, in this tutorial we have learnt -
  
 
|-
 
|-
|10.26
+
| 08.49
| Let us summaries ,In this tutorial we learn't
+
|Function
  
 
|-
 
|-
|10.29
+
| 08.50
| Functions
+
|Syntax of function
 +
 
 
|-
 
|-
| 10.31
+
| 08.51
| Syntax of function
+
|Function without arguments
  
 
|-
 
|-
|10.33
+
| 08.53
| Function without arguments: e.g ; void add()
+
|Eg- void add()  
  
 
|-
 
|-
|10.37
+
| 08.55
| Function with arguments: e.g ;int add( int a,int b)
+
|Function with arguments  
  
 
|-
 
|-
|10.43
+
| 08.57
| As an assignment
+
|Eg- int add(int a and  int b)
  
 
|-
 
|-
|10.45
+
| 09.02
| Write a program to calculate the square of a number using function.
+
|As an assignment-
  
 
|-
 
|-
| 10.50
+
| 09.03
| Watch the video available at  http://spoken-tutorial.org /What\_is\_a\_Spoken\_Tutorial
+
|Write a program to calculate the square of a number.  
  
 
|-
 
|-
|10.53
+
| 09.07
| It summarises the Spoken Tutorial project
+
|Watch the video available at the link shown below
  
 
|-
 
|-
|10.56
+
| 09.11
| If you do not have good bandwidth, you can download and watch it
+
|It summarizes the Spoken Tutorial project
  
 
|-
 
|-
| 11.01
+
| 09.14
| The Spoken Tutorial Project Team
+
|If you do not have good bandwidth, you can download and watch it
  
 
|-
 
|-
|11.03
+
| 09.18
| Conducts workshops using spoken tutorials
+
|The Spoken Tutorial Project Team
  
 
|-
 
|-
|11.07
+
| 09.21
| Gives certificates to those who pass an online test
+
|Conducts workshops using spoken tutorials
  
 
|-
 
|-
|11.11
+
| 09.24
| For more details, please write to contact@spoken-tutorial.org
+
|Gives certificates to those who pass an online test
  
 
|-
 
|-
| 11.19
+
| 09.28
| Spoken Tutorial Project is a part of the Talk to a Teacher project
+
|For more details, please write to, contact@spoken-tutorial.org
  
 
|-
 
|-
|11.23
+
|09.35
| It is supported by the National Mission on Education through ICT, MHRD, Government of India
+
|Spoken Tutorial Project is a part of Talk to a Teacher project
  
 
|-
 
|-
|11.30
+
| 09.40
| More information on this Mission is available at: http://spoken-tutorial.org\NMEICT-Intro
+
|It is supported by the National Mission on Education through ICT, MHRD, Government of India
 +
 
  
 
|-
 
|-
| 11.35
+
| 09.47
| This is Ashwini Patil from IIT Bombay
+
|More information on this Mission is available at the link shown below
  
 
|-
 
|-
|11.39
+
| 09.52
| Thank You for joining
+
|This is Ashwini Patil from IIT Bombay
  
|}
+
|-
 +
| 09.55
 +
|Thank You for joining.

Revision as of 12:54, 3 February 2014

Time Narration


00.01 Welcome to the spoken tutorial on Functions in C and C++
00.06 In this tutorial we will learn,
00.09 What is a function
00.11 Syntax of a function
00.13 Significance of return statement
00.16 We will do this through examples.
00.18 We will also see some common errors and their solutions.
00.22 To record this tutorial, I am using,
00.25 Ubuntu Operating System version 11.10
00.30 gcc and g++ Compiler version 4.6.1 .


00.35 Let us start with the introduction to functions
00.39 A function is a self-contained program executing a specific task.
00.44 Every program consists of one or more functions.
00.49 Once executed, the control will be returned back from where it was accessed.
00.55 Let us see the syntax for function.
00.59 ret-type defines the type of data that the function returns.
01.05 fun_name defines the name of the function.


01.09 parameters is the list of variable names and their types.
01.14 We can specify an empty parameter list.
01.17 This is called as functions without arguments.
01.21 And this is called as functions with arguments.
01.27 Let us see a program using void.
01.29 I have already typed the program on the editor.
01.32 So I will open it.


01.35 Note that our filename is function.
01.38 And I have saved the file with the extentsion .c
01.43 Let me explain the code.


01.45 This is our header file.
01.47 Before using any function, it must be defined.
01.51 Here we have defined a function called add.
01.54 Note that add function is without any arguments.
01.58 And the return type is void.
02.01 There are two types of functions-


02.03 User-defined that is our add function and
02.06 Pre-defined that is printf and main function


02.12 Here we have initialized a and b by assigning them values as 2 and 3
02.19 Here we have declared a variable c.
02.21 Then we add the values of a and b.
02.24 The result is stored in c.
02.27 Then we print the result.
02.29 This is our main function.
02.32 Here we call the add function.
02.34 The addition operation will be performed and the result will be printed.
02.39 Now click on Save.
02.42 Let us execute the program.
02.45 Please open the terminal window by pressing Ctrl, Alt and T keys simultaneously.
02.53 To compile, type gcc function dot c hyphen o fun


03.00 To execute, type ./fun


03.05 We see the output is displayed as Sum of a and b is 5


03.10 Now come back to our program.
03.13 Functions contains special identifiers called as parameters or arguments.
03.20 Let us see the same example with arguments.
03.23 I will change a few things here.
03.27 Type int add(int a, int b)


03.32 Here we have declared a function add.


03.36 int a and int b are the arguments of the function add.
03.41 Let us delete this.
03.42 No need to initialize a and b here.
03.46 Delete the printf statement.
03.49 Type int main()


03.52 Let us declare a variable sum here.


03.54 Type int sum;


03.57 Then type sum = add(5,4);


04.03 Here we call the add function.
04.05 Then we pass the parameters as 5 and 4.
04.10 5 will be stored in a and 4 will be stored in b.
04.14 The addition operation will be performed.
04.18 Let us now print the result.
04.20 Hence type here
04.21 printf(“Sum is %d\n”,sum);
04.27 Delete this, as we have already called the function above.
04.32 Type return 0;


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


04.41 Click on Save
04.43 Let us execute the program.
04.45 Come back to our terminal.
04.48 Now compile the program as before.
04.50 Let us execute.


04.52 The output is displayed as Sum is 9


04.57 Now let us see how to execute the same program in C++.
05.02 Come back to our program.
05.04 Let me change a few things here.


05.07 First press Shift, Ctrl and S keys simultaneously.
05.12 Now save the file with .cpp extension.
05.18 Click on Save.
05.19 First we will change the header file as <iostream>


05.24 We will include the using statement here.
05.28 The function declaration is same in C++.
05.32 So there is no need to change anything here.
05.37 Now replace the printf statement with the cout statement, as we use cout<< function to print a line in C++.
05.48 We don't need the format specifier and \n here.


05.52 Delete the comma.
05.54 Now, type two opening angle brackets
05.58 After sum , again type two opening angle brackets.
06.03 Within double quotes, type backslash n.
06.07 Delete this closing bracket.
06.09 Now Click on Save.
06.11 Let us compile the program.
06.14 Come back to the terminal.
06.16 Type g++ function dot cpp hyphen o fun1


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


06.31 Press Enter.
06.34 Type ./fun1


06.38 The output is displayed as: Sum is 9
06.42 Now we will see the common errors which we can come across.
06.47 Suppose here, we type x in place of 4.
06.51 I will retain the rest of the code as it is.
06.55 Click on Save.
06.58 Let us compile the program.
07.02 We see an error at line no. 10.
07.06 x was not declared in this scope.
07.09 This is because x is a character variable.
07.13 It was not declared anywhere.
07.15 And our add function has integer variable as an argument.
07.21 So, there is a mismatch in return type and return value.
07.25 Now come back to our program.
07.27 Let us fix the error.
07.30 Type 4 at line no. 10.
07.32 Click on Save.
07.35 Let us execute again.
07.37 Let me clear the prompt.
07.40 Compile the program as before.
07.42 Yes! it is working.
07.45 Now we will see another common error which we can come across.
07.50 Suppose here we pass only one parameter.
07.55 Delete 4.
07.56 Click on Save
07.58 Switch to the terminal.
08.00 Let us compile.
08.03 We see an error at line no 10.
08.05 too few arguments to function 'int add (int, int)'
08.11 Switch back to our program.


08.14 You can see here we have two parameters
08.19 int a and int b.
08.22 And here we are passing only one parameter.
08.25 Hence it is giving an error.
08.27 Let us fix the error.
08.29 Type 4.
08.31 Click on Save .
08.34 Switch to the terminal.
08.36 Let us execute again.
08.39 Yes it is working!
08.42 Come back to our slides.


08.44 To summarise, in this tutorial we have learnt -
08.49 Function
08.50 Syntax of function
08.51 Function without arguments
08.53 Eg- void add()
08.55 Function with arguments
08.57 Eg- int add(int a and int b)
09.02 As an assignment-
09.03 Write a program to calculate the square of a number.
09.07 Watch the video available at the link shown below
09.11 It summarizes the Spoken Tutorial project
09.14 If you do not have good bandwidth, you can download and watch it
09.18 The Spoken Tutorial Project Team
09.21 Conducts workshops using spoken tutorials
09.24 Gives certificates to those who pass an online test
09.28 For more details, please write to, contact@spoken-tutorial.org
09.35 Spoken Tutorial Project is a part of Talk to a Teacher project
09.40 It is supported by the National Mission on Education through ICT, MHRD, Government of India


09.47 More information on this Mission is available at the link shown below
09.52 This is Ashwini Patil from IIT Bombay
09.55 Thank You for joining.

Contributors and Content Editors

Ashwini, Kavita salve, Krupali, PoojaMoolya, Pratik kamble, Sandhya.np14