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

From Script | Spoken-Tutorial
Jump to: navigation, search
 
Line 13: Line 13:
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 1
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 1
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Welcome to the spoken tutorial on '''Function call in C and C++'''
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Welcome to the spoken tutorial on '''Function calls in C and C++'''
  
 
|-
 
|-
Line 20: Line 20:
  
  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| In this tutorial we will learn about type of  function calls namely ,
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| In this tutorial we will learn about type of  function calls namely,
  
 
call by value.
 
call by value.
Line 59: Line 59:
 
I have already typed the program on the editor.
 
I have already typed the program on the editor.
  
So i will just open it.
+
I will just open it.
  
 
|-
 
|-
Line 92: Line 92:
 
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here we have function''' cube '''having''' '''an''' argument '''as''' int x.'''
 
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here we have function''' cube '''having''' '''an''' argument '''as''' int x.'''
  
In this 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'''.''' '''
  
 
|-
 
|-
Line 104: Line 104:
  
 
'''int n=8;'''
 
'''int n=8;'''
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here we give the value of '''n''' as '''8.''' '''n''' is an integer variable
+
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here we give the value of '''n''' as '''8.''' '''n''' is an integer variable.
  
 
|-
 
|-
Line 112: Line 112:
 
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Then we call the function''' cube.'''
 
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Then we call the function''' cube.'''
  
And print the valu of''' n '''and the cube of''' n.'''
+
And print the value of''' n '''and the cube of''' n.'''
  
 
|-
 
|-
Line 118: Line 118:
  
 
'''return 0;'''
 
'''return 0;'''
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| This is our return statement.
+
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| And this is our return statement.
  
 
|-
 
|-
Line 128: Line 128:
 
|-
 
|-
 
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Press Ctrl, Alt and T keys simultaneously
 
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Press Ctrl, Alt and T keys simultaneously
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"|  open the terminal window by pressing Ctrl, Alt and T keys simultaneously on  your keyboard.
+
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"|  open the terminal window by pressing '''Ctrl, Alt and T''' keys simultaneously on  your keyboard.
  
 
|-
 
|-
Line 138: Line 138:
  
 
'''./val'''
 
'''./val'''
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| To compile   type
+
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| To compile type
  
'''gcc callbyval.c -o val'''
+
'''gcc space callbyval.c space -o space val'''
 +
'''Press Enter'''
  
To execute, type
+
Now type
  
 
'''./val'''
 
'''./val'''
 +
'''Press Enter'''
  
 
|-
 
|-
Line 150: Line 152:
  
 
Output
 
Output
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| We see the output is displayed as
+
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| The output is displayed as
  
 
'''Cube of 8 is 512.'''
 
'''Cube of 8 is 512.'''
Line 173: Line 175:
 
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| On the editor
 
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| On the editor
 
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us see an example on function call by reference.
 
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us see an example on function call by reference.
 
I have already typed the code on the editor.
 
 
Lets take a look.
 
  
 
|-
 
|-
Line 183: Line 181:
 
'''callbyref.c'''
 
'''callbyref.c'''
 
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Note that our filename is '''callbyref.c'''
 
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Note that our filename is '''callbyref.c'''
 
Let me explain the code now.
 
  
 
|-
 
|-
Line 197: Line 193:
 
'''int swap(int *a, int *b)'''
 
'''int swap(int *a, int *b)'''
 
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Then we have function '''swap'''
 
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Then we have function '''swap'''
 +
This function will exchange the values of the variables.
 +
Value of a will be stored in value of b and vice versa.
  
 
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.'''
Line 217: Line 215:
  
 
Then value of '''t''' is stored in '''<nowiki>*b</nowiki>'''.
 
Then value of '''t''' is stored in '''<nowiki>*b</nowiki>'''.
 +
Like this the values are exchanged.
  
 
|-
 
|-
Line 250: Line 249:
  
 
'''swap(&i,&j);'''
 
'''swap(&i,&j);'''
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here we call the function '''swap'''
+
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Then we call the function '''swap'''
  
 
|-
 
|-
Line 256: Line 255:
  
 
'''printf("After swapping %d and %d\n",i,j);'''
 
'''printf("After swapping %d and %d\n",i,j);'''
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now we print the values after swapping.
+
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| And then we print the values after swapping.
  
 
|-
 
|-
Line 262: Line 261:
  
 
'''return 0;'''
 
'''return 0;'''
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| This is our return statement.
+
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| And this is our return statement.
 
+
|-
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Click on '''Save'''
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Click on''' Save'''
+
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| On the terminal
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| On the terminal
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us execute the program
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"|Now let us execute the program
  
Come back to our terminal
+
Switch back to our terminal
  
 
|-
 
|-
Line 282: Line 277:
  
 
'''./ref'''
 
