Programming in c++ Functions Dale/eems/Headington
1 Functions
Programming in C++ Chapter 7 Topics Writing a Program Using Functional Decomposition Writing a Void Function for a Task s Using Function Arguments and Parameters correctly Differences between Value Parameters and Reference Parameters s Using Local Variables in a Function Function Preconditions and Postconditions
2 Chapter 7 Topics ❖Writing a Program Using Functional Decomposition ❖Writing a Void Function for a Task ❖Using Function Arguments and Parameters correctly ❖Differences between Value Parameters and Reference Parameters ❖Using Local Variables in a Function ❖Function Preconditions and Postconditions
Programming in C++ Functions oevery C++ program must have a function called main program execution always begins with function main o any other functions are subprograms and must be called
3 Functions ❖every C++ program must have a function called main ❖program execution always begins with function main ❖any other functions are subprograms and must be called
Programming in C++ Function Call(Invocation One function calls another by using the name of the called function next to( enclosing an argument list A function call temporarily transfers control from the calling function to the called function
4 Function Call (Invocation) One function calls another by using the name of the called function next to ( ) enclosing an argument list. A function call temporarily transfers control from the calling function to the called function
Programming in C++ Function Call Syntax FunctionName( Argument List The argument list is a way for functions to communicate with each other by passing information y The argument list can contain 0, 1, or more arguments, separated by commas, depending on the function
5 FunctionName ( Argument List ); ➢ The argument list is a way for functions to communicate with each other by passing information. ➢ The argument list can contain 0, 1, or more arguments, separated by commas, depending on the function. Function Call Syntax
Programming in C++ Function Call Syntax( Cont) Argument List Expression, Expression >When a function call is executed the arguments are passed to the parameters according to their positions, left to right, and control is then transferred to the first executable statement in the function body. )When the last statement in the function has executed control returns to the point from which the function was called
6 Function Call Syntax(Cont.) Expression, Expression… Argument List ➢When a function call is executed, the arguments are passed to the parameters according to their positions, left to right, and control is then transferred to the first executable statement in the function body. ➢When the last statement in the function has executed, control returns to the point from which the function was called
Programming in C++ TWo Parts of Function Definition int Cube( int n) heading body return nxnxn
7 Two Parts of Function Definition int Cube ( int n ) heading { body return n * n * n ; }
Programming in C++ What is in a heading? type of value returned name of parameter list int Cube int n
8 What is in a heading? int Cube ( int n ) type of value returned name of function parameter list
Programming in C++ What is in a prototype? A prototype looks like a heading but must end with a semicolon; and its parameter list just needs to contain the type of each parameter. int Cube( int ); prototype
9 What is in a prototype? A prototype looks like a heading but must end with a semicolon; and its parameter list just needs to contain the type of each parameter. int Cube( int ); // prototype
Programming in C++ The difference between Function Definition and Function Prototype )Function Definition: A function declaration that includes the body of the function >Function Prototype: A function declaration that without the body of the function Note that all definitions are declarations but not all declarations are definitions The rule throughout C++ is that you can declare an item as many times as you wish, but you can define it only once 10
10 The difference between Function Definition and Function Prototype ➢Function Definition: A function declaration that includes the body of the function. ➢Function Prototype: A function declaration that without the body of the function. • Note that all definitions are declarations, but not all declarations are definitions. • The rule throughout C++ is that you can declare an item as many times as you wish, but you can define it only once