Your SlideShare is downloading. ×
Function notes
Function notes
Function notes
Function notes
Function notes
Function notes
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Function notes

501

Published on

Published in: Business, Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
501
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
18
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. By : Rakesh Kumar D.A.V. Centenary Public School, Chander Nagar FunctionFunction is a small sub program , which is designed to perform a particular task in a complete program andit 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 screenvoid print_message(void) 1. void at the position of return type shows that{ the function does not return any value to it’s cout<<”n Hello world”; calling function. return ; 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 areturn. How to implement this function in a C++ programMethod -1 Method-2#include<iostream> #include<iostream>#include<conio.h> #include<conio.h>using namespace std; using namespace std;void print_message(void) void print_message(void); // function prototype{ int main() cout<<"n Hello world"; { return; print_message(); // function call } getch();int main() return 0;{ } print_message(); getch(); void print_message(void) return 0; {} 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 functions 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.
  • 2. By : Rakesh Kumar D.A.V. Centenary Public School, Chander Nagar TYPE-I Main function User defined Function Input Phase USE UDF Processing Phase function here Output Phase Problem : Write a function to read base and height of a triangle and find out area of a right angle Triangle ( Area = ½ *b*h). Also implement this function in a C++ program.SolutionProgram 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;}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( ) TYPE –II Main Function User defined function 1. Input 2. Processing 3. result must return to it’s Output Here calling function
  • 3. By : Rakesh Kumar D.A.V. Centenary Public School, Chander NagarType –II Problem : Write a function in C++ to read base and height of a right angle triangle, calculatearea of triangle using formula area = ½*b*h and return it to it’s calling function. Also implement thisfunction in a C++ programSolution 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; } TYPE-III Main function USER defined function 1. Input 2. Processing 3. OutputType –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 andalso display this calculated area on the screen. Also implement this function in C++ programSolution 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; }
  • 4. By : Rakesh Kumar D.A.V. Centenary Public School, Chander NagarType –IV Input Travels from main to UDF Main Function User defined Function 1. Input 2. Processing 3. Output Phase Result Travels from UDF to Main and used inside mainTYPE –IV Problem : Write a user defined function in C++ Area_Triangle( ) which accept two integer typeparameter (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++ programSolutionProblem 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;}Parameter Types • Call by Value method • Call By reference methodCall 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 doesnot 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();
  • 5. By : Rakesh Kumar D.A.V. Centenary Public School, Chander Nagar return 0;}Call By reference Method : In this method actual parameter pass the address of actual parameter. So thechanges 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, actualparameter must be of variable typeScope 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 variableProgram Output#include<iostream>#include<conio.h>using namespace std;int a=20; // Global Variableint main() { int a=10; // Local Variable { int a=30; cout<<"n Value of a :"<<::a; } cout<<"n Value of a :"<<a; getch(); return 0; } •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() {
  • 6. By : Rakesh Kumar D.A.V. Centenary Public School, Chander Nagar show(); show(); show(); getch(); return 0; }Parameter with default ValuesExmaple 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;}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 parameterExample #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. …………. ………… }

×