Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Functions  A function is a named, independent section of C++ code that performs a specific task and optionally returns a value to the calling program.

Similar presentations


Presentation on theme: "1 Functions  A function is a named, independent section of C++ code that performs a specific task and optionally returns a value to the calling program."— Presentation transcript:

1 1 Functions  A function is a named, independent section of C++ code that performs a specific task and optionally returns a value to the calling program.  A function definition consists of two aspects:  prototype  proper definition

2 2 Functions syntax is: return_type function_name ( type [parameterName]...);

3 3 Functions The proper definition of a function consists of the function header and its body Function definition syntax is: return_type function_name ( [type parameterName]...) { statements; }

4 4 Functions Function definition

5 5 Functions Parameters and Arguments There are three ways to pass data from one function to another: By value By address By reference

6 6 Functions #include void swap1 (int x, int y)// pass-by-value (objects) { printf("Function swap1\n" ); printf("Initial values: x=%d, y=%d\n ",x, y) ; int temp = x; x = y; y = temp; printf("Final values: x=%d y= %d\n ", x, y); }

7 7 Functions void swap2 (int *x, int *y)// pass-by-address (pointers) { printf("Function swap2\n "); printf("Initial values: *x=%d, *y=%d\n", *x, *y); int temp = *x; *x = *y; *y = temp; printf("Final values: *x=%d, *y=%d\n",*x, *y ); }

8 8 Functions void swap3 (int &x, int &y)// pass-by-reference { printf("Function swap3\n"); printf("Initial values: x=%d, y=%d\n", x, y) ; int temp = x; x = y; y = temp; printf("Final values: x=%d, y=%d ", x, y ); }

9 9 Functions void main(void) { int a, b; printf("a= "); scanf("%d", &a); printf("b= "); scanf(" %d ", &b); swap1( a, b); printf(“a= %d, b=%d\n“, a, b); swap2( &a, &b); printf(“a= %d, b=%d”, a, b ); swap3( a, b); printf(“a= %d, b=%d”, a, b ); }

10 10 Functions  Scope of variables:  Global  Local Scope  Global variables are dangerous because they are shared data  scope access (or resolution) operator :: (two semicolons) to access a global (or file duration) name even if it is hidden by a local redeclaration of that name.

11 11 Functions  Modifiers of memory location:  auto  register  static  extern

12 12 Functions  Command Line Arguments When a program is executed under an operating system (such as DOS or UNIX), it is able to pass zero or more arguments. These arguments appear after the program executable name and are separated by blanks. Because they appear on the same line as where operating system commands are issued, they are called command line arguments. Command line arguments are made available to a C++ program via the main function.

13 13 Functions The declaration of main looks like this: int main(int argc, char *argv[ ]); There are at least two arguments to main: argc and argv. The first of these is a count of the arguments supplied to the program and the second is an array of pointers to the strings which are those arguments—its type is (almost) ‘array of pointer to char’. These arguments are passed to the program by the host system's command line interpreter or job control language.

14 14 Functions #include int main (int argc, const char *argv[ ]) { double sum = 0; for (int i = 1; i < argc; ++i) sum += atof(argv[i]); printf(“ sum=%d\n”, sum ); return 0; }

15 15 Functions  Variable Number of Arguments It is sometimes desirable to have functions which take a variable number of arguments. The declaration is: tip_returned name_of_function(fix_parameters, …);

16 16 Functions #include int Menu (char *option1,...) { va_list args;// argument list char* option = option1; int count = 0, choice = 0; va_start(args, option1);// initialize args do { printf(“%d. %s \n”, ++count, option); } while ((option = va_arg(args, char*)) != 0); va_end(args);// clean up args printf("option? “); scanf(“%d”, &choice); return (choice > 0 && choice <= count) ? choice : 0; }

17 17 Functions The sample call int n = Menu( "Open file", "Close file", "Revert to saved file", "Delete file", "Quit application", 0); will produce the following output: 1. Open file 2. Close file 3. Revert to saved file 4. Delete file 5. Quit application option?

18 18 Functions  Recursion –A function can call itself. A function which calls itself is said to be recursive long fact (unsigned int n) { if (n<=1) return 1; else return n*fact(n-1); }

19 19 Functions  Inline Functions If a function is declared with the keyword inline, the compiler copies the code from the inline function directly into the calling function. If the function is called 10 times, the inline code is copied into the calling functions each of those 10 times. Effects: the execution speed increases size of executable program increases

20 20 Functions Example of inline function inline int Max (int a, int b) { return a > b ? a : b; }

21 21 Functions  Default Arguments #include void f (int i, int j = 25, float r = 2.5) { printf( "\n%d, %d, %f”, i, j, r); } void main() { f(3,5,1.5); (3,5); f(3); f(); // error, too few parameters ! }

22 22 Functions  Overloading Functions C++ enables to create more than one function with the same name. This is called function overloading (or polymorphism). The functions must differ in their parameter list. int func (int, int); int func (long, long); long func (long);

23 23 Functions /*overloading functions*/ #include int sum(int a,int b) { return a+b; } double sum(double a, double b) { return a+b;} char * sum(char *a, char *b) {return strcat(a,b);} void main() {printf( "42+17 = %d \n“, sum(42,17)); printf( "42.0+17.0 = %lf\n", sum(42.0,17.0)); printf( " C++ + is the best = %s“, sum("C++ ", "is the best !")); }

24 24 Functions  Function pointers  Function Pointers are pointers, i.e. variables, which point to the address of a function.  A running program gets a certain space in the main-memory. Both, the executable compiled program code and the used variables, are put inside this memory.  A function name is an address.

25 25 Functions #include void func1() {printf(“\ncall of function 1”);} void func2(int x) {printf(“\ncall of function 2”); printf(“\nx = %d”, x); } void main() { func1();//call of func1 func1;//address of func1 func2(5);//call of func2 func2;//address of func2 }

26 26 Functions Syntax of declaration of function pointers: type (* pointer_name)(parameter_list);  A function pointer always points to a function with a specific signature!  All functions, you want to use with the same function pointer, must have the same parameters and return-type!

27 27 Functions #include int comp1(char * s1, char *s2) {return s1[0] - s2[0]; } int comp2(char * s1, char *s2) {return strlen(s1) - strlen(s2); } void test(char* s1, char* s2, int(*p)(char*,char*)) {if (p(s1,s2)==0) printf("\nSirurule sunt identice“); else if (p(s1,s2)>0) printf("\nPrimul sir e mai mare“); else printf("\nAl doilea sir este mai mare“); }

28 28 Functions void main() { int (*p)(char*,char*); p=comp1; test("abc", "ABC", p); p=comp2; test("abc", "ABC", p); test("abc", "ABC", comp1); test("abc", "ABC", comp2); }


Download ppt "1 Functions  A function is a named, independent section of C++ code that performs a specific task and optionally returns a value to the calling program."

Similar presentations


Ads by Google