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

From Script | Spoken-Tutorial
Jump to: navigation, search
Line 1: Line 1:
+
{| border=1
Time' Narration
+
|| ''Time'''
 
+
|| '''Narration'''
00.01 Welcome to the spoken tutorial on Functions in C and C++
+
  
'''Functions in C and C++''' પરનાં સ્પોકન ટ્યુટોરીયલમાં સ્વાગત છે.  
 
00.07 In this tutorial we will learn,
 
  
આ ટ્યુટોરીયલમાં આપણે શીખીશું,  
+
|-
00.10 What is a function
+
| 00.01
 +
|  Welcome to the spoken tutorial on '''Functions in C and C++'''
  
એક '''ફંક્શન''' શું છે  
+
|-
00.12 Syntax of a function
+
|00.07
 +
| In this tutorial we will learn,
  
ફંક્શનનાં સિન્ટેક્ષ  
+
|-
00.15 Significance of a return statement
+
|00.10
 +
| What is a function
  
'''return statement''' નું મહત્વ  
+
|-
00.18 Few example on functions.
+
|00.12
 +
| Syntax of  a function
  
ફંક્શનો પર કેટલાક ઉદાહરણ.  
+
|-
00.20 We will also see some common errors and their solutions.
+
|00.15
 +
| Significance of a return statement
  
આપણે અમુક સામાન્ય એરરો અને તેમનાં ઉકેલોને પણ જોઈશું.  
+
|-
00.25 To record this tutorial, I am usingUbuntu Operating system version 11.10
+
|00.18
 +
| Few  example on functions.
  
આ ટ્યુટોરીયલને રેકોર્ડ કરવાં માટે, હું વાપરી રહ્યી છું '''ઉબુન્ટુ ઓપરેટીંગ સીસ્ટમ આવૃત્તિ 11.10'''  
+
|-
00.33 gcc and g++ Compiler version 4.6.1
+
|00.20
 +
| We will also see some common errors and their solutions.
  
'''gcc અને g++ કમ્પાઈલર આવૃત્તિ 4.6.1'''  
+
|-
00.40 Let us start with the introduction to functions
+
|00.25
 +
| To record this tutorial, I am using'''Ubuntu Operating system''' version 11.10
  
ફંક્શનોનાં રજૂઆત સાથે ચાલો શરૂઆત કરીએ  
+
|-
00.43 A function is a self-contained program executing a specific task
+
|00.33
 +
| '''gcc''' and g'''++ Compiler''' version 4.6.1
  
ફંકશન એક '''સ્વયં પર્યાપ્ત પ્રોગ્રામ''' છે જે અમુક ચોક્કસ કાર્યને એક્ઝેક્યુટ કરે છે  
+
|-
00.50 Every program consists of one or more functions
+
|00.40
 +
|Let us start with the introduction to '''functions'''
  
દરેક પ્રોગ્રામ એક અથવાં એકથી વધારે ફંક્શનો ધરાવે છે  
+
|-
00.56 Once executed the control will be returned back from where it was accessed
+
|00.43
 +
| A '''function''' is a self-contained program executing a specific task
  
એકવાર એક્ઝેક્યુટ થયા પછીથી નિયંત્રણ પોતાની એ જગ્યાએ ફરી પાછું આવશે જ્યાંથી એ એક્સેસ થયું હતું  
+
|-
01.03 Now we will see the syntax for the function
+
|00.50
 +
| Every program consists of one or more '''functions'''
  
હવે આપણે ફંક્શન માટેનાં સિન્ટેક્ષ જોઈશું  
+
|-
01.18 ret-type' defines the type of data that the function returns
+
|00.56
 +
| Once executed the control will be returned back from where it was accessed
  
'''ret-type'''' ડેટા પ્રકારને વ્યાખ્યાયિત કરે છે જે ફંક્શન પાછું આપે છે  
+
|-
01.12 fun_name is the name of the function  
+
| 01.03
 +
| Now we will  see the syntax for the function
  
'''fun_name''' ફંક્શનનું નામ છે  
+
|-
01.16 parameters is the list of variable names and their types
+
|01.18
 +
| ''ret-type''' defines the type of data that the '''function''' returns
  
'''parameters''' વેરીએબલ નામોની યાદી અને એનાં પ્રકારો છે  
+
|-
01.20 Another syntax for functions is ret_type function name an empty parameter list
+
|01.12
 +
|fun_name''' is the name of the '''function'''
  
ફંક્શન માટેનું બીજું એક સિન્ટેક્ષ છે '''ret_type function name an empty parameter list'''  
+
|-
01.30 This is called as functions without arguments.
+
|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.35 And This is called as functions with arguments.
 
  
અને આ આર્ગ્યુંમેંટ સાથેનાં ફંક્શનો તરીકે સંબોધાય છે.  
+
|-
01.40 Let us move on to our program
+
|01.30
 +
| This is called as functions without arguments.
  
ચાલો આપણા પ્રોગ્રામ પર જઈએ  
+
|-
01.43 I have already typed the program on the editor
+
|01.35
 +
| And This is called as functions with arguments.
  
મેં એડીટર પર પહેલાથી જ એક પ્રોગ્રામ ટાઈપ કર્યું છે  
+
|-
01.46 Let me open it
+
| 01.40
 +
| Let us move on to  our program
  
ચાલો હું તેને ખોલું.  
+
|-
01.50 Note that our filename is void function.c In this program we will calculate the sum of two numbers using function..
+
|01.43
 +
| I have already typed the program on the editor
  
આપણા ફાઈલનું નામ '''void function.c''' છે એની નોંધ લો, આ પ્રોગ્રામમાં આપણે ફંક્શનનાં મદદથી બે ક્રમાંકોનાં સરવાળાની ગણતરી કરીશું.  
+
|-
 
+
|01.46
02.03 Let me explain the code now.
+
| Let me open it
  
ચાલો હું '''કોડ''' સમજાવું.  
+
|-
02.06 This is our header file
+
|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.09 Before using any function it must be defined
 
  
કોઈપણ ફંક્શનને ઉપયોગ કરતાં પહેલા તે વ્યાખ્યાયિત થયેલ હોવી જ જોઈએ  
+
|-
02.14 Here we have declared a function called add
+
| 02.06
 +
| This is our '''header file'''
  
અહીં અમે '''add''' કહેવાતા એક ફંક્શનને જાહેર કર્યું છે  
+
|-
02.18 Note that add function is without any arguments
+
| 02.09
 +
| Before using any function it must be defined
  
નોંધ લો કે '''add ફંક્શન''' કોઈપણ આર્ગ્યુંમેંટ વિનાનું છે  
+
|-
02.22 And the return type is void
+
|02.14
 +
| Here we have declared a''' function''' called '''add'''
  
અને રીટર્ન પ્રકાર '''void''' છે  
+
|-
02.25 There are two types of functions
+
|02.18
 +
| Note that '''add function''' is without any '''arguments'''
  
'''ફંક્શનો''' બે પ્રકારનાં છે   
+
|-
02.27 First User-defined function that is our add function and
+
|02.22
 +
| And the return type is''' void'''
  
પ્રથમ છે વપરાશકર્તાએ વ્યાખ્યાયિત કરેલ ફંક્શન જે છે આપણું '''add ફંક્શન''' અને   
+
|-
02.33 Pr-defined function that is printf and main function
+
| 02.25
 +
| There are two types of functions
  
'''Pr-defined''' ફંક્શન જે કે '''printf''' અને મુખ્ય ફંક્શન છે  
+
|-
02.39 Here we have initialized a and b by assigning them values 2 and 3
+
|02.27
 +
| First User-defined  function that is our add function and  
  
અહીં આપણે '''a''' અને '''b''' ને '''2''' અને '''3''' વેલ્યુઓ અસાઈન કરીને પ્રારંભ કરી છે   
+
|-
02.47 Then we have declared a variable c
+
|02.33
 +
| Pr-defined function that is printf and main function
  
ત્યારબાદ આપણે '''a વેરીએબલ c''' જાહેર કર્યું છે   
+
|-
02.51 we add the values of a and b  
+
| 02.39
 +
|  Here we have initialized a and b by assigning them values  2 and 3
  
આપણે '''a''' અને '''b''' ની વેલ્યુઓને ઉમેરી છે   
+
|-
02.53 The result is stored in c
+
|  02.47
 +
|  Then  we have declared  a variable '''c'''
  
પરીણામ '''c''' માં સંગ્રહીત થયું છે       
+
|-
02.57 Then we print the result
+
|02.51
 +
|  we add the values of '''a''' and '''b'''
  
ત્યારબાદ આપણે પરીણામ પ્રીંટ કરીએ છીએ   
+
|-
03.00 This is our main function
+
|02.53
 +
| '''The result is stored in c'''
  
આ આપણું મુખ્ય ફંક્શન છે  
+
|-
03.03 Inside the main function, we call the add function
+
|  02.57
 +
|  Then we print the result
  
મુખ્ય ફંક્શનની અંદર, આપણે '''add''' ફંક્શનને બોલાવીએ છીએ     
+
|-
03.07 The addition operation will be performed and the result will be printed.
+
03.00
 +
| This is our main function
  
સરવાળાની ક્રીયા પૂરી થશે અને પરીણામ પ્રીંટ થશે.   
+
|-
03.13 Now click on Save
+
| 03.03
 +
|Inside the main function, we call the add function
  
હવે '''Save''' પર ક્લિક કરો  
+
|-
03.15 Let us execute the program
+
|03.07
 +
| The addition operation will be performed and the result will be printed.
  
ચાલો આપણે પ્રોગ્રામને એક્ઝેક્યુટ કરીએ   
+
|-
03.17 Please open the terminal window by pressing Ctrl, Alt and T keys simultaneously on your keyboard
+
| 03.13
 +
| Now click on Save
  
તમારા કીબોર્ડ પર '''Ctrl, Alt અને T''' કી એકસાથે દાબીને '''ટર્મીનલ વિન્ડો''' ને ખોલો   
+
|-
03.28 To compile type
+
|03.15
 +
| Let us execute the program
  
કમ્પાઈલ કરવાં માટે ટાઈપ કરો  
+
|-
03.29 'gcc void function.c -o void and press enter  
+
| 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
  
''''gcc void function.c -o void''' અને '''enter''' દબાવો  
 
03.40 To execute, type
 
./void
 
એક્ઝેક્યુટ કરવાં માટે, ટાઈપ કરો
 
 
'''./void'''
 
'''./void'''
 
 
03.45 The output is displayed asSum of a and b is 5
 
  
આઉટપુટ '''Sum of a and b is 5''' આ રીતે દેખાય છે   
+
|-
03.50 Now come back to our program  
+
| 03.45
 +
| The output is displayed as'''Sum 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 (int a, int b).;
 +
|-
 +
|04.34
 +
| Click on save
 +
 
 +
 
 +
|-
 +
|04.37
 +
| Here int a''' and '''int b''' are the '''arguments''' of the '''function add'''
 +
 
 +
|-
 +
| 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;
 +
 
 +
|-
 +
|  05.05
 +
|Press enter
 +
 
 +
|-
 +
|05.06
 +
|And type  '''sum = add(5,4);'''
 +
 
 +
|-
 +
|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
 +
| '''The 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
 +
| delete 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
  
હવે આપણા પ્રોગ્રામ પર પાછા આવીએ  
 
03.53 Functions contains special identifiers called as parameters or arguments
 
  
વિશેષ '''identifiers''' ધરાવનાર ફંક્શનો '''પેરામીટરો''' અથવા '''આર્ગ્યુંમેંટો''' તરીકે સંબોધાય છે  
 
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.
+
|09.10
 +
| Type 4 here. Click on '''Save'''
  
હું અહીં અમુક વસ્તુઓ બદલીશ. તમારા કીબોર્ડ પર '''shift Ctrl અને S''' કી એક સાથે દબાવો.  
+
|-
04.14 Now save the file as Function.c .Click on Save.
+
|09.15
 +
| Let us execute
  
હવે ફાઈલને '''Function.c''' તરીકે સંગ્રહીત કરો. '''Save''' પર ક્લિક કરો.   
+
|-
04.24 Replace the void key word with int and within the (int a, int b).;
+
|09.17
 +
| Come back to our terminal. Let me clear the prompt.
  
'''void''' કી શબ્દને '''int અને (int a, int b) દરમ્યાન''' સાથે બદલી કરો;   
+
|-
04.34 Click on save
+
|09.21
 +
| Let us compile as before, execute as before
  
'''save''' પર ક્લિક કરો  
+
|-
04.37 Here int a and int b are the arguments of the function add
+
|09.27
 +
| Yes! it is working
  
અહીં '''int a''' અને '''int b''' ફંક્શન '''add''' નાં આર્ગ્યુંમેંટો છે     
+
|-
04.44 Now delete this
+
| 09.29
 +
|now we will see another common error .Come back to our program
  
હવે આને રદ્દ કરો  
+
|-
04.47 No need to initialize a and b here. Now replace the void keyword again with the int keyword and click on save
+
|09.34
 +
here we  will  pass only 1 argument
  
અહીં '''a અને b''' ને પ્રારંભ કરવાની જરૂર નથી. હવે ફરીથી '''void''' કીશબ્દને '''int''' કીશબ્દ સાથે બદલી કરો અને '''save''' પર ક્લિક કરો   
+
|-
 
+
|09.39
04.58 Let us declare a variable sum here
+
|  delete 4
  
ચાલો એક વેરીએબલ સરવાળાને અહીં જાહેર કરીએ
+
|-
 
+
| 09.40
05.01 type int sum;
+
| Now  Click on '''Save''' .
  
'''int sum''' ટાઈપ કરો;  
+
|-
05.05 Press enter
+
| 09.43
 +
|  Let us see, what happens come back to our terminal.
  
'''enter''' દબાવો  
+
|-
05.06 And type sum = add(5,4);
+
| 09.47
 +
|  Let us compile as before
  
અને ટાઈપ કરો '''sum = add(5,4)''';  
 
05.19 Here we call the add function
 
અહીં આપણે '''add''' ફંક્શનને બોલાવીએ છીએ
 
 
 
05.22 Then we pass the arguments as 5 and 4
 
  
ત્યારબાદ આપણે આર્ગ્યુંમેંટોને '''5 અને 4''' તરીકે પસાર કરીએ છીએ  
 
05.26 5 will be stored in a and 4 will be stored in b
 
  
'''5''' '''a''' માં સંગ્રહીત થશે અને '''4''' '''b''' માં સંગ્રહીત થશે  
+
|-
05.31 The addition operation will be performed
+
| 09.49
 +
|We see error too few arguments to few functions  int  'add''''  
  
સરવાળાની ક્રીયા પૂરી થશે  
 
05.34 The returned value c will be stored in sum.
 
  
પાછી આવેલ વેલ્યુ '''c''' એ કુલ સરવાળામાં સંગ્રહીત થશે.  
+
|-
05.38 Now delete this add as we have already called the function above
+
| 09.54
 +
| Come back to our  program
  
હવે આ સરવાળાને રદ્દ કરો કારણ કે આપણે ઉપરનાં ફંક્શનને પહેલાથી જ બોલાવ્યું છે  
 
05.44 And Type
 
  
અને ટાઈપ કરો  
+
|-
05.45 return 0; Now click on save
+
|09.56
 +
| You can see here we have two argument '''int a''' and '''int b'''
  
'''return 0'''; હવે '''save''' પર ક્લિક કરો  
+
|-
05.51 A non-void function must use a return statement that returns a value.  
+
|10.03
 +
| And here we are passing only one argument.
  
'''non-void ફંક્શને''' એક રીટર્ન સ્ટેટમેંટને વાપરવું જોઈએ જે એક વેલ્યુ પાછી આપે છે.  
+
|-
05.58 Let us execute the program
+
|10.06
 +
| Hence it is giving an error
  
ચાલો પ્રોગ્રામને એક્ઝેક્યુટ કરીએ  
+
|-
06.00 Come back to a terminal
+
|10.09
 +
|  Let us fix the error
  
ટર્મીનલ પર પાછા આવીએ  
+
|-
06.03 Type gcc function.c -o fun and press enter
+
|10.10
 +
| Type 4 ,click on save
  
'''gcc function.c -o fun''' ટાઈપ કરો અને '''enter''' દબાવો
+
|-
 
+
|10.13
06.13 To execute  
+
| Let us execute again
./fun press enter
+
એક્ઝેક્યુટ કરવાં માટે
+
'''./fun''' એન્ટર દબાવો
+
 
+
06.19 the output is displayed as
+
  
આઉટપુટ આપેલ રીતે દેખાય છે  
+
|-
06.21 The Sum of a & b is 9
+
|10.16
 +
|Compile as before , execute as before.
  
'''The Sum of a & b is 9'''  
+
|-
06.25 NOW WE WILL EXECUTE THE SAME PROGRAM IN C++
+
| 10.21
 +
|  Yes it is working!Now come back to our slide
  
હવે આપણે સમાન પ્રોગ્રામ '''C++''' માં એક્ઝેક્યુટ કરીશું   
+
|-
06.29 Come back to our program. I will edit the same code again press Shift'Ctrl & S key simultaneously on your keyboard
+
|10.26
 +
| Let us summaries ,In this tutorial we learn't
  
આપણા પ્રોગ્રામ પર પાછા આવીએ. હું એજ કોડને ફરીથી એડીટ કરીશ '''Shift'Ctrl અને S''' કીને એકસાથે તમારા કીબોર્ડ પર દબાવો 
+
|-
 
+
|10.29
06.41 Now Save the file with an extension .cpp and click on save
+
| Functions
 +
|-
 +
| 10.31
 +
| Syntax of function
  
હવે ફાઈલને '''.cpp''' એક્સટેંશન સાથે સંગ્રહીત કરો અને '''save''' પર ક્લિક કરો  
+
|-
06.47 Let us change the header file as iostream
+
|10.33
 +
| Function without arguments: e.g ; void add()
  
ચાલો હેડર ફાઈલને '''iostream''' તરીકે બદલીએ  
+
|-
06.52 Now include the using statement. Click on save
+
|10.37
 +
| Function with arguments: e.g ;int add( int a,int b)
  
હવે '''using''' સ્ટેટમેંટનો સમાવેશ કરો. '''save''' પર ક્લિક કરો  
+
|-
07.00 The function declaration is same in C++
+
|10.43
 +
| As an assignment
  
ફંક્શન જાહેરાત '''C++''' માં સમાન છે  
+
|-
07.04 So there is no need to change anything here
+
|10.45
 +
| Write a program to calculate the square of a number using function.
  
તો અહીં કંઈપણ બદલવાની જરૂર નથી  
+
|-
07.07 Now replace the printf statement with the cout statement
+
| 10.50
 +
|  Watch the video available at  http://spoken-tutorial.org /What\_is\_a\_Spoken\_Tutorial
  
હવે '''printf''' સ્ટેટમેંટને '''cout''' સ્ટેટમેંટથી બદલી કરો
+
|-
 
+
|10.53
07.13 Delete the format specifier and \n
+
| It summarises the Spoken Tutorial project
  
'''ફોર્મેટ સ્પેસીફાયર''' અને '''\n''' ને રદ્દ કરો  
+
|-
07.16 delete the comma
+
|10.56
 +
| If you do not have good bandwidth, you can download and watch it
  
અલ્પ વિરામ રદ્દ કરો  
+
|-
07.17 Type two opening angle brackets. Delete the closing bracket here
+
| 11.01
 +
| The Spoken Tutorial Project Team
  
બે ખુલ્લા ખૂણાવાળા કૌંસ ટાઈપ કરો. અહીં બંધ કૌંસને રદ્દ કરો  
+
|-
07.23 Again type two opening angle brackets
+
|11.03
 +
| Conducts workshops using spoken tutorials
  
ફરીથી બે ખુલ્લા ખૂણાવાળા કૌંસ ટાઈપ કરો.  
+
|-
07.25 and within the double quotes type backslash n
+
|11.07
 +
| Gives certificates to those who pass an online test
  
અને બે અવતરણ ચિન્હમાં '''backslash n''' ટાઈપ કરો  
+
|-
07.29 We use the cout function to print the line in C++
+
|11.11
'''C++''' માં લાઈનને પ્રીંટ કરવાં માટે આપણે '''cout''' ફંક્શનને વાપરીએ છીએ
+
| For more details, please write to contact@spoken-tutorial.org
 
+
07.34 Now Click on save
+
  
હવે '''save''' પર ક્લિક કરો  
+
|-
07.37 Let us exeute the program
+
| 11.19
 +
| Spoken Tutorial Project is a part of the Talk to a Teacher project
  
 
+
|-
07.39 Come back to our terminal
+
|11.23
 +
| It is supported by the National Mission on Education through ICT, MHRD, Government of India
  
 
+
|-
07.42 To compile, type g++ function.cpp -o fun1
+
|11.30
 +
| More information on this Mission is available at: http://spoken-tutorial.org\NMEICT-Intro
  
 
+
|-
07.52 Here we have fun1, because we don't want to overwrite the output parameter fun for the file fun.c .  
+
| 11.35
 +
| This is Ashwini Patil from IIT Bombay
  
 
+
|-
08.02 Now press Enter
+
|11.39
 +
| Thank You for joining
  
 
+
|}
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
+

Revision as of 13:58, 9 July 2013

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 (int a, int b).;
04.34 Click on save


04.37 Here int a and int b are the arguments of the function add
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;
05.05 Press enter
05.06 And type sum = add(5,4);
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 The 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 delete 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

Gaurav, Jyotisolanki, Pratik kamble