Coding Beyond Technology..

Monday 19 September 2016

Functions

Functions are small block of codes or segments or subroutine that are used to perform some operation.

For example :

printf() - to print message or values onto the console.
scanf() - to take input from the console.
getch() - to take a character from the console.

and  many more..

Functions always have opening and closing brackets  " ( ) ".

Syntax :

  return_type function_name ( paraments )
{
       //body
}

In C and C++, there is a term called Function Prototype . A Function Prototype , can also be called Function Signature that contain return_type , function_name and the types & number of parameter it has, omitting the function's body. While defining function prototype it must be end with a semi-colon ';' .

Function prototyping also called function defining.

Syntax :
return_type function_name ( parameter_1 , parameter_2, ... ) ;

For Eg:
int add( int x , int y ); or int add( int , int ) ;

The above example tells the compiler that there exits a function( add() ) with integer( int ) return type and has two parameters both of integer(int) type , whose body has been declared some where in the program.

The main purpose of the Function Prototype is to make sure the compiler or other functions that function is exits in the program.

Suppose , there is function add() and you call it in the main() ,

//code in turbo C

case I :

#include<iostream.h>
#include<conio.h> 

int add( int x , int y ){
      int z = x + y ;
      return z ;
}

void main(){
     clrscr();
     int a = add(10,20) ;
     cout<<a;
     getch();
}

output : 30

case II :

#include<iostream.h>
#include<conio.h>

void main(){
     clrscr();
     int a = add(10,20) ;
     cout<<a;
     getch();
}

int add( int x , int y ){
      int z = x + y ;
      return z ;
}


output : error

case III :

#include<iostream.h>
#include<conio.h> 

int add(int , int);

void main(){
     clrscr();
     int a = add(10,20) ;
     cout<<a;
     getch();
}

int add( int x , int y ){
      int z = x + y ;
      return z ;
}

output : 30


Now, looking at case I , case II and case III, you came to know what is the importance of Function Prototype and where it is necessary.


Calling a function 

In the above example , you saw that add() is called inside main(). Calling a function means the flow of the program when encounters with the function call , the flow is transferred to the function where it is declared. 
Until and unless the called function's body is not completely executed the  function will not terminated.

#include<iostream.h>
#include<conio.h> 

int add( int x , int y ){
      int z = x + y ;
      cout<<"This is add() body";
      return z ; // it will return the value of z where this function is called
}

void main(){
     clrscr();
     int a = add(10,20) ; // function call
     cout<<"\n"<<a; // this line will not be executed until and unless the add() body is completely executed
     getch();
}

output : 
This is add() body
30

Return keyword

Return is used to either return the processed value by the function to where it has been called or to terminate the function forcefully or both .

case I :

#include<iostream.h>
#include<conio.h> 

int add( int x , int y ){
      int z = x + y ;
      cout<<"This is add() body";
      return z ; // it will return the value of z where this function is called
}

void main(){
     clrscr();
     int a = add(10,20) ; // function call
     cout<<"\n"<<a; // this line will not be executed until and unless the add() body is completely executed
     getch();
}

output : 
This is add() body
30

case II : 
 
#include<iostream.h>
#include<conio.h> 

int add( int x , int y ){
      int z = x + y ;
      cout<<"This is add() body I";
      return z ; // it will return the value of z where this function is called
      cout<<"\nThis is add() body II"; // this line will not be executed because in the above line there  is return keyword
}

void main(){
     clrscr();
     int a = add(10,20) ; // function call
     cout<<"\n"<<a; // this line will not be executed until and unless the add() body is completely executed
     getch();
}

output : 
This is add() body
30



Any question and query, please do comment..

till then, Happy Coding

 

Whats the Output ??

#include <iostream>

using namespace std;

class Test{

    int i; // data member

    public :

        Test(){
            cout<<"\nYou Entered Test Class.";

            i = 10;

            showValue(); // Member function
        }

        void showValue(){
            cout<<"\n i = "<<i;
        }

}obj;

main(){}


Q. Will it be an error  ? If yes why ? If no why ?

Friday 22 May 2015

Using namespace std in Dev C++

Using namespace std :

Its clear from the above that the phase is having three words:-

using- means using what??

namespace - means using a namespace

Std - it is a kind of namespace

Therefore, the phase defines using a namespace of kind std.

Why to use it ??

The built in C++ library routine are kept in the standard namespace. That includes stuff like cout, cin, string, vector, map etc. Because these tools are used so commonly, it's popular to add" using namespace std " at the top of your code source code so that you won't have have to type the std:: prefix contantly before cout, cin etc.





About Dev C++

Dev C++ - Developer C++ is an advanced C++ IDE ( Integrated Development Environment) which has some xtra advanced faetures as compared to other IDEs like Turbo C++, Borland C++ etc.

Dev C++ also comes in little changes in normal syntaxs.

Dev C++ Vs Other IDEs :


  1. In Dev C++, Header File "iostream" is declared without extension ".h"(dot h).
  2. In Dev C++, after Header File declaration , it is required to write a statement 'using namespace std' .
  3. In Dev C++, the return type of the main funtion cannot be 'void'. By deafult the return type is 'int'.
  4. In Dev C++, there is no functions like clrscr() (clear screen). Instead of clrscr(), system("cls") can be preferred.
  5. In Dev C++, memory bytes "int" datatype is 4 byte where as in Other IDEs is 2 byte.
And many more are ...






Main Function

Main Funtion is a function from where the execution of the program starts. It is the first function that is called to perform other operation like calling of other functions etc.

Every C++ program must contain a main function. This means that a main function is a mendatory and necessary funtion and an important one.

Syntax:-

return_type main ()
{
     // body
}

return_type can be void, int, float, char & double. By default the return type of main function is int (integer)

Tuesday 5 May 2015

Header files and their in-built function - 2

2. conio.h - Header File

Syntax:-  #include<conio.h>

This is one of most important header file used in C++ after iostream.h. It means CONSOLE INPUT OUTPUT (con-io).

This header file include some of the important in-built functions like:

i. clrscr():  Full form is "Clear Screen".This functions is used to clear all the previous outputs generated by the program which are not of any use, from the console.

For example: If you run a program with some input and the program is generating some output, and after the termination of the program you tried to run another program without using clrscr(), the output of the program will be shown on the monitor along with the previous outputs of the program you tried last.

Note:- { clrscr() function is only applicable in the IDE like Turbo C++ and Borland C++ not in Dev                 C++. }

ii. getch(): Full form is "Get Character". This funtion is usually used to hold-on the screen or rather used to pause the flow of execution of the program until and unless you press any key from your input device such as keyboard, the execution will not go further.


There are many more in-functions like putch(), getche(), getchar() etc but above two are most important one.


Readers please do not get confuse about functions, they will be discuss later.

For any query and doubts please do drop it in the comment box. It will be pleasure to clearify your doubts.

Happy coding...

Popular Posts

Recent Posts

Unordered List

Text Widget

Powered by Blogger.