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

From Script | Spoken-Tutorial
Revision as of 15:50, 31 January 2014 by Ashwini (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

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