Difference between revisions of "C-and-C++/C2/Functions/English"
Nancyvarkey (Talk | contribs) |
|||
Line 25: | Line 25: | ||
*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 | + | 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 102: | Line 102: | ||
| 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 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;"| 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 172: | Line 172: | ||
'''./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 200: | Line 200: | ||
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 300: | Line 300: | ||
'''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 399: | Line 399: | ||
'''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 443: | Line 443: | ||
|- | |- | ||
| 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 463: | Line 463: | ||
|- | |- | ||
| 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 473: | Line 473: | ||
'''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 551: | Line 551: | ||
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 582: | Line 582: | ||
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 |
|- | |- |
Revision as of 17:12, 29 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. 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.
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, 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. | |
Press Shift, Ctrl and S keys >> 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 here.
After sum , again type two opening angle brackets. Within double quotes, type 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
./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 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. Click on Save . Switch back 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. |