Wednesday, January 25, 2012

Function In C++ – Simplest Definition and Explanation Notes

Function is a small sub program , which is designed to perform a particular task in a complete program and it is designed in this way that it can be coupled with another function.

return type function_name ( argument)

{

statement; function Syntax

}

where

return type : Data type of the value return by the function

function_name : Any Valid identifier

Statement : Any Valid C/C++ statement(s)

Our first Function : To print “hello world” on the screen

void print_message(void)

{

cout<<”\n Hello world”;

return ;

}

  1. void at the position of return type shows that the function does not return any value to it’s calling function.
  2. Void at the position of argument shows that the function does not accept any argument.

NOTE : return is not compulsory, but good programming skill recommends every function should have a return.

How to implement this function in a C++ program

Method -1

Method-2

#include<iostream>

#include<conio.h>

using namespace std;

void print_message(void)

{

cout<<"\n Hello world";

return;

}

int main()

{

print_message();

getch();

return 0;

}

#include<iostream>

#include<conio.h>

using namespace std;

void print_message(void); // function prototype

int main()

{

print_message(); // function call

getch();

return 0;

}

void print_message(void)

{

cout<<"\n Hello world";

return;

}

Function Prototype: A function prototype in C or C++ is a declaration of a function that omits the function

body but does specify the function's name,  argument types and return type. While a

function definition specifies what a function does, a function prototype can be

thought of as specifying its interface.

TYPE-I

Solution

Program

Output

#include<iostream>

#include<conio.h>

using namespace std;

void area_triangle(void)

{

int b,h,ar;

system("cls"); // clrscr()

cout<<"\n Enter base :";

cin>>b;

cout<<"\n Enter height :";

cin>>h;

ar =0.5*b*h;

cout<<"\n Area of Triangle :"<<ar;

return;

}

int main()

{

area_triangle(); // function Call

getch();

return 0;

}

clip_image005

NOTE : main( ) is also a function. It has the following feature

without main program can not execute.

A program can have more than one function but can not have more than one main ( )

Program execution begin from main( )

Main Function

Output Here

TYPE –II

User defined function

1. Input

2. Processing

3. result must return to it’s calling function

Type –II Problem : Write a function in C++ to read base and height of a right angle triangle, calculate area of triangle using formula area = ½*b*h and return it to it’s calling function. Also implement this function in a C++ program

Solution

Program

Output

#include<iostream>

#include<conio.h>

using namespace std;

int area_triangle(void)

{

int b,h,ar;

cout<<"\n Enter base :";

cin>>b;

cout<<"\n Enter Height :";

cin>>h;

ar =int(0.5*b*h);

return (ar);

}

int main()

{

int res =area_triangle();

cout<<"\n Area of Triangle :"<<res;

getch();

return 0;

}

clip_image008

Type –III Problem : Define a function in C++ Area_triangle( ) which accept two integer type parameter (i) int base (ii) int height. This function calculates area of Triangle using formula area = 1/2*base*height and also display this calculated area on the screen. Also implement this function in C++ program

Solution

Program

Output

#include<iostream>

#include<conio.h>

using namespace std;

void area_triangle(int base, int height)

{

int ar;

ar =int(0.5*base*height);

cout<<"\n Area of Triangle :"<<ar;

}

int main()

{

area_triangle(10,20);

getch();

return 0;

}

clip_image011

Type –IV

TYPE –IV Problem : Write a user defined function in C++ Area_Triangle( ) which accept two integer type parameter (i) int base (ii) int height. This function calculates area of triangle using formula area = ½*base*height and return it to it’s calling function. Also implement this function in C++ program

Solution

Problem

Output

#include<iostream>

#include<conio.h>

using namespace std;

int area_triangle(int base, int height)

{

int ar;

ar =int(0.5*base*height);

return(ar);

}

int main()

{

int res =area_triangle(10,20);

cout<<"\n Area of Triangle :"<<res;

getch();

return 0;

}

clip_image016

Parameter Types

  • Call by Value method
  • Call By reference method

Call By Value Method: In this method actual parameter make it’s copy and send to formal parameter. The processing inside function use this copy of actual parameter. So the changes made inside function does not automatically available to it’s calling function.

Program

Output

#include<iostream>

#include<conio.h>

using namespace std;

void add ( int a) // formal Parameter

{

a = a+10;

}

int main()

{

int x=10;

cout<<"\nBefore function call x :"<<x;

add(x);

cout<<"\nafter function call x :"<<x;

getch();

return 0;

}

clip_image018

Call By reference Method : In this method actual parameter pass the address of actual parameter. So the changes made inside function is automatically available to it’s calling function.

Program

output

#include<iostream>

#include<conio.h>

using namespace std;

void add ( int &a) // Call by reference

{

a = a+10;

}

int main()

{

int x=10;

cout<<"\nBefore function call x :"<<x;

add(x);

cout<<"\nafter function call x :"<<x;

getch();

return 0;

}

NOTE : in case of reference type parameter, actual parameter must be of variable type

clip_image020

Scope of Variable

  • Auto / Local Variable : The variable whose life begins within opening curly braces and it dies at the position of it’s corresponding curly braces, is called local variable
  • Global Variable : The variable whose scope is whole program , and is defined outside function ,is called global variable

Program

Output

#include<iostream>

#include<conio.h>

using namespace std;

int a=20; // Global Variable

int main()

{

int a=10; // Local Variable

{

int a=30;

cout<<"\n Value of a :"<<::a;

}

cout<<"\n Value of a :"<<a;

getch();

return 0;

}

clip_image022

  • Static Variable : These are local variable to that function where it is defind , but does not loose their values between the function call.

Example Program

Output

#include<iostream>

#include<conio.h>

using namespace std;

void show(void)

{

static int a=0;

a++;

cout<<"\n Value of a :"<<a;

}

int main()

{

show();

show();

show();

getch();

return 0;

}

clip_image024

Parameter with default Values

Exmaple Program

Output

#include<iostream>

#include<conio.h>

using namespace std;

void show(int a=10)

{

cout<<"\n Value of a :"<<a;

}

int main()

{

show();

show(30);

getch();

return 0;

}

clip_image026

Some Additional Definition_______________________________________________________________

Formal Parameter : The parameter which appears with function prototype is called formal parameter.

Actual parameter : The Parameter which is used at the time of call function , is called actual parameter

Example

#include<iostream>

#include<conio.h>

void show ( int a) // here a is formal parameter

{

………….

………….

}

int main( )

{

int x =10;

Show (x) ; // x is here actual parameter.

………….

…………

}

No comments:

Post a Comment