C-and-C++/C2/Functions/English-timed

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

Jump to: navigation, search
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 What is a function
00.12 Syntax of a function
00.15 Significance of a return statement
00.18 Few example on functions.
00.20 We will also see some common errors and their solutions.
00.25 To record this tutorial, I am usingUbuntu Operating system version 11.10
00.33 gcc and g++ Compiler version 4.6.1
00.40 Let us start with the introduction to functions
00.43 A function is a self-contained program executing a specific task
00.50 Every program consists of one or more functions
00.56 Once executed the control will be returned back from where it was accessed
01.03 Now we will see the syntax for the function
01.18 ret-type' defines the type of data that the function returns
01.12 fun_name is the name of the function
01.16 parameters is the list of variable names and their types
01.20 Another syntax for functions is ret_type function name an empty parameter list


01.30 This is called as functions without arguments.
01.35 And This is called as functions with arguments.
01.40 Let us move on to our program
01.43 I have already typed the program on the editor
01.46 Let me open it
01.50 Note that our filename is void function.c In this program we will calculate the sum of two numbers using function..
02.03 Let me explain the code now.


02.06 This is our header file
02.09 Before using any function it must be defined
02.14 Here we have declared a function called add
02.18 Note that add function is without any arguments
02.22 And the return type is void
02.25 There are two types of functions
02.27 First User-defined function that is our add function and
02.33 Pr-defined function that is printf and main function
02.39 Here we have initialized a and b by assigning them values 2 and 3
02.47 Then we have declared a variable c
02.51 we add the values of a and b
02.53 The result is stored in c
02.57 Then we print the result
03.00 This is our main function
03.03 Inside the main function, we call the add function
03.07 The addition operation will be performed and the result will be printed.
03.13 Now click on Save
03.15 Let us execute the program
03.17 Please open the terminal window by pressing Ctrl, Alt and T keys simultaneously on your keyboard
03.28 To compile type
03.29 'gcc void function.c -o void and press enter
03.40 To execute, type

./void

03.45 The output is displayed asSum of a and b is 5
03.50 Now come back to our program
03.53 Functions contains special identifiers called as parameters or arguments
04.00 Now we will see the same example with arguments
04.03 I will change a few things here. Press shift Ctrl & S key simultaneously on your keyboard.
04.14 Now save the file as Function.c .Click on Save.
04.24 Replace the void key word with int and within the brakets type int a, int b
04.34 Click on save


04.37 Here int a and int b are the arguments of the function add
type int sum; 
04.44 Now delete this
04.47 No need to initialize a and b here. Now replace the void keyword again with the int keyword and click on save


04.58 Let us declare a variable sum here
05.01 type int sum and the semicolon;
05.05 Press enter
05.06 And type sum = add, opening bracket, closing bracket, within the bracket (5,4) at the end type a semicolon;
05.19 Here we call the add function
05.22 Then we pass the arguments as 5 and 4
05.26 5 will be stored in a and 4 will be stored in b
05.31 The addition operation will be performed
05.34 The returned value c will be stored in sum.
05.38 Now delete this add as we have already called the function above
05.44 And Type
05.45 return 0; Now click on save
05.51 A non-void function must use a return statement that returns a value.
05.58 Let us execute the program
06.00 Come back to a terminal
06.03 Type gcc function.c -o fun and press enter


06.13 To execute

./fun press enter

06.19 the output is displayed as
06.21 Sum of a & b is 9
06.25 NOW WE WILL EXECUTE THE SAME PROGRAM IN C++
06.29 Come back to our program. I will edit the same code, again press Shift'Ctrl & S key simultaneously on your keyboard


06.41 Now Save the file with an extension .cpp and click on save
06.47 Let us change the header file as iostream
06.52 Now include the using statement. Click on save
07.00 The function declaration is same in C++
07.04 So there is no need to change anything here
07.07 Now replace the printf statement with the cout statement


07.13 Delete the format specifier and \n
07.16 dTheelete the comma
07.17 Type two opening angle brackets. Delete the closing bracket here
07.23 Again type two opening angle brackets
07.25 and within the double quotes type backslash n
07.29 We use the cout function to print the line in C++
07.34 Now Click on save
07.37 Let us exeute the program
07.39 Come back to our terminal
07.42 To compile, type g++ function.cpp -o fun1
07.52 Here we have fun1, because we don't want to overwrite the output parameter fun for the file fun.c .
08.02 Now press Enter
08.05 To execute
08.06 Type./fun1 And press enter
08.12 The output is displayed as:
08.14 The sum of a & b is 9.
08.16 we can see that the output is similar to our c code
08.20 Let us see some common errors which we can come across.
08.24 Come back to our program.
08.26 Suppose here at line no-11 . I will type x in the place of 4.
08.32 I will retain the rest of the code as it is.
08.36 Now click on Save
08.38 Let us execute the program
08.40 Come back to our terminal.
08.44 Let us compile as before
08.48 We see an error
08.50 x was not declared in this scope. come back to our program
08.54 This is because x is a character variable
08.58 And our add function has integer variable as an argument
09.04 So there is a mismatch in return type and return value.
09.08 Now Let us fix the error


09.10 Type 4 here. Click on Save
09.15 Let us execute
09.17 Come back to our terminal. Let me clear the prompt.
09.21 Let us compile as before, execute as before
09.27 Yes! it is working
09.29 now we will see another common error .Come back to our program
09.34 here we will pass only 1 argument
09.39 delete 4
09.40 Now Click on Save .
09.43 Let us see, what happens come back to our terminal.
09.47 Let us compile as before


09.49 We see error too few arguments to few functions int 'add'


09.54 Come back to our program


09.56 You can see here we have two argument int a and int b
10.03 And here we are passing only one argument.
10.06 Hence it is giving an error
10.09 Let us fix the error
10.10 Type 4 ,click on save
10.13 Let us execute again
10.16 Compile as before , execute as before.
10.21 Yes it is working!Now come back to our slide
10.26 Let us summaries ,In this tutorial we learn't
10.29 Functions
10.31 Syntax of function
10.33 Function without arguments: e.g ; void add()
10.37 Function with arguments: e.g ;int add( int a,int b)
10.43 As an assignment
10.45 Write a program to calculate the square of a number using function.
10.50 Watch the video available at http://spoken-tutorial.org /What\_is\_a\_Spoken\_Tutorial
10.53 It summarises the Spoken Tutorial project
10.56 If you do not have good bandwidth, you can download and watch it
11.01 The Spoken Tutorial Project Team
11.03 Conducts workshops using spoken tutorials
11.07 Gives certificates to those who pass an online test
11.11 For more details, please write to contact@spoken-tutorial.org
11.19 Spoken Tutorial Project is a part of the Talk to a Teacher project
11.23 It is supported by the National Mission on Education through ICT, MHRD, Government of India
11.30 More information on this Mission is available at: http://spoken-tutorial.org\NMEICT-Intro
11.35 This is Ashwini Patil from IIT Bombay
11.39 Thank You for joining

Contributors and Content Editors

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