1 Functions A function is a named, independent section of C++ code that performs a specific task...

28
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

Transcript of 1 Functions A function is a named, independent section of C++ code that performs a specific task...

Page 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.

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

Page 2: 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.

2

Functions

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

Page 3: 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.

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;

}

Page 4: 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.

4

FunctionsFunction definition

Page 5: 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.

5

Functions• Parameters and Arguments

There are three ways to pass data from one function to another:

• By value • By address • By reference

Page 6: 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.

6

Functions#include <stdio.h>

 

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);

}

Page 7: 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.

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 );

}

Page 8: 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.

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 );

}

Page 9: 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.

9

Functionsvoid 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 );

}

Page 10: 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.

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.

Page 11: 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.

11

Functions

Modifiers of memory location:

auto register static extern

Page 12: 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.

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.

Page 13: 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.

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.

Page 14: 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.

14

Functions

#include <stdio.h>#include <stdlib.h>

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;

}

Page 15: 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.

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, …);

Page 16: 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.

16

Functions #include <stdio.h>#include <stdarg.h>

int Menu (char *option1, ...){

va_list args; // argument listchar* option = option1;int count = 0, choice = 0;

va_start(args, option1); // initialize argsdo {

printf(“%d . %s \n”, ++count, option);} while ((option = va_arg(args, char*)) != 0);va_end(args); // clean up argsprintf("option? “);scanf(“%d”, &choice);return (choice > 0 && choice <= count) ? choice : 0;

}

Page 17: 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.

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 file2. Close file3. Revert to saved file4. Delete file5. Quit applicationoption?

Page 18: 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.

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);

}

Page 19: 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.

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

Page 20: 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.

20

Functions

Example of inline function

inline int Max (int a, int b)

{

return a > b ? a : b;

}

Page 21: 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.

21

Functions Default Arguments

#include <stdio.h>

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 !}

Page 22: 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.

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);

Page 23: 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.

23

Functions/*overloading functions*/

#include <stdio.h>#include<string.h>

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 !"));}

Page 24: 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.

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.

Page 25: 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.

25

Functions#include <stdio.h>

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 func1func1; //address of func1func2(5); //call of func2func2; //address of func2

}

Page 26: 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.

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!

Page 27: 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.

27

Functions#include <stdio.h>#include <string.h>

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“);}

Page 28: 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.

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);

}