1
Introduction to programming in C++ for engineers: 13. Functions I
Programming in C++Programming in C++
13. Functions I13...
2
Introduction to programming in C++ for engineers: 13. Functions I
IntroductionIntroduction
Although control structures e...
3
Introduction to programming in C++ for engineers: 13. Functions I
Defining a functionDefining a function
In general a fu...
4
Introduction to programming in C++ for engineers: 13. Functions I
Defining a functionDefining a function
int _____ retur...
5
Introduction to programming in C++ for engineers: 13. Functions I
Calling a functionCalling a function
A function is cal...
6
Introduction to programming in C++ for engineers: 13. Functions I
Return typeReturn type
The return statement causes a f...
7
Introduction to programming in C++ for engineers: 13. Functions I
Function prototypesFunction prototypes
The function ca...
8
Introduction to programming in C++ for engineers: 13. Functions I
Functions cannot declare functions!!!Functions cannot ...
9
Introduction to programming in C++ for engineers: 13. Functions I
Unused argumentsUnused arguments
There is no requireme...
10
Introduction to programming in C++ for engineers: 13. Functions I
Default argumentsDefault arguments
A function declara...
11
Introduction to programming in C++ for engineers: 13. Functions I
Ignoring the return valueIgnoring the return value
Th...
12
Introduction to programming in C++ for engineers: 13. Functions I
SummarySummary
! A function definition implements the...
Upcoming SlideShare
Loading in...5
×

C++

