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

From Script | Spoken-Tutorial
Revision as of 10:22, 7 May 2013 by Sneha (Talk | contribs)

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 call 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.

So 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 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 valu of n and the cube of n.

Highlight

return 0;

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 callbyval.c -o val

To execute, type

./val

Highlight

Output

We see 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.

I have already typed the code on the editor.

Lets take a look.

Point to the filename

callbyref.c

Note that our filename is callbyref.c

Let me explain the code now.

Highlight

#include<stdio.h>

This is our headerfile as stdio.h
Highlight

int swap(int *a, int *b)

Then we have function swap

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.

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);

Here we call the function swap
Highlight

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

Now we print the values after swapping.
Highlight

return 0;

This is our return statement.
Click on Save Click on Save
On the terminal Let us execute the program

Come back to our terminal

Type

gcc callbyref.c -o ref

Type

./ref

Now compile the program as before,

gcc callbyref.c -o ref

let us execute

./ref

Highlight

Output

We see it is output is displayed as:

Enter the values

I will enter as 6 and 4

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

Note that our filename is callbyref.cpp

Let me explain the code now.

Type

<iostream>

First we will change the header file

<iostream>

Type

using namespace std;

We have used 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 and scanf statement is replaced by cout and cin statement.

On the terminal Let us compile the program

Come back to the terminal

Type

g++callbyref.cpp -o ref1


Type

./ref1

Type

g++callbyref.cpp -o ref1


press Enter

To execute

Type

./ref1

Highlight

Output

Here it is displayed as:

Enter values of a and b

I will enter

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.

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

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: http://spoken-tutorial.org\NMEICT-Intro

This is Ashwini Patil from IIT Bombay

Thank You for joining

Contributors and Content Editors

Ashwini, Sneha