'''./ref'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now compile the program as before,  
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| To compile type,  
  
'''gcc callbyref.c -o ref'''
+
'''gcc space callbyref.c space -o space ref'''
 +
'''Press Enter'''
  
let us execute
+
Now type
  
 
'''./ref'''
 
'''./ref'''
 +
'''Press Enter'''
  
 
|-
 
|-
Line 294: Line 291:
  
 
'''Output'''
 
'''Output'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| We see it is output is displayed as:
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| We see:
  
 
Enter the values
 
Enter the values
  
 
I will enter as '''6''' and '''4'''
 
I will enter as '''6''' and '''4'''
 +
 +
The output is displayed as:
  
 
Before swapping''' 6 '''and''' 4'''
 
Before swapping''' 6 '''and''' 4'''
Line 314: Line 313:
  
 
'''callbyref.cpp'''
 
'''callbyref.cpp'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Note that our filename is '''callbyref.cpp'''
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"|This is the second program function callbyreference
 +
 
 +
Note that our filename is '''callbyref.cpp'''
  
Let me explain the code now.
+
Let us go through the code.
  
 
|-
 
|-
Line 322: Line 323:
  
 
'''<nowiki><iostream></nowiki>'''
 
'''<nowiki><iostream></nowiki>'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| First we will change the header file  
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| This is our header file as
  
 
'''<nowiki><iostream></nowiki>'''
 
'''<nowiki><iostream></nowiki>'''
Line 330: Line 331:
  
 
'''using namespace std;'''
 
'''using namespace std;'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| We have used the '''std namespace'''
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here we are using the '''std namespace'''
  
 
|-
 
|-
Line 362: Line 363:
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Rest of the code is similar to our C code.
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Rest of the code is similar to our C code.
  
The '''printf''' and '''scanf''' statement is replaced by '''cout''' and '''cin '''statement.
+
The '''printf''' statement is replaced by '''cout''' and the '''scanf''' statement is replaced by '''cin '''
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| On the terminal
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| On the terminal
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us compile the program
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now let us execute the program
  
Come back to the terminal
+
Come back to our terminal
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Type
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Type
  
'''g++callbyref.cpp -o ref1'''
+
'''g++ callbyref.cpp -o ref1'''
  
  
Line 379: Line 380:
  
 
'''./ref1'''
 
'''./ref1'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Type
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"|To compile type
  
'''g++callbyref.cpp -o ref1'''
+
'''g++ space callbyref.cpp space -o space ref1'''
  
  
 
press''' Enter'''
 
press''' Enter'''
  
To execute
+
Now type
 
+
Type
+
  
 
'''./ref1'''
 
'''./ref1'''
 +
press''' Enter'''
  
 
|-
 
|-
Line 400: Line 400:
 
'''Enter values of a and b'''
 
'''Enter values of a and b'''
  
I will enter
+
I will enter as
  
 
'''4'''
 
'''4'''
Line 423: Line 423:
  
 
Function call by value.
 
Function call by value.
 +
 +
And
  
 
Function call by reference.
 
Function call by reference.
Line 440: Line 442:
  
 
About the Spoken Tutorial Project
 
About the Spoken Tutorial Project
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Watch the video available at  
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Watch the video available at the link shown below.
  
 
http://spoken-tutorial.org /What\_is\_a\_Spoken\_Tutorial
 
http://spoken-tutorial.org /What\_is\_a\_Spoken\_Tutorial
Line 471: Line 473:
 
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
  
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: http://spoken-tutorial.org\NMEICT-Intro
  
 
|-
 
|-

Latest revision as of 15:50, 31 January 2014

Title of script: Functions in C and C++

Author: Ashwini R. Patil

Keywords: Functions, call by value, call by reference, Video Tutorial


Visual Cue
Narration
Slide 1 Welcome to the spoken tutorial on Function calls in C and C++
Slide 2


In this tutorial we will learn about type of function calls namely,

call by value.

call by reference.

We will do this through an example.

Slide 3


To record this tutorial, I am using

Ubuntu Operating system version 11.10

gcc and g++ Compiler version 4.6.1

Slide 4 Let us start with the introduction to functions call by value

It is a method of passing arguments to the function.

When we pass a variable by value it makes a copy of the variable.

Before passing to the function.

Changes made to the arguments inside the function will remain in the function.

It will not be affected outside the function.

Let us see a program on function call by value.

I have already typed the program on the editor.

I will just open it.

Point the cursor to

callbyval.c

Please note that our filename is callbyval.c


In this program we will calculate the cube of a number.

Let me explain the code now.
 
Highlight

#include <stdio.h>

This is our header file
Highlight

int cube(int x)

{

x=x*x*x;

return(x);

}

Here we have function cube having an argument as int x.

In this function we calculate the cube of x and return the value of x.

Highlight

int main()

