Presentation is loading. Please wait.

Presentation is loading. Please wait.

Functions Venkatesh Ramamoorthy 21-March-2005. Examples #include double y ; … y = sin(45*3.1416/180) ; std::cout << “The sine of 45 degrees is: ” << y.

Similar presentations


Presentation on theme: "Functions Venkatesh Ramamoorthy 21-March-2005. Examples #include double y ; … y = sin(45*3.1416/180) ; std::cout << “The sine of 45 degrees is: ” << y."— Presentation transcript:

1 Functions Venkatesh Ramamoorthy 21-March-2005

2 Examples #include double y ; … y = sin(45*3.1416/180) ; std::cout << “The sine of 45 degrees is: ” << y << std::endl ; …

3 Arguments and Parameters Arguments –Passed to a function by a calling program Parameters –Taken in by a function from its caller program The difference stems from where you view the program –Either from the outside, or –From inside the function

4 Exercise Write a program that takes two inputs: – a positive number x (not necessarily an integer) –A positive number y (again, not necessarily an integer) and displays the following values correct to 4 decimal places: –The sine of x –The cosine of x –The tangent of x –The logarithm of x to the base 10 –The value of x y

5 Function Definitions By function definition, we mean the actual implementation of the function Syntax (return-type) function-name( type-1 parmeter1, type-2parmeter2, …) { return (variable-name) ; } Example double sin(double x) ;

6 Parts of the above syntax Let’s identify the different parts of the above syntax –Return-type –Function-name –Type of each parameter –Each parameter

7 Return type What type of result does a function return to its caller? –The usual, i.e. int, float, double, char, etc –This suggests that the function evaluates and outputs, to its caller, an integer, a float, a double, a character, etc –Example int f1(int a, double b, char c) { int x ; return (x) ; }

8 Function name The same rules for naming variables in C++ apply for naming functions as well –Ensure that you do not use standard or built-in function names (e.g. sin, cos, tan, log, etc) Example int EvaluatePolynomial(int polynomial, int degree) { int x ; <Lines of code to evaluate p of degree d. The value gets stored into x.> return (x) ; }

9 Parameters These are inputs, taken in by the function from the external world The inputs are populated as a comma- separated list Each list is a pair, consisting of the following: –The type of the input parameter variable –The name of the parameter variable

10 Example int abs(int x){…} –x is an input parameter of type int –The name of the function is abs –This function outputs (returns) an integer –This integer is the absolute value of the input x –In the above example, we haven’t amplified on the code to implement the function It turns out that this is a standard math function!

11 More examples int EvaluatePolynomial(int polynomial, int degree) { int x ; <Lines of code to evaluate p of degree d. The value gets stored into x.> return (x) ; } –What does this function do?

12 Exercise Write a function EvaluatePolynomial that does the following: –Takes an integer input x –Evaluates the polynomial ( x 3 – 6x 2 + 11x – 6 ) into a variable y –Returns y as an output to the calling program Write the main program that calls this function –Input an integer x using a suitable prompt –Call EvaluatePolynomial –Display its return value on the screen using a suitable prompt

13 Program Skeleton int EvaluatePolynomial(int x) { int y ; return y ; } int main() { int number, result ; /* Input number */ result = EvaluatePolynomial(number) ; /* Display result */ return (0) ; }


Download ppt "Functions Venkatesh Ramamoorthy 21-March-2005. Examples #include double y ; … y = sin(45*3.1416/180) ; std::cout << “The sine of 45 degrees is: ” << y."

Similar presentations


Ads by Google