Difference between revisions of "C-and-C++/C4/Function-Call/English-timed"

From Script | Spoken-Tutorial
Jump to: navigation, search
 
(9 intermediate revisions by 4 users not shown)
Line 1: Line 1:
{| border=1
+
{| border = 1
|| ''Time'''
+
|'''Time'''
|| '''Narration'''
+
|'''Narration'''
 +
 
 +
|-
 +
| 00:01
 +
|Welcome to the spoken tutorial on '''Function calls in C and C++'''.
 +
 
 
|-
 
|-
| 00:02
+
| 00:07
| Welcome to the spoken tutorial on '''Function call in C and C++'''
+
|In this tutorial, we will learn about type of function calls namely-
  
 
|-
 
|-
| 00:08
+
| 00:13
| In this tutorial we will learn about type of  function calls namely, call by value.
+
| call by value call by reference.  
  
 
|-
 
|-
| 00:15
+
| 00:16
|call by reference.
+
|We will do this through an example.  
  
 
|-
 
|-
| 00:17
+
| 00:19
|We will do this through an example.
+
|To record this tutorial, I am using '''Ubuntu Operating system '''version 11.10,
  
 
|-
 
|-
| 00:20
+
| 00:26
| To record this tutorial, I am using'''Ubuntu Operating system''' version 11.10'''gcc''' and g'''++ Compiler''' version 4.6.1
+
|'''gcc''' and '''g++''' Compiler version 4.6.1.
  
 
|-
 
|-
|00:32
+
| 00:31
|Let us start with the introduction to '''functions call by value'''
+
| Let us start with the introduction to '''function call by value'''.
  
 
|-
 
|-
| 00:37
+
| 00:35
|It is a method of passing '''arguments''' to the function.
+
|It is a method of passing '''arguments''' to the function.  
  
 
|-
 
|-
 
| 00:40
 
| 00:40
|When we pass a variable by value it makes a copy of the variable.
+
| When we pass a variable by value, it makes a copy of the variable
  
 
|-
 
|-
| 00:46
+
| 00:45
|Before passing to the function.
+
|before passing to the function.
  
 
|-
 
|-
| 00:49
+
|00:48
|Changes made to the arguments inside the function will remain in the function.
+
|Changes made to the arguments, inside the function, will remain in the function.  
  
 
|-
 
|-
| 00:55
+
|00:54
|It will not be affected outside the function.
+
|It will not be affected outside the function.  
  
 
|-
 
|-
| 00:59
+
|00:58
| Let us see a program on function call by value.
+
|Let us see a program on 'function call by value'.  
  
 
|-
 
|-
 
| 01:02
 
| 01:02
|I have already typed the program on the editor. i will just open it.
+
|I have already typed the program on the editor. I will just open it.
  
 
|-
 
|-
| 01:08
+
| 01:08
| Please note our filename is '''callbyval.c'''
+
| Please note our file name is '''callbyval.c. '''
  
 
 
|-
 
|-
 
| 01:13
 
| 01:13
|In this program we will calculate the cube of a number. Let me explain the code now.
+
|In this program, we will calculate the cube of a number. Let me explain the code now.  
  
 
|-
 
|-
| 01:20
+
| 01:19
|   This is our '''header file'''
+
|This is our '''header file'''.
  
 
|-
 
|-
| 01:22
+
| 01:21
| Here we have function''' cube '''having''' '''an''' argument '''as''' int x.'''
+
|Here we have function '''cube''' having an '''argument''' as '''int x. '''.
  
 
|-
 
|-
| 01:28
+
| 01:27
|In this function we calculate the cube of '''x''' and return the value of '''x'''.''' '''
+
|In this function, we calculate the cube of '''x''' and return the value of '''x. '''
  
 
|-
 
|-
| 01:34
+
| 01:33
| This is our '''main function.'''
+
|This is our '''main() function.'''
  
 
|-
 
|-
| 01:37
+
|01:36
|   Here we give the value of '''n''' as '''8.''' '''n''' is an integer variable
+
|Here we give the value of '''n''' as '''8''', 'n' is an integer variable.
  
 
|-
 
|-
| 01:44
+
| 01:43
|   Then we call the function''' cube.'''
+
|Then we call the function '''cube.'''
  
 
|-
 
|-
| 01:46
+
| 01:45
|And print the value of''' n '''and the cube of''' n.'''
+
|And print the value of 'n' and the cube of n.
  
 
|-
 
|-
| 01:50
+
| 01:49
| And This is our return statement.
+
|And this is our '''return''' statement.  
  
 
|-
 
|-
| 01:53
+
| 01:52
| Now let us execute the program.  
+
|Now let us execute the program.  
 
+
+
  
 
|-
 
|-
| 01:55
+
|01:54
| open the terminal window by pressing '''' Ctrl, Alt and T''' keys simultaneously on your keyboard.
+
|open the terminal window by pressing ''' Ctrl, Alt''' and '''T''' keys simultaneously on your keyboard.  
  
 
|-
 
|-
| 02:03
+
|02:02
| To compile , type
+
|To compile , type '''gcc space callbyval.c space hyphen o space val'''. Press '''Enter'''.
  
 
|-
 
|-
| 02:04
+
|02:12
|'''gcc callbyval.c -o val'''. Press''' Enter'''
+
|Now, type '''./val '''(dot slash val). Press '''Enter'''.
  
 
|-
 
|-
| 02:13
+
|02:16
|Now type '''./val'''.Press''' Enter'''
+
|The output is displayed as: '''Cube of 8 is 512.'''
  
 
|-
 
|-
| 02:17
+
|02:23
|The output is displayed as '''Cube of 8 is 512.'''
+
|Now we will see 'function call by reference'.  
  
 
|-
 
|-
| 02:23
+
|02:26
| Now we will see function call by reference.
+
|Let us go back to our slides.  
  
 
|-
 
|-
| 02:27
+
| 02:29
|Let us go back to our slides.
+
|It is another method of passing '''arguments''' to the function.  
  
 
|-
 
|-
| 02:29
+
| 02:33
| It is another method of passing '''arguments''' to the function.
+
| This method copies the address of the argument instead of the value.
  
 
|-
 
|-
| 02:34
+
| 02:39
|This method copies the address of the argument instead of the value.
+
|Changes made to the arguments, inside a function, can affect them outside.  
  
 
|-
 
|-
| 02:40
+
|02:45
|Changes made to the arguments inside a function can affect them outside.
+
|In this we need to declare the arguments as pointer type.  
  
 
|-
 
|-
| 02:46
+
| 02:50
|In this we need to declare the arguments as pointer type.
+
| Let us see an example on function call by reference.
  
 
|-
 
|-
| 02:51
+
| 02:54
|   Let us see an example on function call by reference.
+
|Note that our file name is '''callbyref.c'''.
  
 
|-
 
|-
+
| 02:59
|-
+
|This is our header file as '''stdio.h'''.
| 02:55
+
|  Note that our filename is '''callbyref.c'''
+
 
+
+
 
+
|-
+
02:59
+
| This is our headerfile as '''stdio.h'''
+
  
 
|-
 
|-
 
| 03:03
 
| 03:03
| Then we have function '''swap'''
+
|Then we have function '''swap'''.
 
+
  
 
|-
 
|-
 
| 03:06
 
| 03:06
|This function will exchange the values of the variables.
+
|This function will exchange the values of the variables.  
  
 +
|-
 +
| 03:10
 +
|Value of 'a' will be stored in value of 'b' and vice versa.
  
|-
 
| 03:11
 
|Value of ''' *a ''' will be  stored  in value of  '''*b'''  and vice-versa.
 
 
|-
 
|-
| 03:16
+
| 03:15
|You can see that the''' arguments '''passed in the function are''' pointer type.'''
+
|You can see that the '''arguments''' passed in the function are '''pointer type.'''
 +
 
 
|-
 
|-
| 03:22
+
| 03:21
| Here we have declared an '''integer''' variable '''t.'''
+
|Here we have declared an '''integer''' variable '''t.'''
  
 
|-
 
|-
 
| 03:25
 
| 03:25
| First value of ''' *a ''' is stored in '''t.'''
+
|First, value of 'a' is stored in 't'.
|-
+
| 03:29
+
|Then value of ''' *b ''' is stored in ''' *a.'''
+
  
 
|-
 
|-
| 03:33
+
| 03:28
|And Then value of '''t''' is stored in ''' *b '''.
+
|Then value of 'b' is stored in 'a'.
  
 
|-
 
|-
| 03:37 
+
| 03:32
|Like this  the values are exchanged.
+
| And then value of 't' is stored in 'b'.  
 +
 
 
|-
 
|-
|   03:40
+
| 03:37
| This is our '''main''' function.
+
| Like this, the values are exchanged.  
  
 
|-
 
|-
| 03:43
+
| 03:40
| Here we have declared two integer variables as '''i and j''' .
+
|This is our '''main()''' function.  
  
 
|-
 
|-
| 03:49
+
| 03:42
| Then we take the values of ''' i and j''' as user inputs.
+
|Here we have declared two integer variables as i and j.  
  
 
|-
 
|-
| 03:54
+
| 03:49
|'''&i''' and '''&j''' will give the memory address of '''i''' and '''j.'''
+
|Then we take the values of i and j as user inputs.  
  
 
|-
 
|-
| 04:00
+
| 03:53
|   First we print the values before swapping.
+
|Ampersand i (&i) and Ampersand j (&j) will give the memory address of i and j.
  
 
|-
 
|-
| 04:05
+
| 03:59
| Then  we call the function '''swap'''
+
|First, we print the values before swapping.
  
 
|-
 
|-
| 04:07
+
| 04:04
| And then we print the values after swapping.
+
|Then we call the function '''swap'''.
  
 
|-
 
|-
| 04:11
+
| 04:06
|And This is our return statement.
+
|And then we print the values after swapping.  
  
 
|-
 
|-
| 04:14
+
| 04:10
|Now  let us execute the program
+
|And this is our '''return''' statement.
  
 
|-
 
|-
| 04:17
+
| 04:13
|Switch back to our terminal
+
| Now let us execute the program.
  
 
|-
 
|-
| 04:19
+
| 04:16
| To compile  type
+
| Switch back to our terminal.
  
 
|-
 
|-
| 04:22
+
| 04:19
|'''gcc callbyref.c -o ref'''. Press ''' Enter'''
+
|To compile type '''gcc space callbyref dot c space hyphen o space ref'''. Press Enter.
  
 
|-
 
|-
| 04:30
+
| 04:29
|Now type''' ./ref'''. Press ''' Enter'''
+
|Now type '''dot slash ref'''. Press Enter.
 
+
 
+
  
 
|-
 
|-
| 04:34
+
| 04:33
| We see, enter the values I will enter as '''6''' and '''4'''
+
|We see, 'Enter the values'. I will enter as 6 and 4.
  
 
|-
 
|-
 
| 04:40
 
| 04:40
| The output is displayed as,before swapping''' 6 '''and''' 4'''
+
|The output is displayed as: '''Before swapping 6 and 4'''
  
 
|-
 
|-
| 04:45
+
| 04:44
|After swapping''' 4 '''and''' 6'''
+
|'''After swapping 4 and 6'''.
  
 
|-
 
|-
| 04:48
+
| 04:48
| NOW LET US SEE HOW TO EXECUTE THE SAME PROGRAM IN C++
+
|Now let us see how to execute the same program in C++.
  
 
|-
 
|-
 
| 04:53
 
| 04:53
|I have the code, lets go through it.
+
|I have the code, lets go through it.  
  
 
|-
 
|-
| 04:58
+
| 04:57
| This is the second program '''function callbyreference '''.
+
|This is the second program, function call by reference .  
  
 
|-
 
|-
| 05:02
+
| 05:01
| Note that our filename is '''callbyref.cpp'''
+
|Note that our file name is '''callbyref.cpp'''
  
 
|-
 
|-
| 05:07
+
| 05:06
|Let us go through the code now
+
|Let us go through the code.
|-
+
| 05:09
+
| This is ou header file as ''' iostream '''
+
  
+
|-
 +
| 05:08
 +
|This is our header file as '''iostream '''.
  
 
|-
 
|-
| 05:13
+
| 05:12
|Here we are   using the '''std namespace'''
+
|Here we are using the '''std namespace'''.
  
 
|-
 
|-
| 05:16
+
| 05:16
| The function declaration is same in C++.
+
|The function declaration is same in C++.  
  
 
|-
 
|-
| 05:20
+
| 05:19
|In this we pass the arguments as '''&x''' and '''&y'''.
+
|In this we pass the arguments as ampersand x and ampersand y.  
  
 
|-
 
|-
 
| 05:25
 
| 05:25
|This will give the memory address of '''x''' and '''y'''.
+
| This will give the memory address of x and y.  
  
 
|-
 
|-
| 05:30
+
| 05:29
|Then we swap the values.
+
|Then we swap the values.  
  
 
|-
 
|-
| 05:33
+
| 05:32
| Rest of the code is similar to our C code.
+
|Rest of the code is similar to our C code.  
  
 
|-
 
|-
| 05:37
+
| 05:36
|The '''printf''' statement is replaced by '''cout''' and the '''scanf''' statement is replaced by ''cin '''  
+
|The '''printf''' statement is replaced by '''cout''' and the '''scanf''' statement is replaced by '''cin'''
  
 
|-
 
|-
| 05:44
+
| 05:44
| Now let us execute the program. Come back to our terminal
+
|Now let us execute the program. Come back to our terminal.
  
 
|-
 
|-
| 05:49
+
| 05:48
| To compile type'''g++callbyref.cpp -o ref1''' press''' Enter'''
+
|To compile type, '''g++ space callbyref.cpp space hyphen o space ref1''', press Enter.
  
 
|-
 
|-
| 06:01
+
| 06:00
| Now Type '''./ref1''', Press ''' Enter'''
+
|Now, type  '''dot slash ref1'''. Press Enter.
  
 
|-
 
|-
| 06:06
+
| 06:05
| Here it is displayed as:  
+
|Here it is displayed as:
  
 
|-
 
|-
 
| 06:07
 
| 06:07
|'''Enter values of a and b'''
+
|Enter values of a and b
  
 
|-
 
|-
 
| 06:10
 
| 06:10
|I will enter as '''4'''  and '''3'''
+
|I will enter as 4 and 3.
  
 
|-
 
|-
| 06:14
+
| 06:13
|The output is displayed
+
|The output is displayed.
  
 
|-
 
|-
| 06:16
+
| 06:15
|'''Before swapping a and b: 4 and 3'''
+
|'''Before swapping a and b 4 and 3'''
  
 
|-
 
|-
| 06:20
+
| 06:19
|'''After swapping a and b: 3 and 4'''
+
|'''After swapping a and b 3 and 4'''
  
 
|-
 
|-
|   06:24
+
| 06:23
| This brings us to the end of this tutorial.
+
|This brings us to the end of this tutorial.  
  
 
|-
 
|-
| 06:27
+
| 06:26
|Let us go back to our slides.
+
|Let us go back to our slides.  
  
 
|-
 
|-
| 06:30
+
| 06:30
| Let us summarize, In this tutorial we learn't:
+
|Let us summarize, In this tutorial we learnt:  
  
 
|-
 
|-
| 06:33
+
| 06:32
|Function call by value.
+
|Function call by value.  
  
 
|-
 
|-
| 06:35
+
| 06:34
|And Function call by reference.
+
|And Function call by reference.  
  
 
|-
 
|-
| 06:38
+
| 06:37
| As an assignment
+
|As an assignment,write a similar program to calculate the cube of a number,
  
 
|-
 
|-
| 06:39
+
| 06:42
|Write a similar program to calculate the cube of a number.
+
|using call by value in C++.  
  
 
|-
 
|-
| 06:43
+
| 06:46
|Using call by value in C++.
+
|Watch the video available at the link shown below.
  
 
|-
 
|-
| 06:47
+
| 06:49
| Watch the video available at the link shown below ,http://spoken-tutorial.org /What\_is\_a\_Spoken\_Tutorial
+
|It summarizes the Spoken Tutorial project.  
  
 
|-
 
|-
| 06:50
+
| 06:52
|It summarises the Spoken Tutorial project
+
|If you do not have good bandwidth, you can download and watch it.
  
 
|-
 
|-
| 06:53
+
| 06:56
|If you do not have good bandwidth, you can download and watch it
+
|The Spoken Tutorial Project Team:
  
 
|-
 
|-
| 06:57
+
| 06:58
| The Spoken Tutorial Project Team
+
|Conducts workshops using spoken tutorials.
  
 
|-
 
|-
| 06:59
+
|07:01
|Conducts workshops using spoken tutorials
+
|Gives certificates to those who pass an online test.
 
+
|-
+
| 07:02
+
|Gives certificates to those who pass an online test  
+
  
 
|-
 
|-
 
| 07:05
 
| 07:05
|For more details, please write to contact@spoken-tutorial.org
+
|For more details, please write to contact@spoken-tutorial.org.
  
 
|-
 
|-
| 07:12
+
| 07:11
| Spoken Tutorial Project is a part of the Talk to a Teacher project
+
|Spoken Tutorial Project is a part of the Talk to a Teacher project.
  
 
|-
 
|-
| 07:16
+
| 07:15
|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.
  
 
|-
 
|-
 
| 07:23
 
| 07:23
|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.
  
 
|-
 
|-
 
| 07:27
 
| 07:27
|   This is Ashwini Patil from IIT Bombay. Signing off.
+
| This is Ashwini Patil from IIT Bombay, signing off.  
  
Thank You for joining
+
|-
 
+
| 07:31
|}
+
|Thank You for joining.

Latest revision as of 11:43, 28 April 2020

Time Narration
00:01 Welcome to the spoken tutorial on Function calls in C and C++.
00:07 In this tutorial, we will learn about type of function calls namely-
00:13 call by value call by reference.
00:16 We will do this through an example.
00:19 To record this tutorial, I am using Ubuntu Operating system version 11.10,
00:26 gcc and g++ Compiler version 4.6.1.
00:31 Let us start with the introduction to function call by value.
00:35 It is a method of passing arguments to the function.
00:40 When we pass a variable by value, it makes a copy of the variable
00:45 before passing to the function.
00:48 Changes made to the arguments, inside the function, will remain in the function.
00:54 It will not be affected outside the function.
00:58 Let us see a program on 'function call by value'.
01:02 I have already typed the program on the editor. I will just open it.
01:08 Please note our file name is callbyval.c.
01:13 In this program, we will calculate the cube of a number. Let me explain the code now.
01:19 This is our header file.
01:21 Here we have function cube having an argument as int x. .
01:27 In this function, we calculate the cube of x and return the value of x.
01:33 This is our main() function.
01:36 Here we give the value of n as 8, 'n' is an integer variable.
01:43 Then we call the function cube.
01:45 And print the value of 'n' and the cube of n.
01:49 And this is our return statement.
01:52 Now let us execute the program.
01:54 open the terminal window by pressing Ctrl, Alt and T keys simultaneously on your keyboard.
02:02 To compile , type gcc space callbyval.c space hyphen o space val. Press Enter.
02:12 Now, type ./val (dot slash val). Press Enter.
02:16 The output is displayed as: Cube of 8 is 512.
02:23 Now we will see 'function call by reference'.
02:26 Let us go back to our slides.
02:29 It is another method of passing arguments to the function.
02:33 This method copies the address of the argument instead of the value.
02:39 Changes made to the arguments, inside a function, can affect them outside.
02:45 In this we need to declare the arguments as pointer type.
02:50 Let us see an example on function call by reference.
02:54 Note that our file name is callbyref.c.
02:59 This is our header file as stdio.h.
03:03 Then we have function swap.
03:06 This function will exchange the values of the variables.
03:10 Value of 'a' will be stored in value of 'b' and vice versa.
03:15 You can see that the arguments passed in the function are pointer type.
03:21 Here we have declared an integer variable t.
03:25 First, value of 'a' is stored in 't'.
03:28 Then value of 'b' is stored in 'a'.
03:32 And then value of 't' is stored in 'b'.
03:37 Like this, the values are exchanged.
03:40 This is our main() function.
03:42 Here we have declared two integer variables as i and j.
03:49 Then we take the values of i and j as user inputs.
03:53 Ampersand i (&i) and Ampersand j (&j) will give the memory address of i and j.
03:59 First, we print the values before swapping.
04:04 Then we call the function swap.
04:06 And then we print the values after swapping.
04:10 And this is our return statement.
04:13 Now let us execute the program.
04:16 Switch back to our terminal.
04:19 To compile type gcc space callbyref dot c space hyphen o space ref. Press Enter.
04:29 Now type dot slash ref. Press Enter.
04:33 We see, 'Enter the values'. I will enter as 6 and 4.
04:40 The output is displayed as: Before swapping 6 and 4
04:44 After swapping 4 and 6.
04:48 Now let us see how to execute the same program in C++.
04:53 I have the code, lets go through it.
04:57 This is the second program, function call by reference .
05:01 Note that our file name is callbyref.cpp
05:06 Let us go through the code.
05:08 This is our header file as iostream .
05:12 Here we are using the std namespace.
05:16 The function declaration is same in C++.
05:19 In this we pass the arguments as ampersand x and ampersand y.
05:25 This will give the memory address of x and y.
05:29 Then we swap the values.
05:32 Rest of the code is similar to our C code.
05:36 The printf statement is replaced by cout and the scanf statement is replaced by cin.
05:44 Now let us execute the program. Come back to our terminal.
05:48 To compile type, g++ space callbyref.cpp space hyphen o space ref1, press Enter.
06:00 Now, type dot slash ref1. Press Enter.
06:05 Here it is displayed as:
06:07 Enter values of a and b
06:10 I will enter as 4 and 3.
06:13 The output is displayed.
06:15 Before swapping a and b 4 and 3
06:19 After swapping a and b 3 and 4
06:23 This brings us to the end of this tutorial.
06:26 Let us go back to our slides.
06:30 Let us summarize, In this tutorial we learnt:
06:32 Function call by value.
06:34 And Function call by reference.
06:37 As an assignment,write a similar program to calculate the cube of a number,
06:42 using call by value in C++.
06:46 Watch the video available at the link shown below.
06:49 It summarizes the Spoken Tutorial project.
06:52 If you do not have good bandwidth, you can download and watch it.
06:56 The Spoken Tutorial Project Team:
06:58 Conducts workshops using spoken tutorials.
07:01 Gives certificates to those who pass an online test.
07:05 For more details, please write to contact@spoken-tutorial.org.
07:11 Spoken Tutorial Project is a part of the Talk to a Teacher project.
07:15 It is supported by the National Mission on Education through ICT, MHRD, Government of India.
07:23 More information on this mission is available at the link shown below.
07:27 This is Ashwini Patil from IIT Bombay, signing off.
07:31 Thank You for joining.

Contributors and Content Editors

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