Functions_C++

Functions_C++ - Modular Program with Functions User-Defined...

Info iconThis preview shows pages 1–11. Sign up to view the full content.

View Full Document Right Arrow Icon
Modular Program with Functions
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
User-Defined Functions Void functions : do not have a return type Value-returning functions : have a data type To use these functions you need to: Know the name of the function Know the number of parameters, if any Know the data type of each parameter Know the data type of the value computed by the function, called the type of the function Include the correct header file (if necessary)
Background image of page 2
Value-Returning Functions Because the value returned by a value-returning function is unique, we must either do one of the following: Save the value for further calculation X = fun1 (A, b); Use the value in some calculation X = fun1 (A,b) * D + sqrt(A); Print the value cout << “Result is: “ << fun1 (A,b); A value-returning function is used in an assignment or in an output statement
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Value-Returning Functions (continued) Properties that form the function definition: 1. Name of the function 2. Number of parameters 3. Data type of each parameter 4. Type of the function 5. Code required to accomplish the task (the body of the function)
Background image of page 4
Value-Returning Functions (continued) Heading : first four properties above Formal Parameter : variable declared in the heading Actual Parameter : variable or expression listed in a call to a function Function Prototype : Introduction of a function Function Call: Using a function
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
// Function Prototype char Grade_it (float ); or char Grade_it (float Average) ; // Function Definition char Grade_it (float Average) // Function header (heading) { char Grade ; // Code segment if (Average >= 70) Grade = ‘P’; else Grade = ‘F’ ; return (Grade) ; // Returned value } // Function call float avg ; char Letter_Grade ; cin >> avg ; Letter_Grade = Grade_it (avg); cout << “Your average is: “ << avg << endl ; out <<“Your letter grade is : “<< Letter_grade << endl ;
Background image of page 6
Value-Returning Functions (continued) Syntax: functionType : type of the value returned by the function Also called the data type
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Syntax The syntax of the formal parameter list is: The syntax for a function call is: The syntax for the actual parameter list is:
Background image of page 8
Functions The formal parameter list can be empty If the formal parameter list is empty Parentheses are still needed Function heading of the value-returning function takes either of the following forms: functionType functionName() In a function call the actual parameter is empty A call to a value-returning function with an empty formal parameter list is: functionName()
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Value-Returning Functions To call a value-returning function: Use its name, with the actual parameters (if any) in parentheses There is a one-to-one correspondence between actual and formal parameters
Background image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 34

Functions_C++ - Modular Program with Functions User-Defined...

This preview shows document pages 1 - 11. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online