This is our main function.
Highlight

int n=8;

Here we give the value of n as 8. n is an integer variable.
Highlight

printf("Cube of %d is %d\n",n,cube(n));

Then we call the function cube.

And print the value of n and the cube of n.

Highlight

return 0;

And this is our return statement.

Now let us execute the program.

Press Ctrl, Alt and T keys simultaneously open the terminal window by pressing Ctrl, Alt and T keys simultaneously on your keyboard.
Type

gcc callbyval.c -o val

Type

./val

To compile type

gcc space callbyval.c space -o space val Press Enter

Now type

./val Press Enter

Highlight

Output

The output is displayed as

Cube of 8 is 512.

Now we will see function call by reference.

Let us go back to our slides.

Slide 5 It is another method of passing arguments to the function.

This method copies the address of the argument instead of the value.

Changes made to the arguments inside a function can affect them outside.

In this we need to declare the arguments as pointer type.

On the editor Let us see an example on function call by reference.
Point to the filename

callbyref.c

Note that our filename is callbyref.c
Highlight

#include<stdio.h>

This is our headerfile as stdio.h
Highlight

int swap(int *a, int *b)

Then we have function swap

This function will exchange the values of the variables. Value of a will be stored in value of b and vice versa.

You can see that the arguments passed in the function are pointer type.

Highlight

int t;

t=*a;

*a=*b;

*b=t;

Here we have declared an integer variable t.

First value of *a is stored in t.

Then value of *b is stored in *a.

Then value of t is stored in *b. Like this the values are exchanged.

Highlight

int main()

This is our main function.
Highlight

int i, j;

Here we have declared two integer variables as i and j.
Highlight

printf("Enter the values ");

scanf("%d%d",&i,&j);

Then we take the values of i and j as user inputs.

&i and &j will give the memory address of i and j.

Highlight

printf("Before swapping %d and %d\n",i,j);

First we print the values before swapping.
Highlight

swap(&i,&j);

Then we call the function swap
Highlight

printf("After swapping %d and %d\n",i,j);

And then we print the values after swapping.
Highlight

return 0;

And this is our return statement.
On the terminal Now let us execute the program

Switch back to our terminal

Type

gcc callbyref.c -o ref

Type

./ref

To compile type,

gcc space callbyref.c space -o space ref Press Enter

Now type

./ref Press Enter

Highlight

Output

We see:

Enter the values

I will enter as 6 and 4

The output is displayed as:

Before swapping 6 and 4

After swapping 4 and 6

NOW LET US SEE HOW TO EXECUTE THE SAME PROGRAM IN C++

I have the code, lets go through it.

Point cursor

callbyref.cpp

This is the second program function callbyreference

Note that our filename is callbyref.cpp

Let us go through the code.

Type

<iostream>

This is our header file as

<iostream>

Type

using namespace std;

Here we are using the std namespace
Highlight

int swap(int &x, int &y)

{

int temp;

temp = x;

x = y;

y = temp;

}

The function declaration is same in C++.

In this we pass the arguments as &x and &y.

This will give the memory address of x and y.

Here we swap the values.

Highlight

The code

Rest of the code is similar to our C code.

The printf statement is replaced by cout and the scanf statement is replaced by cin

On the terminal Now let us execute the program

Come back to our terminal

Type

g++ callbyref.cpp -o ref1


Type

./ref1

To compile type

g++ space callbyref.cpp space -o space ref1


press Enter

Now type

./ref1 press Enter

Highlight

Output

Here it is displayed as:

Enter values of a and b

I will enter as

4

3

The output is displayed as

Before swapping a and b: 4 and 3

After swapping a and b: 3 and 4

This brings us to the end of this tutorial.

Let us go back to our slides.

Slide 6 Let us summarize, In this tutorial we learn't:

Function call by value.

And

Function call by reference.

Slide 7

Assignment

As an assignment

Write a similar program to calculate the cube of a number.

Using call by value in C++.

Slide 8

About the Spoken Tutorial Project

Watch the video available at the link shown below.

http://spoken-tutorial.org /What\_is\_a\_Spoken\_Tutorial

It summarises the Spoken Tutorial project

If you do not have good bandwidth, you can download and watch it

Slide 9

Spoken Tutorial Workshops

The Spoken Tutorial Project Team

Conducts workshops using spoken tutorials

Gives certificates to those who pass an online test

For more details, please write to

contact@spoken-tutorial.org

Slide 10


Acknowledgement

Spoken Tutorial Project is a part of the Talk to a Teacher project

It is supported by the National Mission on Education through ICT, MHRD, Government of India

More information on this Mission is available at the link shown below: http://spoken-tutorial.org\NMEICT-Intro

This is Ashwini Patil from IIT Bombay

Thank You for joining

Contributors and Content Editors

Ashwini, Sneha