-->




Cpp Tutorial Functions in c++ examples

Leave a Comment
This cpp tutorial contains
  • Introduction to functions in c++
  • Different methods to write functions
  • Examples of c++ functions code
  • Types of functions in c++

What are Functions and why they are important in c++ programming Language

A C++ function may have 2 or 3 phases
  1. Prototype
  2. Calling
  3. Definition or body of a function
  •  First one is optional it can be written or not depend upon the code.
  •  The second and third are compulsory to use

The purpose of a function is to do something like in a defining way. Let we want to do 3 different task then we will make 3 different functions and each function name should give us the idea about our function body.
For example we have to calculate prime numbers and detect vowel characters. There should be two functions one will calculate prime and other will check vowels and their name should be like that
calculatePrime();
in the above link there is a prime number program try to do it in a function for practice
CheckVowel();  
or any other name 

 What a function has
  •   return type
  •  It can be any thing like int,  float, or any other data  structure
  •  If we want to that our function not return any thing we use this
  ' void '  means nothing to return from where function has called    If function is not  returning any thing and function name is myfun then we will write it like that
  void myfun(){

}

Function body
  Function body it starts from curly braces and end with curly braces    void myfun(){ // start of function body        cout<<" I AM A Function";          
   }// end of fucntion body

its a function with
Name:        myfun
Return type:   void     

in funtion body we have written
            cout<<" I AM A Function";          

so now its time to use our function How to use the function?
Answer is CALL It
  As program starts from main() function we will use myfun function with in it or simple we can say we will call it within the main().

SO in order to use or call it write its name in main() with curly brackets just like that
#include<iostream> 
using namespace std;
int main()
{  myfun();  return 0; } // here is our function void myfun(){
cout<<"I Am a Function "; }
Lets Run this program Oh.. it will show an error on the compiler
Note: Its a common programming error a beginner can face
 It occurs because our programs run from top to bottom when compiler will reach at
myfun();  // function calling It will not be able to identify the function because compiler does not know what it is
so in order remove this error first we have to tell the compiler what is myfun(); then use it and write function body before the main() in this way compiler will read our function body first and will not show the error..

#include<iostream>
using namespace std;

void myfun(){
cout<<"I Am a Function "; }
int main(){ myfun(); return 0;
}

An alternate method to solve this error is to write function prototype which tells compiler that a function is exist

#include<iostream>
using namespace std;
void myfun();
// it is a prototype int main()
{ myfun();

return 0;
}
void myfun(){
cout<<"I Am a Function "; }

It is always recommend to write prototype when code is large it increase the readability of program because on a look at prototypesit is clear that what a program is about or what it contains.

Function with return type
If a function do some task in its body and want to send or return the result form where it was called then
Identify the data type or result what has to be send or return write this data type before the function name like below
Let this fucntion wants to return an integer
Function body will be look like that

int myfun()
{

  return result; // here result is of integer type
}

if a return type is defined and function is not returning anything then compiler will through an error e.g. "Function myfun must return "

Function with parameters
If we want to send some information in the form of any data type we can send this info at the time of calling it and this function can return the value too.
Let we want a function to which we send to integers it calculates both sum and return the final result

int Sum2Integers(int first, int second)
{
    int result = first+second;
   return result;
// Above to line can be written in one line like that
// return (first+second);

}

To call it form main()
int main()
{
      int myResult;
      myResult=Sum2Integers(5,5); // calling and sending two ints

}
Here cpp tutorial about basics of Cpp functions ends
here are two examples of functions
 more find here C++ simple examples
If you face any problem comment below your problem.

0 comments:

Post a Comment