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

From Script | Spoken-Tutorial
Jump to: navigation, search
(Created page with '{| border=1 || ''Time''' || '''Narration''' |- | 00.01 | Welcome to the spoken tutorial on '''Functions in C and C++''' |- |00.07 | In this tutorial we will learn, |- |00.10…')
 
 
(26 intermediate revisions by 5 users not shown)
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:29
|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:45
| 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
+
|01:09
|Another syntax for functions  is '''ret_type function name  an empty parameter list
+
|'''parameters''' is the list of '''variable''' names and their types.
  
 +
|-
 +
| 01:14
 +
|We can specify an '''empty parameter list'''.
  
 
|-
 
|-
|01.30
+
| 01:18
| This is called as functions without arguments.
+
|This is called as '''functions without arguments. '''
  
 
|-
 
|-
|01.35
+
| 01:21
| And This is called as functions with arguments.
+
|And this is called as '''functions with arguments. '''
  
 
|-
 
|-
| 01.40
+
|01:26
| Let us move on to  our program  
+
|Let us see a program using '''void. '''
  
 
|-
 
|-
|01.43
+
|01:29
| I have already typed the program on the editor
+
|I have already typed the program on the editor.
  
 
|-
 
|-
|01.46
+
|01:32
| Let me open it
+
|So I will open it.
  
 
|-
 
|-
|01.50
+
|01:35
| Note that our filename is ''' void function.c ''' In this program we will calculate the sum of two numbers  using function..
+
|Note that our file name is '''function. '''
 +
 
 
|-
 
|-
|02.03
+
| 01:38
|Let me explain the code now.
+
|And I have saved the file with the extension '''.c''' (dot c).
  
 +
|-
 +
| 01:43
 +
|Let me explain the code.
  
 
|-
 
|-
| 02.06
+
| 01:45
| This is our '''header file'''
+
|This is our '''header file. '''
  
 
|-
 
|-
| 02.09
+
|01:47
| Before using any function it must be defined
+
|Before using any '''function''', it must be defined.
  
 
|-
 
|-
|02.14
+
|01:51
| Here we have declared a''' function''' called '''add'''
+
|Here we have defined a '''function''' called '''add.'''  
  
 
|-
 
|-
|02.18
+
|01:54
| Note that '''add function''' is without any '''arguments'''
+
|Note that '''add function''' is without any '''arguments.'''  
  
 
|-
 
|-
|02.22
+
|01:58
| And the return type is''' void'''
+
|And the return type is '''void. '''
  
 
|-
 
|-
| 02.25
+
| 02:01
| There are two types of functions
+
|There are two types of functions-
  
 
|-
 
|-
|02.27
+
| 02:03
| First User-defined function that is our add function and  
+
| '''User-defined''' that is our '''add function''' and  
  
 
|-
 
|-
|02.33
+
| 02:06
| Pr-defined function that is printf and main function
+
| '''Pre-defined''' that is '''printf''' and '''main function '''.
  
 
|-
 
|-
| 02.39
+
| 02:12
|   Here we have initialized a and b by assigning them values 2 and 3
+
| Here we have initialized '''a''' and '''b''' by assigning them values as 2 and 3.
  
 
|-
 
|-
| 02.47
+
| 02:19
| Then  we have declared a variable '''c'''
+
| Here we have declared a variable '''c. '''
  
 
|-
 
|-
|02.51
+
| 02:21
| we add the values of '''a''' and '''b'''
+
|Then we add the values of a and b.
  
 
|-
 
|-
|02.53
+
| 02:24
| '''The result is stored in c'''
+
|The result is stored in c.
  
 
|-
 
|-
| 02.57
+
| 02:27
|   Then we print the result
+
|Then we print the result.
  
 
|-
 
|-
| 03.00
+
| 02:29
| This is our main function
+
| This is our '''main function. '''
  
 
|-
 
|-
| 03.03
+
| 02:32
|Inside the main function, we call the add function
+
|Here we call the '''add''' function.
  
 
|-
 
|-
|03.07
+
| 02:34
| The addition operation will be performed and the result will be printed.
+
|The addition operation will be performed and the result will be printed.  
  
 
|-
 
|-
| 03.13
+
| 02:39
| Now click on Save
+
| Now click on '''Save.'''
  
 
|-
 
|-
|03.15
+
| 02:42
| Let us execute the program
+
|Let us execute the program.
  
 
|-
 
|-
| 03.17
+
| 02:45
| Please open the terminal window by pressing Ctrl, Alt and T keys simultaneously on your keyboard
+
|Please open the terminal window by pressing '''Ctrl, Alt''' and '''T ''' keys simultaneously.
  
 
|-
 
|-
| 03.28
+
| 02:53
| To compile type
+
|To compile, type '''gcc  function dot c  hyphen o  fun'''.
  
 
|-
 
|-
|03.29
+
| 03:00
| '''gcc void function.c -o void '' and press enter
+
|To execute, type '''./fun''' (dot slash fun).
  
 
|-
 
|-
| 03.40
+
| 03:05
| To execute, type
+
|We see the output is displayed as '''Sum of a and b is 5 '''.
  
'''./void'''
+
|-
 +
| 03:10
 +
|Now come back to our program.  
  
 
|-
 
|-
| 03.45
+
| 03:13
| The output is displayed as'''Sum of a and b is 5'''
+
|Functions contain special identifiers called as '''parameters''' or '''arguments.'''  
  
 
|-
 
|-
|03.50
+
| 03:20
| Now come back to our program
+
|Let us see the same example with arguments.
  
 
|-
 
|-
|03.53
+
| 03:23
| Functions contains special identifiers called as parameters or arguments
+
|I will change a few things here.
  
 
|-
 
|-
|04.00
+
| 03:27
| Now we will  see the same example with arguments
+
|Type '''int add(int a, int b) '''
  
 
|-
 
|-
| 04.03
+
| 03:32
| |I will  change a few things here. Press ''shift'' ''Ctrl'' & ''S'' key  simultaneously on your keyboard.
+
|Here we have declared a '''function add. '''
  
 
|-
 
|-
|04.14
+
| 03:36
| Now save the file as'' Function.c'' .Click on ''Save''.
+
|'''int a''' and '''int b''' are the '''arguments''' of the function  '''add.'''
 +
 
 
|-
 
|-
|04.24
+
| 03:41
| Replace the void key word  with ''int''  and within the (int a, int b).;
+
|Let us delete this.No need to initialize a and b here.  
 +
 
 
|-
 
|-
|04.34
+
| 03:46
| Click on save
+
|Delete the '''printf '''statement.
  
 +
|-
 +
| 03:49
 +
|Type '''int main() '''
  
 
|-
 
|-
|04.37
+
| 03:52
| Here int a''' and '''int b''' are the '''arguments''' of the '''function add'''
+
| Let us declare a variable '''sum''' here.
  
 
|-
 
|-
| 04.44
+
| 03:54
| Now  delete this
+
|Type '''int sum; '''
  
 
|-
 
|-
|04.47
+
| 03:57
| No need to initialize a and b here. Now replace the  void keyword  again with the ''int '' keyword  and click on save
+
|Then type '''sum = add(5,4); '''
  
 +
|-
 +
| 04:03
 +
|Here we call the '''add function.'''
  
 
|-
 
|-
| 04.58
+
| 04:05
| Let us declare a variable sum here
+
|Then we pass the parameters as 5 and 4.
  
 
|-
 
|-
|05.01
+
| 04:10
| type int sum;
+
|5 will be stored in '''a''' and 4 will be stored in ''' b. '''
  
 
|-
 
|-
| 05.05
+
| 04:14
|Press enter
+
|The addition operation will be performed.
  
 
|-
 
|-
|05.06
+
| 04:18
|And type  '''sum = add(5,4);'''
+
|Let us now print the result.
  
 
|-
 
|-
|05.19
+
| 04:20
| Here we call the '''add function'''
+
|Hence type here '''printf(“Sum is %d\n”, sum);'''
  
 
|-
 
|-
|05.22
+
| 04:27
| Then we pass the arguments as 5 and 4
+
|Delete this, as we have already called the function above.
  
 
|-
 
|-
|05.26
+
| 04:32
| 5 will be stored in a and 4 will be stored in b
+
|Type '''return 0;'''
  
 
|-
 
|-
| 05.31
+
| 04:36
| | The addition operation will be performed
+
|A '''non-void function''' must use a '''return''' statement that returns a value.
  
 
|-
 
|-
|05.34
+
| 04:41
| The returned value c will be stored in sum.  
+
|Click on '''Save'''.
 +
 
 
|-
 
|-
| 05.38
+
| 04:43
| Now delete this add  as we have already called the function above
+
|Let us execute the program.
  
 
|-
 
|-
| 05.44
+
| 04:45
| And Type
+
|Come back to our terminal.
  
 
|-
 
|-
|05.45
+
| 04:48
| return 0; Now click on save
+
|Now compile the program as before.
  
 
|-
 
|-
|05.51
+
| 04:50
| A '''non-void function''' must use a '''return''' statement that returns a value.
+
|Let us execute.  
  
 
|-
 
|-
| 05.58
+
| 04:52
| Let us execute the program
+
|The output is displayed as '''Sum is 9 '''.
  
 
|-
 
|-
|06.00
+
| 04:57
| Come back to a  terminal
+
|Now let us see how to execute the same program in C++.
  
 
|-
 
|-
| 06.03
+
| 05:02
| Type '''gcc function.c -o fun''' and press enter
+
|Come back to our program.
  
 +
|-
 +
| 05:04
 +
|Let me change a few things here.
  
 
|-
 
|-
|06.13
+
| 05:07
|To execute
+
|First press '''Shift, Ctrl and S''' keys simultaneously.
  
'''./fun''' press enter
+
|-
 +
| 05:12
 +
|Now save the file with '''.cpp''' extension.
  
 
|-
 
|-
| 06.19
+
| 05:18
| the output is displayed as
+
|Click on '''Save'''. First we will change the header file as <iostream>
  
 
|-
 
|-
|06.21
+
| 05:24
| '''The Sum of a & b is 9'''
+
|We will include the '''using''' statement here.
  
 
|-
 
|-
| 06.25
+
| 05:28
|NOW  WE  WILL EXECUTE THE SAME PROGRAM IN C++
+
|The '''function declaration''' is same in C++.
  
 
|-
 
|-
|06.29
+
| 05:32
| Come back to our program. I will edit the same code again press ''Shift''''Ctrl'' & ''S'' key simultaneously on your keyboard
+
|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.
  
 
|-
 
|-
|06.41
+
| 05:52
| Now Save the file''' '''with an extension ''' .cpp ''' and click on save
+
|Delete the '''comma.'''  
  
 
|-
 
|-
| 06.47
+
| 05:54
| Let us change the header file as ''iostream''
+
|Now, type two opening '''angle brackets'''.
  
 
|-
 
|-
| 06.52
+
| 05:58
| Now include the '''using '''statement. Click on save
+
|After sum , again type two opening '''angle brackets'''.  
  
 
|-
 
|-
| 07.00
+
| 06:03
|   The function declaration is same in C++
+
|Within double quotes, type '''backslash n'''.
  
 
|-
 
|-
|07.04
+
| 06:07
| So there is no need to change anything here
+
|Delete this closing bracket.
  
 
|-
 
|-
| 07.07
+
| 06:09
| Now replace the '''printf '''statement with  the '''cout''' statement
+
|Now Click on '''Save. '''
 
+
  
 
|-
 
|-
| 07.13
+
| 06:11
| Delete  the '''format specifier''' and '''\n'''
+
|Let us compile the program.
  
 
|-
 
|-
|07.16
+
| 06:14
| delete the comma
+
|Come back to our terminal.
  
 
|-
 
|-
| 07.17
+
| 06:16
| Type two opening angle brackets. Delete the closing  bracket here
+
|Type '''g++ function dot cpp hyphen o fun1 '''
  
 
|-
 
|-
|07.23
+
| 06:23
| Again  type  two opening angle brackets
+
|Here we have '''fun1''', this is because we don't want to overwrite the output file '''fun'''.
  
 
|-
 
|-
|07.25
+
| 06:31
| and within the double quotes  type '''backslash n'''
+
|Press '''Enter. '''
  
 
|-
 
|-
|07.29
+
| 06:34
| We use the cout function to print the line in C++ 
+
|Type '''./fun1 '''
|-
+
| 07.34
+
| Now Click on''' save'''
+
  
 
|-
 
|-
| 07.37
+
| 06:38
| Let us exeute  the program
+
|The output is displayed as: Sum is 9
  
 
|-
 
|-
|07.39
+
| 06:42
| Come back to our terminal
+
|Now we will see the common errors which we can come across.
  
 
|-
 
|-
| 07.42
+
| 06:47
| To compiletype '''g++ function.cpp -o fun1'''
+
|Suppose here, we type x in place of 4.  
  
 
|-
 
|-
|07.52
+
| 06:51
| Here we have fun1, because we don't want to overwrite the output parameter fun for the   file fun.c .
+
|I will retain the rest of the code as it is.  
  
 
|-
 
|-
|08.02
+
| 06:55
| Now press''' Enter'''
+
|Click on '''Save.'''  
  
 
|-
 
|-
|08.05
+
| 06:58
| To execute
+
|Let us compile the program.
  
 
|-
 
|-
|08.06
+
| 07:02
| Type'''./fun1''' And press enter
+
|We see an error at line no. 10.  
  
 
|-
 
|-
| 08.12
+
| 07:06
| The output is displayed as:  
+
|''' 'x' was not declared in this scope'''.
+
|-
+
|08.14
+
| '''The sum of a & b is 9.'''
+
  
 
|-
 
|-
| 08.16
+
| 07:09
|we can see that  the output is similar to our c code
+
|This is because 'x' is a '''character''' variable.
|-
+
|08.20
+
| Let us  see some  common errors which we can come across.
+
|-
+
|08.24
+
| Come back to our program.
+
  
 
|-
 
|-
|08.26
+
| 07:13
| Suppose here at line no-11 . I will type '''x''' in the place of 4.
+
|It was not declared anywhere.  
  
 
|-
 
|-
|08.32
+
| 07:15
| I will retain the rest of the code as it is.
+
|And our '''add''' function has '''integer''' variable as an '''argument'''.  
  
 
|-
 
|-
| 08.36
+
| 07:21
|Now click on  Save
+
|So, there is a mismatch in '''return type''' and '''return value'''.
  
 
|-
 
|-
| 08.38
+
| 07:25
|Let us execute the  program
+
|Now come back to our program.  
|-
+
|08.40
+
| Come back to our terminal.
+
|-
+
|08.44
+
| Let us  compile  as  before
+
  
 
|-
 
|-
| 08.48
+
| 07:27
| We see an error  
+
|Let us fix the error.  
|-
+
|08.50
+
| '''x '''was not declared  in this scope. come back to our program
+
  
 
|-
 
|-
|08.54
+
| 07:30
| This is because '''x''' is a '''character''' variable
+
|Type 4 at line no. 10.
  
 
|-
 
|-
|08.58
+
| 07:32
| And our '''add''' function has '''integer''' variable as an '''argument'''
+
|Click on '''Save. '''
  
 
|-
 
|-
|09.04
+
| 07:35
| So there is a mismatch in return type and return value.
+
|Let us execute again.  
  
 
|-
 
|-
| 09.08
+
| 07:37
| Now Let us fix the error
+
|Let me clear the prompt.
 
+
 
+
  
 
|-
 
|-
|09.10
+
| 07:40
| Type 4 here. Click on '''Save'''
+
|Compile the program as before.  
  
 
|-
 
|-
|09.15
+
| 07:42
| Let us execute
+
|Yes! It is working.
  
 
|-
 
|-
|09.17
+
| 07:45
| Come back to our terminal. Let me clear the prompt.
+
|Now we will see another common error which we can come across.
  
 
|-
 
|-
|09.21
+
| 07:50
| Let us compile as before, execute as before
+
|Suppose here we pass only one parameter. 
  
 
|-
 
|-
|09.27
+
| 07:55
| Yes! it is working
+
|Delete 4. Click on '''Save'''.
  
 
|-
 
|-
| 09.29
+
| 07:58
|now we will see another common error .Come back to our program
+
|Switch to the terminal.
  
 
|-
 
|-
|09.34
+
| 08:00
| here we  will  pass only 1 argument
+
| Let us compile.We see an error at line no 10.
  
 
|-
 
|-
|09.39
+
| 08:06
| delete 4
+
|'''too few arguments to function 'int add (int, int)' '''
  
 
|-
 
|-
| 09.40
+
| 08:11
| Now  Click on '''Save''' .
+
|Switch back to our program.  
  
 
|-
 
|-
| 09.43
+
| 08:14
| Let us see, what happens come back to our terminal.
+
|You can see here we have two parameters
  
 
|-
 
|-
| 09.47
+
| 08:19
| Let us compile as before
+
|'''int a '''and '''int b. '''
 
+
 
+
  
 
|-
 
|-
| 09.49
+
| 08:22
|We see error too few arguments to few functions  int  'add''''
+
|And here we are passing only one parameter.
 
+
  
 
|-
 
|-
| 09.54
+
| 08:25
| Come back to our  program
+
|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 summarize, in this tutorial we have learnt -
  
 
|-
 
|-
|10.26
+
| 08:49
| Let us summaries ,In this tutorial we learn't
+
|'''function''' Syntax of '''function'''
  
 
|-
 
|-
|10.29
+
| 08:51
| Functions
+
|'''function without arguments'''
|-
+
| 10.31
+
| Syntax of function
+
  
 
|-
 
|-
|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, int b)
  
 
|-
 
