C-and-C++/C4/Function-Call/English-timed

From Script | Spoken-Tutorial
Revision as of 11:26, 17 October 2013 by Krupali (Talk | contribs)

Jump to: navigation, search
Time' Narration
00:02 Welcome to the spoken tutorial on Function call in C and C++
00:08 In this tutorial we will learn about type of function calls namely, call by value.
00:15 call by reference.
00:17 We will do this through an example.
00:20 To record this tutorial, I am usingUbuntu Operating system version 11.10gcc and g++ Compiler version 4.6.1
00:32 Let us start with the introduction to functions call by value
00:37 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:46 Before passing to the function.
00:49 Changes made to the arguments inside the function will remain in the function.
00:55 It will not be affected outside the function.
00:59 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 filename is callbyval.c.


01:13 In this program we will calculate the cube of a number. Let me explain the code now.
01:20 This is our header file
01:22 Here we have function cube having an argument as int x.
01:28 In this function we calculate the cube of x and return the value of x.
01:34 This is our main function.
01:37 Here we give the value of n as 8. n is an integer variable
01:44 Then we call the function cube.
01:46 And print the value of n and the cube of n.
01:50 And This is our return statement.
01:53 Now let us execute the program.


01:55 open the terminal window by pressing ' Ctrl, Alt and T keys simultaneously on your keyboard.
02:03 To compile , type
02:04 gcc callbyval.c -o val. Press Enter
02:13 Now type ./val.Press Enter
02:17 The output is displayed as Cube of 8 is 512.
02:23 Now we will see function call by reference.
02:27 Let us go back to our slides.
02:29 It is another method of passing arguments to the function.
02:34 This method copies the address of the argument instead of the value.
02:40 Changes made to the arguments inside a function can affect them outside.
02:46 In this we need to declare the arguments as pointer type.
02:51 Let us see an example on function call by reference.
02:55 Note that our filename is callbyref.c


02:59 This is our headerfile as stdio.h
03:03 Then we have function swap


03:06 This function will exchange the values of the variables.


03:11 Value of *a will be stored in value of *b and vice-versa.
03:16 You can see that the arguments passed in the function are pointer type.
03:22 Here we have declared an integer variable t.
03:25 First value of *a is stored in t.
03:29 Then value of *b is stored in *a.
03:33 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:43 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:54 &i and &j will give the memory address of i and j.
04:00 First we print the values before swapping.
04:05 Then we call the function swap
04:07 And then we print the values after swapping.
04:11 And This is our return statement.
Click on Save
04:14 Now let us execute the program
04:17 Switch back to our terminal
04:19 To compile type
04:22 gcc callbyref.c -o ref. Press Enter
04:30 Now type ./ref. Press Enter


04:34 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:45 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:58 This is the second program function callbyreference .
05:02 Note that our filename is callbyref.cpp
05:07 Let us go through the code now
05:09 This is ou header file as iostream


05:13 Here we are using the std namespace
05:16 The function declaration is same in C++.
05:20 In this we pass the arguments as &x and &y.
05:25 This will give the memory address of x and y.
05:30 Then we swap the values.
05:33 Rest of the code is similar to our C code.
05:37 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:49 To compile type; g++callbyref.cpp -o ref1 press Enter
06:01 Now Type ./ref1, Press Enter
06:06 Here it is displayed as:
06:07 Enter values of a and b
06:10 I will enter as 4 and 3
06:14 The output is displayed
06:16 Before swapping a and b: 4 and 3
06:20 After swapping a and b: 3 and 4
06:24 This brings us to the end of this tutorial.
06:27 Let us go back to our slides.
06:30 Let us summarize, In this tutorial we learn't:
06:33 Function call by value.
06:35 And Function call by reference.
06:38 As an assignment
06:39 Write a similar program to calculate the cube of a number.
06:43 Using call by value in C++.
06:47 Watch the video available at the link shown below ,http://spoken-tutorial.org /What\_is\_a\_Spoken\_Tutorial
06:50 It summarises the Spoken Tutorial project
06:53 If you do not have good bandwidth, you can download and watch it
06:57 The Spoken Tutorial Project Team
06:59 Conducts workshops using spoken tutorials
07:02 Gives certificates to those who pass an online test
07:05 For more details, please write to contact@spoken-tutorial.org
07:12 Spoken Tutorial Project is a part of the Talk to a Teacher project
07:16 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: http://spoken-tutorial.org\NMEICT-Intro
07:27 This is Ashwini Patil from IIT Bombay. Signing off.

Thank You for joining

Contributors and Content Editors

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