Difference between revisions of "C-and-C++/C2/Functions/English"
Nancyvarkey (Talk | contribs) |
|||
(2 intermediate revisions by 2 users not shown) | |||
Line 23: | Line 23: | ||
| 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, | | 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, | ||
− | What is a function | + | *What is a '''function''' |
− | Syntax of function | + | *Syntax of a '''function''' |
− | Significance of return statement | + | *Significance of '''return statement''' |
− | + | We will do this through examples. | |
We will also see some common errors and their solutions. | We will also see some common errors and their solutions. | ||
Line 40: | Line 40: | ||
| 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 record this tutorial, I am using | | 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 record this tutorial, I am using | ||
− | '''Ubuntu Operating system''' version 11.10 | + | *'''Ubuntu Operating system''' version 11.10 |
− | + | *'''gcc''' and '''g++ Compiler''' version 4.6.1 | |
− | '''gcc''' and | + | |
|- | |- | ||
Line 48: | Line 47: | ||
| 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 start with the introduction to '''functions''' | | 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 start with the introduction to '''functions''' | ||
− | A '''function''' is a self-contained program executing a specific task | + | A '''function''' is a self-contained program executing a specific task. |
− | Every program consists of one or more '''functions''' | + | Every program consists of one or more '''functions'''. |
− | Once executed the control will be returned back from where it was accessed | + | Once executed, the control will be returned back from where it was accessed. |
|- | |- | ||
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 5 | | style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 5 | ||
− | | 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 the syntax for function | + | | 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 the syntax for '''function'''. |
− | |||
− | + | '''ret-type''' defines the type of data that the '''function returns'''. | |
− | + | ||
− | + | ||
− | + | ||
− | '''ret-type''' defines the type of data that the '''function''' | + | |
'''fun_name''' defines the name of the '''function'''. | '''fun_name''' defines the name of the '''function'''. | ||
Line 74: | Line 68: | ||
| 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 can specify an '''empty parameter list'''. | | 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 can specify an '''empty parameter list'''. | ||
− | This is called as | + | This is called as '''functions without arguments'''. |
+ | |||
+ | And | ||
This is called as '''functions with arguments'''. | This is called as '''functions with arguments'''. | ||
Line 84: | Line 80: | ||
I have already typed the program on the editor. | I have already typed the program on the editor. | ||
− | So I will | + | So I will open it. |
|- | |- | ||
Line 90: | Line 86: | ||
'''function.c''' | '''function.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;"| | + | | 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 '''function'''. |
And I have saved the file with the extentsion '''.c''' | And I have saved the file with the extentsion '''.c''' | ||
Line 108: | Line 104: | ||
| 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;"| Before using any '''function''', it must be defined. | | 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;"| Before using any '''function''', it must be defined. | ||
− | Here we have | + | Here we have defined a''' function''' called '''add'''. |
Note that '''add function''' is without any '''arguments'''. | Note that '''add function''' is without any '''arguments'''. | ||
Line 118: | Line 114: | ||
| 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;"| There are two types of functions- | | 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;"| There are two types of functions- | ||
− | *'''User-defined''' that is our '''add function''' | + | *'''User-defined''' that is our '''add function''' and |
*'''Pre-defined''' that is '''printf''' and '''main function''' | *'''Pre-defined''' that is '''printf''' and '''main function''' | ||
Line 164: | Line 160: | ||
| 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 click on '''Save'''. | | 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 click on '''Save'''. | ||
− | + | Let us execute the program. | |
|- | |- | ||
Line 178: | Line 174: | ||
'''./fun''' | '''./fun''' | ||
− | | 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 | + | | 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 function.c -o fun''' | '''gcc function.c -o fun''' | ||
Line 206: | Line 202: | ||
int add(int a, int b) | int add(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;"| I will | + | | 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;"| I will change a few things here. |
Type | Type | ||
Line 306: | Line 302: | ||
'''Output''' | '''Output''' | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| | + | | style="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 |
'''Sum is 9''' | '''Sum is 9''' | ||
Line 319: | Line 315: | ||
|- | |- | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Press '''Shift, Ctrl''' and '''S''' keys >> type | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|First Press '''Shift, Ctrl''' and '''S''' keys simultaneously>> type |
'''function.cpp''' in Name field >> Click on "Save" | '''function.cpp''' in Name field >> Click on "Save" | ||
Line 364: | Line 360: | ||
'''<nowiki><<sum <<”\n”</nowiki>''' | '''<nowiki><<sum <<”\n”</nowiki>''' | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now, type two opening '''angle brackets''' | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now, type two opening '''angle brackets''' |
After '''sum ''', again type two opening '''angle brackets'''. | After '''sum ''', again type two opening '''angle brackets'''. | ||
Line 372: | Line 368: | ||
Delete this closing bracket. | Delete this closing bracket. | ||
− | Click on''' Save'''. | + | Now Click on''' Save'''. |
|- | |- | ||
Line 405: | Line 401: | ||
'''Output''' | '''Output''' | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| | + | | style="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: |
'''Sum is 9''' | '''Sum is 9''' | ||
Line 449: | Line 445: | ||
|- | |- | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| On the editor | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| On the editor | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now come back to our program. |
Let us fix the error. | Let us fix the error. | ||
Line 469: | Line 465: | ||
|- | |- | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Compile the program as before. |
Yes! it is working. | Yes! it is working. | ||
Line 479: | Line 475: | ||
'''4''' | '''4''' | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now we will see another common error which we can come across. |
Suppose here we pass only one parameter. | Suppose here we pass only one parameter. | ||
Line 503: | Line 499: | ||
|- | |- | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| On the editor | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| On the editor | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Switch back to | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Switch back to our program. |
|- | |- | ||
Line 523: | Line 519: | ||
Click on '''Save''' . | Click on '''Save''' . | ||
− | Switch | + | Switch to the '''terminal'''. |
|- | |- | ||
Line 557: | Line 553: | ||
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 588: | Line 584: | ||
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 | + | More information on this Mission is available at the link shown below http://spoken-tutorial.org\NMEICT-Intro |
|- | |- |
Latest revision as of 11:55, 30 January 2014
Title of script: Functions in C and C++
Author: Ashwini R. Patil
Keywords: Functions, return statement, Video Tutorial
|
|
Slide 1 | Welcome to the spoken tutorial on Functions in C and C++ |
Slide 2
|
In this tutorial we will learn,
We will do this through examples. We will also see some common errors and their solutions. |
Slide 3
|
To record this tutorial, I am using
|
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 executed, the control will be returned back from where it was accessed. |
Slide 5 | Let us see the syntax for function.
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. And 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 open it. | |
Point the cursor on the filename
function.c |
Note that our filename is function.
And I have saved the file with the extentsion .c Let me explain the code. |
Highlight
#include <stdio.h> |
This is our header file. |
Highlight
void add() |
Before using any function, it must be defined.
Here we have defined a function called add. Note that add function is without any arguments. And the return type is void. |
There are two types of functions-
| |
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.
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, 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 change a few things here.
Type int add(int a, int b) Here we have declared a function 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. |
Delete printf statement.
Type int main() |
Delete the printf statement.
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. | |
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.
Let us execute. |
Highlight
Output |
The output is displayed as
Sum 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. | |
First Press Shift, Ctrl and S keys simultaneously>> type
function.cpp in Name field >> Click on "Save" |
First press Shift, Ctrl and S keys simultaneously.
Now save the file with .cpp extension. Click on Save. |
Type
<iostream> |
First we will change the header file as
<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.
Delete the comma. |
Type
<<sum <<”\n” |
Now, type two opening angle brackets
After sum , again type two opening angle brackets. Within double quotes, type backslash n. Delete this closing bracket. Now Click on Save. |
On the terminal | Let us compile the program.
Come back to the terminal. |
Type
g++ function.cpp -o fun1
./fun1 |
Type
g++ function.cpp -o fun1 Here we have fun1, this is because we don't want to overwrite the output file fun. Press Enter. Type ./fun1 |
Highlight
Output |
The output is displayed as:
Sum is 9 |
Errors
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 | Click on Save. |
On the terminal | Let us compile the program. |
Highlight
|
We see an error at line no. 10.
x was not declared in this scope. This is because x is a character variable. It was not declared anywhere. 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 come back to our program.
Let us fix the error. |
Type 4 | Type 4 at line no. 10. |
Click on Save | Click on Save.
Let us execute again. |
On the terminal | Let me clear the prompt. |
Compile the program as before.
Yes! it is working. | |
Error 2
Delete 4 |
Now we will see another common error which we can come across.
Suppose here we pass only one parameter. Delete 4. |
Click on Save | Click on Save |
On the terminal | Switch to the terminal. Let us compile. |
Highlight
Error |
We see an error at line no 10.
too few arguments to function 'int add (int, int)' |
On the editor | Switch back to our 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. Click on Save . Switch to the terminal. |
On the terminal | Let us execute again. |
Yes it is working!
Come back to our slides. | |
Slide 7 | To summarise, in this tutorial we have learnt -
|
Slide 10
Assignment |
As an assignment-
Write a program to calculate the square of a number. |
Slide 11
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 12
Spoken Tutorial Workshops |
The Spoken Tutorial Project Team
For more details, please write to contact@spoken-tutorial.org |
Slide 13
|
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. |