376

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
376
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
8
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "C++"

  1. 1. 1 Introduction to programming in C++ for engineers: 13. Functions I Programming in C++Programming in C++ 13. Functions I13. Functions I ! Introduction ! Defining a function ! Calling a function ! Return type ! Function prototypes ! Functions cannot declare functions!!! ! Unused arguments ! Default arguments ! Ignoring the return value ! Summary
  2. 2. 2 Introduction to programming in C++ for engineers: 13. Functions I IntroductionIntroduction Although control structures enable us to write very powerful programs they do not give us a means of controlling this power nor do they encourage reusable code. The introduction of functions is an essential first step in the writing of modular, maintanable and reusable code. In particular testing can be carried out on each function in isolation, rather than on the whole program. This is important as the number of different pathways through even a modest program is often very large indeed. Functions provide a way of encapsulating relatively self-contained segments of code. A function typically carries out some well-defined action.
  3. 3. 3 Introduction to programming in C++ for engineers: 13. Functions I Defining a functionDefining a function In general a function definition takes the form: return_type function_name(type argument, ... , type argument) { // function body } factorial function is a very instructive example. int factorial(int n) // calculates n! { int result = 1; do result *= n--; while (n>1); return result; }
  4. 4. 4 Introduction to programming in C++ for engineers: 13. Functions I Defining a functionDefining a function int _____ return type factorial(int n) _____ argument type and formal argument { // code implementing the function return result _____ value returned } _____ no semi-colon There is no terminating column in function!!! The first line sometimes known as the function header declares that the function returns type int and defines the name of the function to be factorial, which is the identifier used to invoke this particular function. The identifier n is known as formal argument. A function can have any number of formal arguments separated by commas. The set of braces { } contains what is called as a function body. This body contains a return statement which terminates the function.
  5. 5. 5 Introduction to programming in C++ for engineers: 13. Functions I Calling a functionCalling a function A function is called (invoked or executed) by including its name together with appropriate arguments within parenthesis. These arguments are called actual arguments. The identifiers used in both cases may differ since the actual arguments are copied to the formal arguments. The scope of the the formal arguments is limited to the function body and changes made to these arguments are not propagated back to the calling program. This process of copying the value of the actual argument to the formal is known as pass by value. In C++ function can also use pass by reference, but pass by value is the usual technique. Pass by value encourages more modular, safer code and help to prevent unexpected changes in the values of variables.
  6. 6. 6 Introduction to programming in C++ for engineers: 13. Functions I Return typeReturn type The return statement causes a function to terminate but in many cases this statement also causes a value to be passed back to the calling environment. The type returned by a function must agree with that specified in the function header. There can be no requirement for a function to return a value in which a special type called void should be used to specify the return type. void print_text(void) { cout << “Hello!!!n”; return; // This statement can be omitted!!! } If no function return type is specified then it is taken to be int. It is a good idea to specify return type. Even if a function takes no arguments the calling program must use ().
  7. 7. 7 Introduction to programming in C++ for engineers: 13. Functions I Function prototypesFunction prototypes The function can be declared without specifying how it is implemented. int factorial(int n); Such a statement is called function prototype. Unlike a function definition a function prototype must end with a semi-colon and has no function body. A function with an empty body is not a prototype!!! Any argument names given in the prototype are ignored by the compiler, but can be useful documentation technique. The code implementing a function may be compiled independently to the code using it. Such prototypes are often collected together in a header file and since a function is only known to the compiler after it has been declared. In C++ prototypes have a fundamental role to play in defining classes and therefore objects.
  8. 8. 8 Introduction to programming in C++ for engineers: 13. Functions I Functions cannot declare functions!!!Functions cannot declare functions!!! Declaring functions within other functions is not available in C++. Since every program must have a function called main() and a function cannot be defined inside another function the general structure of a program is a list of function definitions. The relationship between the function look like a tree. For some problems functions are a very effective way of controlling complexity. In really complicated problems related functions can be encapsulated. We do not start from this feature because: - there is no point in attacking a small problem with quite complicated technique, - classes are partly built from functions - we need to know them quite good before introducing object-oriented techniques.
  9. 9. 9 Introduction to programming in C++ for engineers: 13. Functions I Unused argumentsUnused arguments There is no requirement for all of the formal arguments in a function declaration to be used in which case an identifier does not have to be specified, but the function cannot be called without an argument, even though the argument is not used. void print(int) { cout << “Testn”; } print(1); // OK print(); // WRONG! Incorrect number of param. Unused arguments may serve to reserve a place in the argument list for future use. An argument type without identifier can also be useful when an argument has been made redundant by a changing function implementation, but the calling has not been updated
  10. 10. 10 Introduction to programming in C++ for engineers: 13. Functions I Default argumentsDefault arguments A function declaration can specify expressions which are to be used as the default values for one or more arguments. double rectangle(double a = 1.0, double b = 2.0) { return a *b } // three possible function calls area1 = rectangle; // 1*2=2 is assigned area2 = rectangle(3.0); // 3*2=6 is assigned area3 = rectangle(4.0, 5.0) // 4*5=20 is assigned The default arguments must be supplied right to left. A default argument cannot be redefined in a subsequent declaration. It is not allowed to give the same default arguments as in a function definition (in a prototype). However a subsequent declaration can introduce one or more additional default arguments. Default arguments must be provided for any arguments omitted in a function call.
  11. 11. 11 Introduction to programming in C++ for engineers: 13. Functions I Ignoring the return valueIgnoring the return value The default arguments do not need to be named in a function prototype, however omitting argument names is not a good idea since they are a valuable method of self-documenting code. There is also the distinction between prototypes and definitions: names cannot be omitted from function definitions. Ignoring the return value The calling function can ignore the value returned by a function. int status = mkdir(“test”); // create directory test if (status) cout < “Failed to create directory.n”; else cout << “Directory created.n”; If we want to ignore the risk that the directory creation can fail we can simply write: mkdir(“test);
  12. 12. 12 Introduction to programming in C++ for engineers: 13. Functions I SummarySummary ! A function definition implements the function. ! Functions are executed (called or invoked) by statements like: x = f(m); or f(m); ! If function is declared to return a value it must do so. ! The order of evaluation of expressions in argument lists is not defined. x = f(++i, (i+5)); // compiler dependent ! A function prototype defines the function interface. ! A function cannot be declared inside another function. ! Functions can have default arguments which are supplied from right to left.
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×