All about C plus plus algorithms!

Monday, 9 May 2016

What is a function in C++? | For Beginners



What is a Function?


A function in C++ provides the basic reuse of code. 
For example, if you want to get the size of a character array. You may require this code:

char* arr = "Hello!";
int size = 0;
for (int i = 0; arr[i] != NULL; i++)
{
          size++;
}
size = size+1;      //The size of a char array also has a null at its end hence increasing size by one


Let you have to use this code again and again for different char arrays in your code. Isn't it tiresome? Re-writing all the code again? If you're smart, you may copy-paste the code. ;) But it makes your code long. Making a function allows us to reuse this code by calling in one line hence we only need to make it once and then reuse it. Smarter way! ;)

How to make a function?

Functions are made on top of the main, outside it. 
A function contains a header or a prototype. You cannot make more than one functions of the same prototype.
What is a prototype?

<Return type> Function_Name (parameters)

Return type : It is what a function returns. In case of the above given example, it returns the size which is an integer type hence the return type would be int.

Function Name : You can give any name to your function. I would give call it Char_Arr_Size. You can even name it End_of_World, it won't give any error :P However, try to give names that makes sense of what they are doing, for your own ease.  ;) 

Parameters : Parameters are what you pass to the function. Like the object on which you want to perform a function or things that are needed for the function. For this example, we need to pass the Char* array whose size is to be calculated.
PS : Remember to mention the data type of the parameters passed. For more than one parameters, separate them with commas (,) e.g. 
(int a, char b, double c)

Our function will look like this :

int Char_Arr_Size (char* arr)
{
        //define the function inside these brackets.

         int size = 0;
       for (int i = 0; arr[i] != NULL; i++)
       {
             size++;
       }
       size = size+1;  

       return size;   //This returns the size of the passed arr
}

How to call the function?

void main ()
{
        char* arr = "Hello!";
        int size;      //make a variable to store what the function returns
       size = Char_Arr_Size(arr);    //catching the returned size in this variable :P
      
     //Remember : You don't need to pass the data type of the parameter while calling.. just the name of it as you can see above.


}


PLUS :
      The return type can be of many types e.g. int, char even void. There are some functions that don't return anything like a function in which you are just printing something and you don't need to return anything. You can simply make it void in the return type and don't return anything.
      There are other ways to return the objects and pass as well i.e. by address. This one is copy method. I'll explain passing & returning by copy in my next post. 


If you have any questions, feel frank to ask. 

Thank you!:)
Arigatou Gozaimasu!! ^^


Share:

0 comments:

Post a Comment

Popular Posts

Blog Archive

Copyright by progrithms.blogspot.com. All Rights Reserved.. Powered by Blogger.

Copyright © Programs and Algorithms | Powered by Blogger

Design by ThemePacific | Blogger Theme by NewBloggerThemes.com