article

Introducing C++ - Functions

Email
Submitted on: 1/7/2015 7:16:00 PM
By: Karthik A (from psc cd)  
Level: Beginner
User Rating: By 3 Users
Compatibility: C, C++ (general)
Views: 900
 
     This tutorial intends to teach beginners how to use functions in programs along with the various terms used like 'calling' a function and 'passing' values to a function and so on.

 
				

Introducing C++ - Part 3

 

Functions

 

        We will now see the usage of functions is C++. Functions are very useful if you desire to use a particular set of statements or a particular operation again and again. With the help of a simple example i will explain the concept of functions.

The general form of a function is

     return-type function-name (parameter 1, parameter 2)

     {

 

     }

        return-type     -> Its the value that the function will return after various calculations.

        function-name -> This is the name of the function that we like to use

        parameter 1    ->  This is also called as the argument. These are the variables that are being passed on to the function

        parameter 2    ->  Another variable that is passed to the function

        I will illustrate the use of functions with the help of the following example

#include<iostream.h>

int add(int x, int y)

{

int z;

z=x+y;

return z;

}

void main()

{

int a=3,b=5,c;

c=add(a,b);

cout<<"The sum is "<<c;

}

 

Let us see how the code works

int add(int x, int y)

{

int z;

z=x+y;

return z;

}

        In the above code segment 'add' is the function name and 'int' is the return-type.

       There are two parameters 'int x' and 'int y'.

       The variable int z is used to store the sum of the two numbers.

        As mentioned before,

              return z;

         the integer z is returned back.

 

            Ok but where does the terms 'calling' and 'returning' the values arises? We will see that...

        In the main section you will be able to the lines

int a=3,b=5,c;

        Here we are declaring (and initializing) a and b and declaring a variable c.

        Consider the following line:

c=add(a,b);

        At this point the function gets 'CALLED'. The function 'add' is called with two parameters 'a' and 'b'. It is said that the integers a and b are passed to the function 'add'.

        When the function is called the program control is transferred to the 'add' function. The two parameters a and b are added and 'RETURNED' through the variable 'z'. This variable is stored in 'c' and then printed.

        This is how functions work in C++ and any other language. This is just one way of using functions and you will know more as you proceed.

RATE THE TUTORIAL IF YOU LIKE IT and GIVE YOUR COMMENTS TOO.

THANK YOU!


Other 1 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Beginner category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.