|-
|10.45
+
| 09:02
| Write a program to calculate the square of a number using function.
+
|As an assignment- Write a program to calculate the square of a number.  
  
 
|-
 
|-
| 10.50
+
| 09:07
| Watch the video available at http://spoken-tutorial.org /What\_is\_a\_Spoken\_Tutorial
+
|Watch the video available at the link shown below.
  
 
|-
 
|-
|10.53
+
| 09:11
| It summarises the Spoken Tutorial project  
+
|It summarizes the Spoken Tutorial project.
  
 
|-
 
|-
|10.56
+
| 09:14
| If you do not have good bandwidth, you can download and watch it
+
|If you do not have good bandwidth, you can download and watch it.
  
 
|-
 
|-
| 11.01
+
| 09:18
| The Spoken Tutorial Project Team  
+
|The Spoken Tutorial Project Team:
  
 
|-
 
|-
|11.03
+
| 09:21
| Conducts workshops using spoken tutorials  
+
|Conducts workshops using spoken tutorials,
  
 
|-
 
|-
|11.07
+
| 09:24
| Gives certificates to those who pass an online test  
+
|Gives certificates to those who pass an online test.
  
 
|-
 
|-
|11.11
+
| 09:28
| For more details, please write to contact@spoken-tutorial.org
+
|For more details, please write to, contact@spoken-tutorial.org  
  
 
|-
 
|-
| 11.19
+
|09:35
| Spoken Tutorial Project is a part of the Talk to a Teacher project
+
|Spoken Tutorial Project is a part of Talk to a Teacher project.
  
 
|-
 
|-
|11.23
+
| 09:40
| 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.
  
 
|-
 
|-
|11.30
+
| 09:47
| 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.
  
 
|-
 
|-
| 11.35
+
| 09:52
| This is Ashwini Patil from IIT Bombay
+
|This is Ashwini Patil from IIT Bombay.
  
 
|-
 
|-
|11.39
+
| 09:55
| Thank You for joining
+
|Thank You for joining.
 
+
|}
+

Latest revision as of 12:54, 24 March 2017

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:29 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:45 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:18 This is called as functions without arguments.
01:21 And this is called as functions with arguments.
01:26 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 file name is function.
01:38 And I have saved the file with the extension .c (dot 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 (dot slash 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 contain 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.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 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. 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 our 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. Click on Save.
07:58 Switch to the terminal.
08:00 Let us compile.We see an error at line no 10.
08:06 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 summarize, in this tutorial we have learnt -
08:49 function 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, int b)
09:02 As an assignment- 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