Download presentation
Presentation is loading. Please wait.
1
Lecture 4: Functions and Pointer to Function
Object Oriented Programming (C++) Lecture 4: Functions and Pointer to Function
2
Contents: Function declaration Function definition Inline function
Static variables Arguments of function Function overloading
3
Function Declaration A function declaration gives the name of the function, the type of the value returned (if any) by the function, and the number and types of the arguments that must be supplied in a call of the function. For example: char* strcpy(char* to, const char* from) ; void exit(int) ; The presentation is split up in 3 sections. Section 1 is contains some background information on C++ to, a short comparison to C and then an introduction to some basics that might be useful for the examples.
4
Function Declaration(cont.)
Declaration principle: 1) If a function is defined before its being used, the declaration may be omitted; 2) If a function is used before its definition, it must be declared in advance. Declaration method: double sqrt(double n) ; double sqrt(double) ;//the argument’s name can be omitted in the declaration. Use: double sr2 = sqrt (2) ; / / ok double sq3 = sqrt ("three") ; / / error, can’t be matched The presentation is split up in 3 sections. Section 1 is contains some background information on C++ to, a short comparison to C and then an introduction to some basics that might be useful for the examples.
5
Function Definition A function definition is a function declaration in which the body of the function is presented. For example: extern void swap(int*, int*) ; / / a declaration void swap(int* p, int* q) / / a definition { int t= *p; *p= *q; *q = t; } The C language was developed mainly by Dennis Ritchie from Bell Labs in the Early ’70s as a programming language used to develop the Unix operating system. Many implementations of the C language followed and only in 1989 was the ANSI standard for C published. In the early ’80s, Bjarne Stroustrup, also from Bell Labs, started development of a language based on C but which included Object Orientated concepts from other languages such as Simula67. This was known as “C with Classes”. In October 1985 the first commercial release of the language appeared together with the first edition of the book: “The C++ Programming Language” by Bjarne Stroustrup. In November 1997, the ANSI standard for C++ was published. Nowadays, C++ is the most widely used programming language in the world to develop applications. The reasons include its portability, flexibility, code reuse and quality.
6
Function Definition (cont.)
Note: The type of the definition and all declarations for a function must specify the same type. The argument names, however, are not part of the type and need not be identical. It is not uncommon to have function definitions with unused arguments: void search(table* t, const char* key, const char*) { / / no use of the third argument } The C language was developed mainly by Dennis Ritchie from Bell Labs in the Early ’70s as a programming language used to develop the Unix operating system. Many implementations of the C language followed and only in 1989 was the ANSI standard for C published. In the early ’80s, Bjarne Stroustrup, also from Bell Labs, started development of a language based on C but which included Object Orientated concepts from other languages such as Simula67. This was known as “C with Classes”. In October 1985 the first commercial release of the language appeared together with the first edition of the book: “The C++ Programming Language” by Bjarne Stroustrup. In November 1997, the ANSI standard for C++ was published. Nowadays, C++ is the most widely used programming language in the world to develop applications. The reasons include its portability, flexibility, code reuse and quality.
7
Function’s type 1 Getting parameters and returning value. Eg:
int bigger(int a, int b) { return(a>b)?a:b; } 2 Getting parameters but not return value. Eg: void delay(long a) for(int i=1;i<=a;i++); The C language was developed mainly by Dennis Ritchie from Bell Labs in the Early ’70s as a programming language used to develop the Unix operating system. Many implementations of the C language followed and only in 1989 was the ANSI standard for C published. In the early ’80s, Bjarne Stroustrup, also from Bell Labs, started development of a language based on C but which included Object Orientated concepts from other languages such as Simula67. This was known as “C with Classes”. In October 1985 the first commercial release of the language appeared together with the first edition of the book: “The C++ Programming Language” by Bjarne Stroustrup. In November 1997, the ANSI standard for C++ was published. Nowadays, C++ is the most widely used programming language in the world to develop applications. The reasons include its portability, flexibility, code reuse and quality.
8
Function’s type (cont.)
3 Getting no parameters but returning value. Eg: int geti() { int x; cout<<“Please input a interger:\n”; cin>>x; return x; } 4 Getting no parameters and returning no value. Eg: void message() { cout<<“This is a message.\n” The C language was developed mainly by Dennis Ritchie from Bell Labs in the Early ’70s as a programming language used to develop the Unix operating system. Many implementations of the C language followed and only in 1989 was the ANSI standard for C published. In the early ’80s, Bjarne Stroustrup, also from Bell Labs, started development of a language based on C but which included Object Orientated concepts from other languages such as Simula67. This was known as “C with Classes”. In October 1985 the first commercial release of the language appeared together with the first edition of the book: “The C++ Programming Language” by Bjarne Stroustrup. In November 1997, the ANSI standard for C++ was published. Nowadays, C++ is the most widely used programming language in the world to develop applications. The reasons include its portability, flexibility, code reuse and quality.
9
Example Get resolution of Xn #include <iostream.h>
double power (double, int); //declaration void main (void) { cout << "5 to the power 2 is " << power(5,2) << endl; } double power (double x, int n) //definition double val = 1.0; while (n--) val = val*x; return(val); The C language was developed mainly by Dennis Ritchie from Bell Labs in the Early ’70s as a programming language used to develop the Unix operating system. Many implementations of the C language followed and only in 1989 was the ANSI standard for C published. In the early ’80s, Bjarne Stroustrup, also from Bell Labs, started development of a language based on C but which included Object Orientated concepts from other languages such as Simula67. This was known as “C with Classes”. In October 1985 the first commercial release of the language appeared together with the first edition of the book: “The C++ Programming Language” by Bjarne Stroustrup. In November 1997, the ANSI standard for C++ was published. Nowadays, C++ is the most widely used programming language in the world to develop applications. The reasons include its portability, flexibility, code reuse and quality. Result: 5 to the power 2 is 25
10
inline function Origination:
Calling function need a certain time. If some function is called frequently, it may need much time,and will reduce the program’s executing efficiency. Definition: While compiling, the compiler can built the called function’s code into source function. This built-in function is called inline function. Declaration: using keyword inline, syntax form is like the following: inline double Cub(double x); The C language was developed mainly by Dennis Ritchie from Bell Labs in the Early ’70s as a programming language used to develop the Unix operating system. Many implementations of the C language followed and only in 1989 was the ANSI standard for C published. In the early ’80s, Bjarne Stroustrup, also from Bell Labs, started development of a language based on C but which included Object Orientated concepts from other languages such as Simula67. This was known as “C with Classes”. In October 1985 the first commercial release of the language appeared together with the first edition of the book: “The C++ Programming Language” by Bjarne Stroustrup. In November 1997, the ANSI standard for C++ was published. Nowadays, C++ is the most widely used programming language in the world to develop applications. The reasons include its portability, flexibility, code reuse and quality.
11
inline function(cont.)
Notes: Inside inline function, there are no cycle and switch sentence. Inline function must be declared before its being used. Recursion function can’t be treated as inline function. Substituting for the code can save time of transferring parameters and program shift, but it also needs more memory space . The C language was developed mainly by Dennis Ritchie from Bell Labs in the Early ’70s as a programming language used to develop the Unix operating system. Many implementations of the C language followed and only in 1989 was the ANSI standard for C published. In the early ’80s, Bjarne Stroustrup, also from Bell Labs, started development of a language based on C but which included Object Orientated concepts from other languages such as Simula67. This was known as “C with Classes”. In October 1985 the first commercial release of the language appeared together with the first edition of the book: “The C++ Programming Language” by Bjarne Stroustrup. In November 1997, the ANSI standard for C++ was published. Nowadays, C++ is the most widely used programming language in the world to develop applications. The reasons include its portability, flexibility, code reuse and quality.
12
Example Result: 28.26 #include<iostream.h> const double pi=3.14;
inline double CalArea (double radius) { return pi*radius*radius; } int main( ) double r=3.0; double area; area=CalArea(r); cout<<area<<endl; return 0; The C language was developed mainly by Dennis Ritchie from Bell Labs in the Early ’70s as a programming language used to develop the Unix operating system. Many implementations of the C language followed and only in 1989 was the ANSI standard for C published. In the early ’80s, Bjarne Stroustrup, also from Bell Labs, started development of a language based on C but which included Object Orientated concepts from other languages such as Simula67. This was known as “C with Classes”. In October 1985 the first commercial release of the language appeared together with the first edition of the book: “The C++ Programming Language” by Bjarne Stroustrup. In November 1997, the ANSI standard for C++ was published. Nowadays, C++ is the most widely used programming language in the world to develop applications. The reasons include its portability, flexibility, code reuse and quality. Result: 28.26
13
Static variables Definition: If a local variable is declared static, a single, statically allocated object will be used to represent that variable in all calls of the function. It will be initialized only at the first time the thread of execution reaches its definition. For example: void f(int a) { while (a--) static int n = 0; / / initialized once int x = 0; / / initialized n times cout << "n == " << n++ << ", x == " << x++ << ´\n´; } The C language was developed mainly by Dennis Ritchie from Bell Labs in the Early ’70s as a programming language used to develop the Unix operating system. Many implementations of the C language followed and only in 1989 was the ANSI standard for C published. In the early ’80s, Bjarne Stroustrup, also from Bell Labs, started development of a language based on C but which included Object Orientated concepts from other languages such as Simula67. This was known as “C with Classes”. In October 1985 the first commercial release of the language appeared together with the first edition of the book: “The C++ Programming Language” by Bjarne Stroustrup. In November 1997, the ANSI standard for C++ was published. Nowadays, C++ is the most widely used programming language in the world to develop applications. The reasons include its portability, flexibility, code reuse and quality.
14
Static variables (cont.)
void main () { f (3) ; } Result: n == 0, x == 0 n == 1, x == 0 n == 2, x == 0 A static variable provides a function with ‘‘a memory’’ without introducing a global variable that might be accessed and corrupted unconsciously by other functions (see also §10.2.4). The C language was developed mainly by Dennis Ritchie from Bell Labs in the Early ’70s as a programming language used to develop the Unix operating system. Many implementations of the C language followed and only in 1989 was the ANSI standard for C published. In the early ’80s, Bjarne Stroustrup, also from Bell Labs, started development of a language based on C but which included Object Orientated concepts from other languages such as Simula67. This was known as “C with Classes”. In October 1985 the first commercial release of the language appeared together with the first edition of the book: “The C++ Programming Language” by Bjarne Stroustrup. In November 1997, the ANSI standard for C++ was published. Nowadays, C++ is the most widely used programming language in the world to develop applications. The reasons include its portability, flexibility, code reuse and quality.
15
Argument Passing Notes:
When a function is called, store is set aside for its formal arguments and each formal argument is initialized by its corresponding actual argument. The type of an actual argument is checked against the type of the corresponding formal argument, and all standard and user defined type conversions are performed. There are special rules for passing arrays, a facility for passing unchecked arguments, and a facility for specifying default arguments . The C language was developed mainly by Dennis Ritchie from Bell Labs in the Early ’70s as a programming language used to develop the Unix operating system. Many implementations of the C language followed and only in 1989 was the ANSI standard for C published. In the early ’80s, Bjarne Stroustrup, also from Bell Labs, started development of a language based on C but which included Object Orientated concepts from other languages such as Simula67. This was known as “C with Classes”. In October 1985 the first commercial release of the language appeared together with the first edition of the book: “The C++ Programming Language” by Bjarne Stroustrup. In November 1997, the ANSI standard for C++ was published. Nowadays, C++ is the most widely used programming language in the world to develop applications. The reasons include its portability, flexibility, code reuse and quality.
16
Argument Passing (cont.)
Example: void f(int val, int& ref) { val++; //a local copy of the first actual argument, by value ref++; //the second actual argument, by reference } void main( ) int i = 2; int j = 2; f(i,j); cout<<i=<<endl; cout<<j<<endl; Result: 2 3 The C language was developed mainly by Dennis Ritchie from Bell Labs in the Early ’70s as a programming language used to develop the Unix operating system. Many implementations of the C language followed and only in 1989 was the ANSI standard for C published. In the early ’80s, Bjarne Stroustrup, also from Bell Labs, started development of a language based on C but which included Object Orientated concepts from other languages such as Simula67. This was known as “C with Classes”. In October 1985 the first commercial release of the language appeared together with the first edition of the book: “The C++ Programming Language” by Bjarne Stroustrup. In November 1997, the ANSI standard for C++ was published. Nowadays, C++ is the most widely used programming language in the world to develop applications. The reasons include its portability, flexibility, code reuse and quality.
17
Argument Passing(cont.)
Const argument: Const argument tells readers that the value of an object pointed to by that argument is not changed by the function. For example: void f(const Large& arg) { / / the value of "arg" cannot be changed } 1. int strlen(const char*);//number of characters in a C-style string 2. char* strcpy(char* to,const char* from);//copy a C-style string 3. int strcmp(const char*, const char*);//compare C-style strings The C language was developed mainly by Dennis Ritchie from Bell Labs in the Early ’70s as a programming language used to develop the Unix operating system. Many implementations of the C language followed and only in 1989 was the ANSI standard for C published. In the early ’80s, Bjarne Stroustrup, also from Bell Labs, started development of a language based on C but which included Object Orientated concepts from other languages such as Simula67. This was known as “C with Classes”. In October 1985 the first commercial release of the language appeared together with the first edition of the book: “The C++ Programming Language” by Bjarne Stroustrup. In November 1997, the ANSI standard for C++ was published. Nowadays, C++ is the most widely used programming language in the world to develop applications. The reasons include its portability, flexibility, code reuse and quality.
18
Argument Passing(cont.)
Note: A literal, a constant, and an argument that requires conversion can be passed as a const& argument, but not as a non const argument. float fsqrt(const float&) ; void g(double d) { float r = fsqrt(2.0f);//pass ref to temp holding 2.0f r = fsqrt(r);//pass ref to r r = fsqrt(d);//pass ref to temp holding float(d) } The C language was developed mainly by Dennis Ritchie from Bell Labs in the Early ’70s as a programming language used to develop the Unix operating system. Many implementations of the C language followed and only in 1989 was the ANSI standard for C published. In the early ’80s, Bjarne Stroustrup, also from Bell Labs, started development of a language based on C but which included Object Orientated concepts from other languages such as Simula67. This was known as “C with Classes”. In October 1985 the first commercial release of the language appeared together with the first edition of the book: “The C++ Programming Language” by Bjarne Stroustrup. In November 1997, the ANSI standard for C++ was published. Nowadays, C++ is the most widely used programming language in the world to develop applications. The reasons include its portability, flexibility, code reuse and quality.
19
Argument Passing(cont.)
void update(float& i) ; // not const argument void g(double d, float r) { update(2.0f) ; // error: const argument update(r) ; // pass ref to r update(d) ; // error: type conversion required } Call by value Copy of data passed to function Changes to copy do not change original Call by reference Function can directly access data Changes affect original The C language was developed mainly by Dennis Ritchie from Bell Labs in the Early ’70s as a programming language used to develop the Unix operating system. Many implementations of the C language followed and only in 1989 was the ANSI standard for C published. In the early ’80s, Bjarne Stroustrup, also from Bell Labs, started development of a language based on C but which included Object Orientated concepts from other languages such as Simula67. This was known as “C with Classes”. In October 1985 the first commercial release of the language appeared together with the first edition of the book: “The C++ Programming Language” by Bjarne Stroustrup. In November 1997, the ANSI standard for C++ was published. Nowadays, C++ is the most widely used programming language in the world to develop applications. The reasons include its portability, flexibility, code reuse and quality.
20
Example1 #include<iostream.h> void Swap(int a, int b);
int main( ) { int x=5, y=10; cout<<"x="<<x<<" y="<<y<<endl; Swap(x,y); return 0; } void Swap(int a, int b) int t; t=a; a=b; b=t; Results: x= y=10 The C language was developed mainly by Dennis Ritchie from Bell Labs in the Early ’70s as a programming language used to develop the Unix operating system. Many implementations of the C language followed and only in 1989 was the ANSI standard for C published. In the early ’80s, Bjarne Stroustrup, also from Bell Labs, started development of a language based on C but which included Object Orientated concepts from other languages such as Simula67. This was known as “C with Classes”. In October 1985 the first commercial release of the language appeared together with the first edition of the book: “The C++ Programming Language” by Bjarne Stroustrup. In November 1997, the ANSI standard for C++ was published. Nowadays, C++ is the most widely used programming language in the world to develop applications. The reasons include its portability, flexibility, code reuse and quality.
21
Example2 #include<iostream.h> void Swap(int& a, int& b);
int main( ) { int x=5, y=10; cout<<"x="<<x<<" y="<<y<<endl; Swap(x,y); //Swap(5,10); //error not const reference return 0; } void Swap(int& a, int& b) { int t; t=a; a=b; b=t; } The C language was developed mainly by Dennis Ritchie from Bell Labs in the Early ’70s as a programming language used to develop the Unix operating system. Many implementations of the C language followed and only in 1989 was the ANSI standard for C published. In the early ’80s, Bjarne Stroustrup, also from Bell Labs, started development of a language based on C but which included Object Orientated concepts from other languages such as Simula67. This was known as “C with Classes”. In October 1985 the first commercial release of the language appeared together with the first edition of the book: “The C++ Programming Language” by Bjarne Stroustrup. In November 1997, the ANSI standard for C++ was published. Nowadays, C++ is the most widely used programming language in the world to develop applications. The reasons include its portability, flexibility, code reuse and quality. Results: x= y=10 x= y=5
22
Argument Passing(cont.)
Array Arguments: If an array is used as a function argument, a pointer to its initial element is passed. For example: int strlen(const char*) ; void f() { char v[] = "an array"; int i = strlen(v) ; int j = strlen("Nicholas") ; } The C language was developed mainly by Dennis Ritchie from Bell Labs in the Early ’70s as a programming language used to develop the Unix operating system. Many implementations of the C language followed and only in 1989 was the ANSI standard for C published. In the early ’80s, Bjarne Stroustrup, also from Bell Labs, started development of a language based on C but which included Object Orientated concepts from other languages such as Simula67. This was known as “C with Classes”. In October 1985 the first commercial release of the language appeared together with the first edition of the book: “The C++ Programming Language” by Bjarne Stroustrup. In November 1997, the ANSI standard for C++ was published. Nowadays, C++ is the most widely used programming language in the world to develop applications. The reasons include its portability, flexibility, code reuse and quality.
23
Value return A value must be returned from a function that is not declared void (however, main()is special; see§3.2). Conversely, a value cannot be returned from a void function. For example: int f1( ) { } / / error: no value returned void f2( ) { } / / ok int f3( ) { return 1; } / / ok void f4( ) { return 1; } / / error: return value in void function int f5( ) { return; } / / error: return value missing void f6( ) { return; } / / ok The C language was developed mainly by Dennis Ritchie from Bell Labs in the Early ’70s as a programming language used to develop the Unix operating system. Many implementations of the C language followed and only in 1989 was the ANSI standard for C published. In the early ’80s, Bjarne Stroustrup, also from Bell Labs, started development of a language based on C but which included Object Orientated concepts from other languages such as Simula67. This was known as “C with Classes”. In October 1985 the first commercial release of the language appeared together with the first edition of the book: “The C++ Programming Language” by Bjarne Stroustrup. In November 1997, the ANSI standard for C++ was published. Nowadays, C++ is the most widely used programming language in the world to develop applications. The reasons include its portability, flexibility, code reuse and quality.
24
Value return (cont.) Recursion call
Function calls itself directly or indirectly. Example: Get the resolution of n! n!=n*(n-1)! (n>1) n!= (n=1) long fac(int n) { long f; if (n>1) f=n*fac(n-1); else f=1; return f; } The C language was developed mainly by Dennis Ritchie from Bell Labs in the Early ’70s as a programming language used to develop the Unix operating system. Many implementations of the C language followed and only in 1989 was the ANSI standard for C published. In the early ’80s, Bjarne Stroustrup, also from Bell Labs, started development of a language based on C but which included Object Orientated concepts from other languages such as Simula67. This was known as “C with Classes”. In October 1985 the first commercial release of the language appeared together with the first edition of the book: “The C++ Programming Language” by Bjarne Stroustrup. In November 1997, the ANSI standard for C++ was published. Nowadays, C++ is the most widely used programming language in the world to develop applications. The reasons include its portability, flexibility, code reuse and quality.
25
Enter a positive integer:5 5!=120
void main( ) { long fac(int n); int n; long y; cout<<"Enter a positive integer:"; cin>>n; y=fac(n); cout<<n<<"!="<<y<<endl; } Results: Enter a positive integer:5 5!=120 The C language was developed mainly by Dennis Ritchie from Bell Labs in the Early ’70s as a programming language used to develop the Unix operating system. Many implementations of the C language followed and only in 1989 was the ANSI standard for C published. In the early ’80s, Bjarne Stroustrup, also from Bell Labs, started development of a language based on C but which included Object Orientated concepts from other languages such as Simula67. This was known as “C with Classes”. In October 1985 the first commercial release of the language appeared together with the first edition of the book: “The C++ Programming Language” by Bjarne Stroustrup. In November 1997, the ANSI standard for C++ was published. Nowadays, C++ is the most widely used programming language in the world to develop applications. The reasons include its portability, flexibility, code reuse and quality.
26
Overloaded Function Names
Using the same name for operations on different types is called overloading. For example: int abs(int); long abs(long); double abs(double); void main( ) { abs(-10); // abs(int); abs( ); //abs(long); abs(-12.23); // abs(double); } The C language was developed mainly by Dennis Ritchie from Bell Labs in the Early ’70s as a programming language used to develop the Unix operating system. Many implementations of the C language followed and only in 1989 was the ANSI standard for C published. In the early ’80s, Bjarne Stroustrup, also from Bell Labs, started development of a language based on C but which included Object Orientated concepts from other languages such as Simula67. This was known as “C with Classes”. In October 1985 the first commercial release of the language appeared together with the first edition of the book: “The C++ Programming Language” by Bjarne Stroustrup. In November 1997, the ANSI standard for C++ was published. Nowadays, C++ is the most widely used programming language in the world to develop applications. The reasons include its portability, flexibility, code reuse and quality.
27
Some important criteria:
[1] Exact match; that is, match using no or only trivial conversions ; (for example, array name to pointer, function name to pointer to function, and T to const T) [2] Match using promotions; that is, integral promotions (bool to int, char to int, short to int, and their unsigned counterparts ), float to double, and double to long double [3] Match using standard conversions (for example, int to double, double to int, Derived* to Base* (§12.2), T* to void* (§5.6), int to unsigned int; §C.6) [4] Match using user defined conversions (§11.4) [5] Match using the ellipsis ... in a function declaration (§7.6) The C language was developed mainly by Dennis Ritchie from Bell Labs in the Early ’70s as a programming language used to develop the Unix operating system. Many implementations of the C language followed and only in 1989 was the ANSI standard for C published. In the early ’80s, Bjarne Stroustrup, also from Bell Labs, started development of a language based on C but which included Object Orientated concepts from other languages such as Simula67. This was known as “C with Classes”. In October 1985 the first commercial release of the language appeared together with the first edition of the book: “The C++ Programming Language” by Bjarne Stroustrup. In November 1997, the ANSI standard for C++ was published. Nowadays, C++ is the most widely used programming language in the world to develop applications. The reasons include its portability, flexibility, code reuse and quality.
28
void print(const char*) ; void print(long) ; void print(char) ;
void print(int) ; void print(const char*) ; void print(long) ; void print(char) ; void h(char c, int i, short s, float f) { print(c) ; //exact match: invoke print(char) print(i) ; //exact match: invoke print(int) print(s) ;//integral promotion: invoke print(int) print(´a´);//exact match: invoke print(char) print(49) ;//exact match: invoke print(int) print("a");//exact match: invoke print(const char*) } The C language was developed mainly by Dennis Ritchie from Bell Labs in the Early ’70s as a programming language used to develop the Unix operating system. Many implementations of the C language followed and only in 1989 was the ANSI standard for C published. In the early ’80s, Bjarne Stroustrup, also from Bell Labs, started development of a language based on C but which included Object Orientated concepts from other languages such as Simula67. This was known as “C with Classes”. In October 1985 the first commercial release of the language appeared together with the first edition of the book: “The C++ Programming Language” by Bjarne Stroustrup. In November 1997, the ANSI standard for C++ was published. Nowadays, C++ is the most widely used programming language in the world to develop applications. The reasons include its portability, flexibility, code reuse and quality.
29
void print_ char(char) ; void print_ string(const char*) ;
Without overloading, we must define several functions with different names: (C-style) void print_ int(int) ; void print_ char(char) ; void print_ string(const char*) ; void g(int i, char c, const char* p, double d) { print_ int(i) ; / / ok print_ char(c) ; / / ok print_ string(p) ; / / ok } This can be tedious, defeats attempts to do generic programming. The C language was developed mainly by Dennis Ritchie from Bell Labs in the Early ’70s as a programming language used to develop the Unix operating system. Many implementations of the C language followed and only in 1989 was the ANSI standard for C published. In the early ’80s, Bjarne Stroustrup, also from Bell Labs, started development of a language based on C but which included Object Orientated concepts from other languages such as Simula67. This was known as “C with Classes”. In October 1985 the first commercial release of the language appeared together with the first edition of the book: “The C++ Programming Language” by Bjarne Stroustrup. In November 1997, the ANSI standard for C++ was published. Nowadays, C++ is the most widely used programming language in the world to develop applications. The reasons include its portability, flexibility, code reuse and quality.
30
Overloading and Return Type
Return types are not considered in overload resolution. float sqrt(float) ; double sqrt(double) ; void f(double da, float fla) { float fl = sqrt(da) ; / / call sqrt(double) double d = sqrt(da) ; / / call sqrt(double) fl = sqrt(fla) ; / / call sqrt(float) d = sqrt(fla) ; / / call sqrt(float) } The C language was developed mainly by Dennis Ritchie from Bell Labs in the Early ’70s as a programming language used to develop the Unix operating system. Many implementations of the C language followed and only in 1989 was the ANSI standard for C published. In the early ’80s, Bjarne Stroustrup, also from Bell Labs, started development of a language based on C but which included Object Orientated concepts from other languages such as Simula67. This was known as “C with Classes”. In October 1985 the first commercial release of the language appeared together with the first edition of the book: “The C++ Programming Language” by Bjarne Stroustrup. In November 1997, the ANSI standard for C++ was published. Nowadays, C++ is the most widely used programming language in the world to develop applications. The reasons include its portability, flexibility, code reuse and quality.
31
Overloading and Scopes
Functions declared in different non-namespace scopes do not overload. void f(int) ; void g() { void f(double) ; f(1) ; / / call f(double) } Clearly, f(int) would have been the best match for f(1), but only f(double) is in scope. The C language was developed mainly by Dennis Ritchie from Bell Labs in the Early ’70s as a programming language used to develop the Unix operating system. Many implementations of the C language followed and only in 1989 was the ANSI standard for C published. In the early ’80s, Bjarne Stroustrup, also from Bell Labs, started development of a language based on C but which included Object Orientated concepts from other languages such as Simula67. This was known as “C with Classes”. In October 1985 the first commercial release of the language appeared together with the first edition of the book: “The C++ Programming Language” by Bjarne Stroustrup. In November 1997, the ANSI standard for C++ was published. Nowadays, C++ is the most widely used programming language in the world to develop applications. The reasons include its portability, flexibility, code reuse and quality.
32
#include<iostream> using namespace std; struct complex {
Example: #include<iostream> using namespace std; struct complex { double real; double imaginary; }; int main() int m, n; double x, y; complex c1, c2, c3; int add(int m, int n); double add(double x, double y); complex add(complex c1, complex c2); The C language was developed mainly by Dennis Ritchie from Bell Labs in the Early ’70s as a programming language used to develop the Unix operating system. Many implementations of the C language followed and only in 1989 was the ANSI standard for C published. In the early ’80s, Bjarne Stroustrup, also from Bell Labs, started development of a language based on C but which included Object Orientated concepts from other languages such as Simula67. This was known as “C with Classes”. In October 1985 the first commercial release of the language appeared together with the first edition of the book: “The C++ Programming Language” by Bjarne Stroustrup. In November 1997, the ANSI standard for C++ was published. Nowadays, C++ is the most widely used programming language in the world to develop applications. The reasons include its portability, flexibility, code reuse and quality.
33
cout<<"Enter two integer: "; cin>>m>>n;
cout<<"integer <<m<<'+'<<n<<"="<<add(m,n)<<endl; cout<<"Enter two real number: "; cin>>x>>y; cout<<"real number "<<x<<'+'<<y<<"= "<<add(x,y) <<endl; cout<<"Enter the first complex number: "; cin>>c1.real>>c1.imaginary; cout<<"Enter the second complex number: "; cin>>c2.real>>c2.imaginary; c3=add(c1,c2); The C language was developed mainly by Dennis Ritchie from Bell Labs in the Early ’70s as a programming language used to develop the Unix operating system. Many implementations of the C language followed and only in 1989 was the ANSI standard for C published. In the early ’80s, Bjarne Stroustrup, also from Bell Labs, started development of a language based on C but which included Object Orientated concepts from other languages such as Simula67. This was known as “C with Classes”. In October 1985 the first commercial release of the language appeared together with the first edition of the book: “The C++ Programming Language” by Bjarne Stroustrup. In November 1997, the ANSI standard for C++ was published. Nowadays, C++ is the most widely used programming language in the world to develop applications. The reasons include its portability, flexibility, code reuse and quality.
34
cout<<"complex number (" <<c1.real<< ','
<< c1.imaginary <<")+("<<c2.real<<',' <<c2.imaginary<<")=("<<c3.real<<',' <<c3.imaginary<<")\n"; } int add(int m, int n) { return m+n; } double add(double x, double y) { return x+y; } complex add(complex c1, complex c2) { complex c; c.real=c1.real+c2.real; c.imaginary=c1.imaginary+c2.imaginary; return c; The C language was developed mainly by Dennis Ritchie from Bell Labs in the Early ’70s as a programming language used to develop the Unix operating system. Many implementations of the C language followed and only in 1989 was the ANSI standard for C published. In the early ’80s, Bjarne Stroustrup, also from Bell Labs, started development of a language based on C but which included Object Orientated concepts from other languages such as Simula67. This was known as “C with Classes”. In October 1985 the first commercial release of the language appeared together with the first edition of the book: “The C++ Programming Language” by Bjarne Stroustrup. In November 1997, the ANSI standard for C++ was published. Nowadays, C++ is the most widely used programming language in the world to develop applications. The reasons include its portability, flexibility, code reuse and quality.
35
Results: Enter two integer: 3 5 integer 3+5=8
Enter two real number: real number = 8.1 Enter the first complex number: Enter the second complex number: complex number (12.3,45.6)+(56.7,67.8)= (69,113.4) Note: Functions with same name and different parameters(type or number) Overloaded. These functions should perform similar tasks The C language was developed mainly by Dennis Ritchie from Bell Labs in the Early ’70s as a programming language used to develop the Unix operating system. Many implementations of the C language followed and only in 1989 was the ANSI standard for C published. In the early ’80s, Bjarne Stroustrup, also from Bell Labs, started development of a language based on C but which included Object Orientated concepts from other languages such as Simula67. This was known as “C with Classes”. In October 1985 the first commercial release of the language appeared together with the first edition of the book: “The C++ Programming Language” by Bjarne Stroustrup. In November 1997, the ANSI standard for C++ was published. Nowadays, C++ is the most widely used programming language in the world to develop applications. The reasons include its portability, flexibility, code reuse and quality.
36
Default Arguments If function parameter omitted, gets default value
Can be constants, global variables If not enough parameters specified, rightmost go to their defaults For example: int add(int x,int y=5,int z=6); //ok int add(int x=1,int y=5,int z); //error int add(int x=1,int y, int z=6); //error The C language was developed mainly by Dennis Ritchie from Bell Labs in the Early ’70s as a programming language used to develop the Unix operating system. Many implementations of the C language followed and only in 1989 was the ANSI standard for C published. In the early ’80s, Bjarne Stroustrup, also from Bell Labs, started development of a language based on C but which included Object Orientated concepts from other languages such as Simula67. This was known as “C with Classes”. In October 1985 the first commercial release of the language appeared together with the first edition of the book: “The C++ Programming Language” by Bjarne Stroustrup. In November 1997, the ANSI standard for C++ was published. Nowadays, C++ is the most widely used programming language in the world to develop applications. The reasons include its portability, flexibility, code reuse and quality.
37
Example 1: int add(int x=5,int y=6) { return x+y; } void main(void)
#include <iostream.h> int add(int x=5,int y=6) { return x+y; } void main(void) add(10,20); //10+20 add(10); //10+6 add(); //5+6 The C language was developed mainly by Dennis Ritchie from Bell Labs in the Early ’70s as a programming language used to develop the Unix operating system. Many implementations of the C language followed and only in 1989 was the ANSI standard for C published. In the early ’80s, Bjarne Stroustrup, also from Bell Labs, started development of a language based on C but which included Object Orientated concepts from other languages such as Simula67. This was known as “C with Classes”. In October 1985 the first commercial release of the language appeared together with the first edition of the book: “The C++ Programming Language” by Bjarne Stroustrup. In November 1997, the ANSI standard for C++ was published. Nowadays, C++ is the most widely used programming language in the world to develop applications. The reasons include its portability, flexibility, code reuse and quality.
38
Default Arguments(cont.)
A default argument cannot be repeated or changed in a subsequent declaration in the same scope. For example: int add(int x=1,int y=2); int add(int =1, int =2); //error int add(int x=3,int y=4); //error void main(void) { int add(int x=3,int y=4); add(); //3+4 Local function ::add( ); //1+2 Global function } The C language was developed mainly by Dennis Ritchie from Bell Labs in the Early ’70s as a programming language used to develop the Unix operating system. Many implementations of the C language followed and only in 1989 was the ANSI standard for C published. In the early ’80s, Bjarne Stroustrup, also from Bell Labs, started development of a language based on C but which included Object Orientated concepts from other languages such as Simula67. This was known as “C with Classes”. In October 1985 the first commercial release of the language appeared together with the first edition of the book: “The C++ Programming Language” by Bjarne Stroustrup. In November 1997, the ANSI standard for C++ was published. Nowadays, C++ is the most widely used programming language in the world to develop applications. The reasons include its portability, flexibility, code reuse and quality.
39
Pointer to Function There are only two things one can do to a function: call it and take its address. Conventionally, we use the following function: int add(int, int); void error(char *); char *copy(char *, char *); The last we call the pointer function, which can return lots of data to the function, but the first like can just return a data.
40
Example 1: #include <iostream.h> #include <math.h>
double x[2]; void main() { double *fc(double *); double f[3],*r; cin>>f[0]>>f[1]>>f[2]; r=fc(f); if (r) cout<<"x1="<<r[0]<<'\t'<<"x2="<<r[1]<<endl; else cout<<"No resolution!"<<endl; }
41
delta=f[1]*f[1]-4*f[0]*f[2]; if(delta<0) return 0;
double *fc(double *f) { double delta=0; delta=f[1]*f[1]-4*f[0]*f[2]; if(delta<0) return 0; delta=sqrt(delta); x[0]=(-f[1]+delta)/(2*f[0]); x[1]=(-f[1]-delta)/(2*f[0]); return x; } Result: 1 3 2 x1=-1 x2=-2
42
Pointer to Function(cont.)
Different from pointer function, pointer to function also called function pointer that is declared as: type (*function name)(<arg_list>) Such as: int (*fun)(int, int); void (*efct)(string); The pointer obtained by taking the address of a function can then be used to call the function.
43
Example 2: #include <iostream> using namespace std;
int add(int, int); int sub(int, int); int mult(int,int); int dev(int,int); int (*fun)(int, int);
44
int add(int a, int b) { return a+b; } int sub(int a, int b) return a-b; int mult(int a,int b) return a*b; int dev(int a,int b) return a/b;
45
void main() { int x,y; char c; cin>>x>>c>>y; switch(c) case '+': fun=&add; break; case '-': fun=⊂ case '*': fun=&mult;
46
case '/': fun=&dev; break; default: cout<<"error input"<<endl; } cout<<x<<c<<y<<"="<<(*fun)(x,y)<<endl; The compiler will discover that fun is a pointer and call the function pointed to. That is, dereferencing of a pointer to function using * is optional. Similarly, using & to get the address of a function is optional: fun=&dev can be written as : fun=dev (*fun)(x,y) can be written as : fun(x,y)
47
Example 3: #include <iostream> using namespace std;
void (*menfun)(); void f1() {cout<<"good!\n";} void f2() {cout<<"better!\n";} void f3() {cout<<"best!\n";}
48
void main() { int choice; cin>>choice; switch(choice) case 1: menfun=f1; break; case 2: menfun=f2; break; case 3: menfun=f3; break; default: cout<<"error input\n"; } menfun(); Result: 2 better!
49
Pointer to Function(cont.)
Important notes: A function must be called through a pointer to function with exactly the right argument and return types. There is no implicit conversion of argument or return types when pointers to functions are assigned or initialized. for example: void f(int) ; int f(char) ; void (*pf1)(int) = &f; / / void f(int) int (*pf2)(char) = &f; / / int f(char) void (*pf3)(char) = &f; / / error: no void f(char)
50
Advice If you want the function to modify its arguments, use pointers and reference return instead; Use const reference arguments when you need to minimize copying of arguments; Use overloading when functions perform conceptually the same task on different types. When you pass any value to a function by value, the compiler makes a local copy of the variable inside the function. However, for user defined objects (Classes etc.) this is not possible because there are problems when the compiler performs a bitwise-copy. Therefore, you need to define your own copy function. The reason it is also a constructor is because you are creating a new object. Secondly, you use references because you construct a new object from an existing one. The details is however outside of the scope and I refer you to the source for further reading.
51
The End! Thank you!
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.