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

From Script | Spoken-Tutorial
Jump to: navigation, search
(Created page with ''''Title of script:''' Functions in C and C++ '''Author: '''Ashwini R. Patil '''Keywords: Functions, return statement, Video Tutorial ''' {| style="border-spacing:0;" | styl…')
 
Line 27: Line 27:
 
Syntax of function
 
Syntax of function
  
Significance of a return statement
+
Significance of return statement
  
We will do this through an example
+
Few examples on functions
  
 
We will also see some common errors and their solutions.
 
We will also see some common errors and their solutions.

Revision as of 12:54, 3 September 2013

Title of script: Functions in C and C++

Author: Ashwini R. Patil

Keywords: Functions, return statement, Video Tutorial


Visual Cue
Narration


Slide 1 Welcome to the spoken tutorial on Functions in C and C++
Slide 2


In this tutorial we will learn,

What is a function

Syntax of function

Significance of return statement

Few examples on functions

We will also see some common errors and their solutions.

Slide 3


To record this tutorial, I am using

Ubuntu Operating system version 11.10

gcc and g++ Compiler version 4.6.1 on Ubuntu

Slide 4 Let us start with the introduction to functions

A function is a self-contained program executing a specific task

Every program consists of one or more functions

Once it is executed the control will be returned back from where it was accessed

Slide 5 Let us see the syntax for function

ret_type fun_name(parameters)

Or

ret_type fun_name( )

ret-type defines the type of data that the function returns

fun_name defines the name of the function

parameters is the list of variable names and their types

Slide 6 We can specify an empty parameter list

This is called as functions without arguments.

This is called as functions with arguments.

Let us see a program using void

I have already typed the program on the editor

So i will just open it

Point the cursor on the filename

function.c

Please note that our filename is function

and I have saved the file with the extentsion .c

Let me explain the code now

Highlight

#include <stdio.h>

This is our header file
Highlight

void add()

Before using any function it must be defined

Here we have declared a function called add

Note that add function is without any arguments

And the return type is void

There are two types of functions

User-defined that is our add function

Pre-defined that is printf and main function

Highlight

int a = 2;

int b = 3;

Here we have initialized a and b by assigning them values as 2 and 3
Highlight

int c = a + b;

Here we have declared a variable c

Then we add the values of a and b

The result is stored in c

Highlight

printf("Value of C is %d\n",c);

Then we print the result
Highlight

void main()

This is our main function
Highlight

add();

Here we call the add function

The addition operation will be performed and the result will be printed.

Now click on Save

Now let us execute the program

Press Ctrl, Alt and T keys simultaneously Please open the terminal window by pressing Ctrl, Alt and T keys simultaneously
Type

gcc function.c -o fun

Type

./fun

To compile the program, type

gcc function.c -o fun

To execute, type

./fun

Highlight

Output

We see the output is displayed as

Sum of a and b is 5

On the editor Now come back to our program

Functions contains special identifiers called as parameters or arguments

Let us see the same example with arguments

Type

int add(int a, int b)

I will just change a few things here

Type

int add(int a, int b)

Here we have declared a function called add

int a and int b are the arguments of the function add

Delete

int a=2;

int b=3;

Let us delete this

No need to initialize a and b here

Type

int main()

Then type

int main()

Type

int sum;

Let us declare a variable sum here

type

int sum;

Highlight

sum = add(5,4);

Then type

sum = add(5,4);

Here we call the add function

Then we pass the parameters as 5 and 4

5 will be stored in a and 4 will be stored in b

The addition operation will be performed

The returned value c will be stored in sum

Let us now print the result.

Hence type here

printf(“Sum is %d\n”,sum);

Delete this as we have already called the function above.
Type

return 0;

A non-void function must use a return statement that returns a value.

Click on Save Click on Save
On the terminal Let us execute the program

Come back to our terminal

Type

gcc function.c -o fun

Type

./fun

Now compile the program as before,

gcc function.c -o fun

let us execute

./fun

Highlight

Output

We see the output is displayed as

The result is 9

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

Come back to our program

Let me change a few things here

Go to File menu

Click on save as option

Type

cpp

Now click on File menu

Click on Save as option

Save the file with .cpp extension

Type

<iostream>

First we will change the header file

<iostream>

Type

using namespace std;

We will include the using statement here
The function declaration is same in C++

So there is no need to change anything here

Type

cout<<

Now replace the printf statement with cout statement

as we use cout<< function to print a line in C++

 %d We don't need the format specifier and \n here

So delete it

Type

<<sum <<”\n”

Now type two opening angle brackets here

Then type sum two opening angle brackets again

and within the double quotes backslash n

Delete this closing bracket

Click on save

On the terminal Let us compile the program

Come back to the terminal

Type

g++ function.cpp -o fun1


Type

./fun1

Type

g++ function.cpp -o fun1

Here we have fun1, this is because we don't want to overwrite the output file.

press Enter

To execute

Type

./fun1

Highlight

Output

Here the output is displayed as:

The result is 9.

Errors


Type

x


Now we will see the common errors which we can come across.

Suppose here we type x in the place of 4.

I will retain the rest of the code as it is.

Click on save Save the program
On the terminal Let us go to the terminal

and compile the program

Highlight


error

We see an error at line no. 11 in our function.cpp file

x undeclared

This is because x is a character variable

And our add function has integer variable as an argument

So there is a mismatch in return type and return value.

On the editor Now let's come back to our program

Let us fix the error

Type

4

Type 4 here at line no. 11
Click on Save Click on Save

Let us execute this program

On the terminal Come back to our terminal
Let us compile and execute as before

Yes! it is working

Error 2

Delete

4

Let us see another common error which we can come across.

Suppose here we pass only 1 parameter

Let us delete 4

Click on Save Click on Save
On the terminal Let us compile it

Switch to the terminal

Highlight

Error

We see an error at line no 11 in our function.cpp file

too few arguments to function 'add'

On the editor Let us switch back to the program
You can see here we have two parameters

int a and int b

And here we are passing only one parameter

Hence it is giving an error

Click on Save Let us fix the error

Type 4 here

Click on Save

Switch back to the terminal

On the terminal Let us compile the program
Yes it is working!

Now let go through a quick summary

Come back to our slides

Slide 7 Syntax of function

Function without arguments

Function with arguements

Significance of a return statement

Slide 10

Assignment

As an assignment

Write a program to calculate the square of a number using function.

Slide 11

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 12

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 13


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, Nancyvarkey, Priyacst