1 / 88

Functions

Functions. What is function????. Function is a self contained block of statements that perform a coherent task of some kind. Every C++ program can be a thought of the collection of functions. main( ) is also a function. Types of Functions. Library functions

sdominique
Download Presentation

Functions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Functions

  2. What is function???? • Function is a self contained block of statements that perform a coherent task of some kind. • Every C++ program can be a thought of the collection of functions. • main( ) is also a function.

  3. Types of Functions. • Library functions • These are the in- -built functions of ‘C++ ’library. • These are already defined in header files. • e.g. Cout<<; is a function which is used to print at output. It is defined in ‘iostream.h ’ file . • User defined functions. • Programmer can create their own function in C++ to perform specific task

  4. Actual arguments:-the arguments of calling function are actual arguments. • Formal arguments:-arguments of called function are formal arguments. • Argument list:-means variable name enclosed within the paranthesis.they must be separated by comma • Return value:-it is the outcome of the function. the result obtained by the function is sent back to the calling function through the return statement.

  5. Why use function?

  6. Writing functions avoids rewriting of the same code again and again in the program. • Using function large programs can be reduced to smaller ones. It is easy to debug and find out the errors in it. • Using a function it becomes easier to write program to keep track of what they are doing.

  7. Function Declarationret_typefunc_name(data_type par1,data_type par2); Function Defination ret_typefunc_name(data_type par1,data_type par2) { } Function Call func_name(data_type par1,data_type par2);

  8. Function prototype • A prototype statement helps the compiler to check the return type and arguments type of the function. • A prototype function consist of the functions return type, name and argument list. • Example • int sum( int x, int y);

  9. Example #include<iostream.h> #include<conio.h> void main() { clrscr(); void sum(int, int); // function declaration int a, b, c; cout<<“enter the two no”; cin>>a>>b>>c; sum(a,b); // function calling getch(); } void sum( int x, int y)// function definition { c=x+y; cout<< “ sum is”<<c; }

  10. #include<conio.h> void main() { clrscr(); int a=10,b=20; int sum(int, int); int c=sum(a,b); /*actual arguments Cout<<“sum is” << c; getch(); } int sum(int x, int y) /*formal arguments { int s; s=x+y; return(s); /*return value }

  11. Categories of functions • A function with no parameter and no return value • A function with parameter and no return value • A function with parameter and return value • A function without parameter and return value

  12. A function with no parameter and no return value #include<conio.h> void main() { clrscr(); void print(); /*func declaration Cout<<“no parameter and no return value”; print(); /*func calling getch(); } void print() /*funcdefination { For(inti=1;i<=30;i++) { cout<<“*”; } Cout<<“\n”; }

  13. A function with no parameter and no return value • There is no data transfer between calling and called function • The function is only executed and nothing is obtained • Such functions may be used to print some messages, draw stars etc

  14. A function with parameter and no return value #include<conio.h> void main() { clrscr(); int a=10,b=20; void mul(int,int); mul(a,b); /*actual arguments getch(); } void mul(int x, int y) /*formal arguments { int s; s=x*y; Cout<<“mul is” << s; }

  15. A function with parameter and return value #include<conio.h> void main() { clrscr(); int a=10,b=20,c; int max(int,int); c=max(a,b); Cout<<“greatest no is” <<c; getch(); } int max(int x, int y) { if(x>y) return(x); else { return(y); } }

  16. A function without parameter and return value #include<conio.h> void main() { clrscr(); int a=10,b=20; int sum(); int c=sum(); /*actual arguments Cout<<“sum is”<< c; getch(); } int sum() /*formal arguments { int x=10,y=30; return(x+y); /*return value }

  17. Some important points • A function declaration,defination,and call must match in number,type and order of actual and formal arguments. • A function can b called from other function but a function cannot be defined inside another function • Function defined and function call order can b different • The actual arguments in a function can be variables,constants,or expression where as formal arguments must be variables

  18. ARGUMENT PASSING TECHNIQUES

  19. Argument passing techniques • Pass By Value • Pass By Reference • Pass By Pointer\address

  20. Call By Value • It is a default mechanism for argument passing. • When an argument is passed by value then the copy of argument is made know as formal arguments which is stored at separate memory location • Any changes made in the formal argument are not reflected back to actual argument, rather they remain local to the block which are lost once the control is returned back to calling program

  21. Example void main() { int a=10,b=20; int swap(int,int); Cout<<“before function calling”<<a<<b; swap(a,b); Cout<<“after function calling”<<a<<b; getch(); Return 0; }

  22. void swap(int x,int y) { int z; z=x; x=y; y=z; Cout<<“value is”<<x<<y; }

  23. Output: before function calling a=10 b=20 value of x=20 and y= 10 after function calling a=10 b=20

  24. Call By pointer/address • In this instead of passing value, address are passed. • Here formal arguments are pointers to the actual arguments • Hence change made in the argument are permanent. • The address of arguments is passed by preceding the address operator(&) with the name of the variable whose value you want to modify. • The formal arguments are processed by asterisk (*) which acts as a pointer variable to store the addresses of the actual arguments

  25. Example Void main() { int a=10 ,b=25; void swap(int *,int *); Cout<<“before function calling”<<a<<b; swap(&a,&b); Cout<<“after function calling”<<a<<b; getch(); }

  26. void swap(int *x,int *y) { int z; z=*x; *x=*y; *y=z; Cout<<“value is”<<*x<<*y; }

  27. Output: before function calling a= 10 b= 25 value of x=25 and y=10 after function calling a=25 b= 10

  28. Using Reference Variables with Functions • To create a second name for a variable in a program, you can generate an alias, or an alternate name • In C++ a variable that acts as an alias for another variable is called a reference variable, or simply a reference

  29. Declaring Reference Variables • You declare a reference variable by placing a type and an ampersand in front of a variable name, as in double &cash; and assigning another variable of the same type to the reference variable double someMoney; double &cash = someMoney; • A reference variable refers to the same memory address as does a variable, and a pointer holds the memory address of a variable

  30. Pass By Reference void main() { int i=10; int &j=i; // j is a reference variable of I cout<<“value”<<i<<“\t”<<j; j=20; cout<<“modified value”<<i<<“\t”<<j; getch(); }

  31. Output:- Value 10 10 modified value 20 20

  32. Declaring Reference Variables • There are two differences between reference variables and pointers: • Pointers are more flexible • Reference variables are easier to use • You assign a value to a pointer by inserting an ampersand in front of the name of the variable whose address you want to store in the pointer • It shows that when you want to use the value stored in the pointer, you must use the asterisk to dereference the pointer, or use the value to which it points, instead of the address it holds

  33. Call by valueThis method copies the actual value of an argument into the formal parameter of the function. In this case, changes made to the parameter inside the function have no effect on the argument.

  34. . Call by pointerThis method copies the address of an argument into the formal parameter. Inside the function, the address is used to access the actual argument used in the call. This means that changes made to the parameter affect the argument.

  35. Call by referenceThis method copies the reference of an argument into the formal parameter. Inside the function, the reference is used to access the actual argument used in the call. This means that changes made to the parameter affect the argument.

  36. INLINE FUNCTIONS

  37. Inline Functions • Each time you call a function in a C++ program, the computer must do the following: • Remember where to return when the function eventually ends • Provide memory for the function’s variables • Provide memory for any value returned by the function • Pass control to the function • Pass control back to the calling program • This extra activity constitutes the overhead, or cost of doing business, involved in calling a function.

  38. Solution to this is to replace each function call with the necessary code instead of making function. • Inline function is a function which gets expanded inline at each point in the program where it is called. • In other words inline functions are those functions whose function body is inserted in place of the function call during the compilation process. • Syntax: inline ret_type func_name(parameter_list) { function body }

  39. inline int max(intx,int y) { return (x>y?x:y); } int main() { int m=10,n=25; inta,b; a=max(6,8); cout<<“greatest is”<<a; b=max(m,n); cout<<“greates of m=“<<m<<“ and n=“<<n <<“is “<<b; getch(); return 0; }

  40. Output : Greatest of 6 and 8=8 Greatest of m=10 and n=25 is 25

  41. An inline function is a small function with no calling overhead • Overhead is avoided because program control never transfers to the function • A copy of the function statements is placed directly into the compiled calling program • The inline function appears prior to the main(), which calls it • Any inline function must precede any function that calls it, which eliminates the need for prototyping in the calling function

  42. When you compile a program, the code for the inline function is placed directly within the main() function • You should use an inline function only in the following situations: • When you want to group statements together so that you can use a function name • When the number of statements is small (one or two lines in the body of the function) • When the function is called on few occasions

  43. Disadvantages: • All functions that uses the inline function must be recompiled if any changes are made to the inline function. • Although inline function reduces the execution time. It increases the size of the executable file as multiple copies of the inline functions body are inserted in the programs. So it is always recommended to use inline functions for small frequently used functions. • The definition of inline function should appear before the function call.

  44. DEFAULT ARGUMENTS

  45. DEFAULT ARGUMENTS • Default arguments is useful in situation where a arguments has the same value in most function calls. • When the desired value is different from the default value,the desired value can be specified explicity in a function call. • This changes the default value of the argument to be overridden for the duration of function call. • If in the function call all the arguments are not specified then the omitted arguments can take default value by providing them in the function declaration.

  46. void volume(int l=10,int w=10,int h=10); //function declatration int main() { volume (); volume(5); volume(8,6); volume(6,4,5); getch(); return 0; } void volume(int l,intw,int h) { Cout<<“volume =“<<l*w*h<<endl; } Output: Volume:1000 Volume:500 Volume:480 Volume:120

  47. Using Default Arguments • When you don’t provide enough arguments in a function call, you usually want the compiler to issue a warning message for this error • Sometimes it is useful to create a function that supplies a default value for any missing parameters

  48. Using Default Arguments • Two rules apply to default parameters: • If you assign a default value to any variable in a function prototype’s parameter list, then all parameters to the right of that variable also must have default values • If you omit any argument when you call a function that has default parameters, then you also must leave out all arguments to the right of that argument

  49. Examples of Legal and Illegal Use of Functions with Default Parameters

More Related