Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Programming 1 Functions. Computer Programming 2 Objectives Take a first look at building functions Study how a function is called Investigate.

Similar presentations


Presentation on theme: "Computer Programming 1 Functions. Computer Programming 2 Objectives Take a first look at building functions Study how a function is called Investigate."— Presentation transcript:

1 Computer Programming 1 Functions

2 Computer Programming 2 Objectives Take a first look at building functions Study how a function is called Investigate how a function executes Take a first look at class methods

3 Computer Programming 3 Problem Fahrenheit to Celsius Conversion Two scales used to measure temperature are the Fahrenheit and Celsius scales. A program is needed to convert temperatures in Fahrenheit to the equivalent Celsius temperatures. This program is suppose to be used in several other programs.

4 Computer Programming 4 Algorithm 1. Output a prompt for a Fahrenheit temperature to cout. 2. Input tempFahrenheit from cin. 3. Calculate tempCelsius = (tempFahrenheit – 32.0) / 1.8. 4. Output tempCelsius (and descriptive text) to cout.

5 Computer Programming 5 Functions The previous program cannot be used in other programs Real-world programs may contain thousands (or even millions) of lines of code. How can developers understand, develop, and maintain these monster programs? – Divide the program into chunks known as functions Functions are compact code units that can be used in other programs Written, inspected, and debugged without worrying about the code that surrounds them.

6 Computer Programming 6 Function Definitions Function declaration – function prototype – Gives the “full name” of the function to be called – Function prototype must precede any call or definition of a function - Compiler must know of a function's existence Else a compiler error will occur … undeclared Function definition – function implementation – Contains statements that specify its behavior when it is called. – Function must be defined in order to be called Else a linker error will occur undefined reference to …

7 Computer Programming 7 Syntax Declare a function – Usually, it is written in a specification file with.h extension ReturnType name (parameterDeclarations); Define a function – Usually, it is written in an implementation file with.cpp extension ReturnType name (parameterDeclarations) { //statements }

8 Computer Programming 8 Back to Fahrenheit To Celsius Conversion In a file named fahrCelConv.h const double scl=1.8; const double trn=32.0; double convertFahrToCel(double); In a file named fahrCelConv.cpp #include #include “fahrCelConv.h” using namesapce std; int main() { cout << convertFahrToCel(100.0); return 0; } double convertFahrToCel(double fahr) { return (fahr-trn)/scl; }

9 Computer Programming 9 Back to Fahrenheit To Celsius Conversion (without.h) In a file named fahrCelConv.cpp #include using namesapce std; double convertFahrToCel(double); int main() { cout << convertFahrToCel(100.0); return 0; } double convertFahrToCel(double fahr) { return (fahr-trn)/scl; } Note: Declaration is optional if the definition is given before calling the function

10 Computer Programming 10 Return type and name The return type can be any type – Primitive/basic types – User defined types Use void when the function does not return anything The rules for the names of functions are the same as for the variables Must start with a letter or underscore Use meaningful names

11 Computer Programming 11 Parameters Function variables for which the caller can specify values. Defined between the parentheses of a function’s definition. A function may not have parameters double fahrToCelsius(double tempFahr) { return (tempFahr - 32.0) / 1.8; } void do() { //statements }

12 Computer Programming 12 Arguments When a function is called – Caller can pass it values called arguments – Stored in the function’s parameters. double newTemp = convertFahToCel (212); double fahrToCelsius(double tempFahr) { return (tempFahr - 32.0) / 1.8; } The function then runs using its parameter values.

13 Computer Programming 13 How functions are called Recall: PC (program counter)/EIP (extended instruction pointer) – The register that holds the address of the next instruction to execute Call stack – Area of memory that holds information need for calling functions – A sequence of stack frames Each stack frame is associated with each function call

14 Computer Programming 14 Stack frames Each time a function is called, a new stack frame is created Each time a function is returned (done), the stack frame associated with it is eliminated ESP (extended stack pointer) holds the address of the top of the stack EBP (extended base pointers/frame pointer) is used to reference local variables and parameters inside the current stack frame

15 Computer Programming 15 Putting it all together Calling a function First the calling function pushes the parameters into the stack frame from R to L The EIP is then pushed into the stack, and points to the first instruction in the called function The old value of EBP is pushed into the stack frame Push the top of the stack into EBP Push the local variables into the stack – The local variables are not between EBP and ESP Save the old values of registers that are used by the called function

16 Computer Programming 16 Putting it all together Returning from function Restore the saved registers Release the storage for local variables Restore the old EBP Restore the old EIP (RET) – Go back to the calling function

17 Computer Programming 17 Design Specification of the function – Determines the form of the function heading Return value Name Parameters Algorithm of the function – Determines the content of the function body Input, Output Branching, looping

18 Computer Programming 18 Functions are subprograms A function can be viewed as a sub-program containing statements such as – Variable declarations and definitions – Selections and repetitions – Call to other functions The variables declared inside a function are called local variables – They are not visible to the code surrounding the function

19 Computer Programming 19 Problem Write a program that simulates a calculator with four menus. When the user presses S, the calculator computes the sum 1+…+n, where n is an integer entered by the user. When he presses F, the calculator computes the factorial of n (i.e. 1*2*…*n). If the user presses P, the calculator computes the p(n), Where p is a polynomial function give by p(x)=x 2 -5x 5 +2 + 3*(x 2 -5x 5 ) 6 The number n is displayed as it is in all they other cases.

20 Computer Programming 20 Function inside a class Recall that a class is a new type that has attributes and operations – Attributes are the data that describe an object – Operations are for manipulating objects and their data An operation in a class is a function in that class. It is also known as class method (method) or a member function – A mechanism for building a new operation for a class type Two categories of methods – Class method: defines a message that can be sent to a class; the declaration/definition has static keyword – Instance method: defines a message sent to an object (an instance of a class); does not have static keyword

21 Computer Programming 21 Example on classes and methods A point in two dimension is determined by its x and y positions. Write a program that computes 1. The symmetric point of a point with respect to (0,0) 2. The distance of the point to (0,0) 3. The distance between two points The solution will be provided in the lecture

22 Computer Programming 22 Function inside a class class Person { private: string name; unsigned int id; public: Person (string n=“”, unsigned int x=0) { name = n; id = x; } string getName () { return name; } static string talk() { return "Salam!"; } }; Person bel = Person (“Belaid”, “27”); cout << bel.getName(); Cout << Person::talk(); //bel.talk()


Download ppt "Computer Programming 1 Functions. Computer Programming 2 Objectives Take a first look at building functions Study how a function is called Investigate."

Similar presentations


Ads by Google