Lec02FunctionsParams_6per.pdf

# Lec02FunctionsParams_6per.pdf - EECS 402 Function...

• 5

This preview shows pages 1–3. Sign up to view the full content.

1 The University Of Michigan Andrew M. Morgan Andrew M. Morgan 1 EECS402 Lecture 02 Savitch Ch. 3-4 Functions Value and Reference Parameters EECS EECS 402 EECS 402 Andrew M. Morgan 2 Functions Allows for modular programming Write the function once, call it many times Group similar things together Parameters allow values from calling function to be used within the function May, or may not, return a value to calling function General function template: return_type function_name(formal parameter list) { function code... } EECS EECS EECS EECS 402 EECS 402 Andrew M. Morgan 3 Function Prototype A function prototype "declares a function" Provides user with information about the function. Name of function, what values (and types) need to be passed in, what type to expect the function to return Here is a function prototype for a factorial function The function is called "computeFactorial" Takes in one integer value from the calling function as a parameter Returns an integer value to the calling function Note: The function name should be descriptive of its purpose! int computeFactorial(int num); EECS 402 402 EECS 402 Andrew M. Morgan 4 Function Definition A function definition provides the implementation (in C++) of an algorithm Here is a function definition for computing factorial of a number passed in by the user: Function header matches function prototype (no ; though) int computeFactorial(int num) //Function header { int result = 1; //Value to return.. int i; //Loop variable for (i = 1; i <= num; i++) { result *= i; } return (result); //Returns an integer, as expected } EECS 402 EECS 402 Andrew M. Morgan 5 Function Call A function is "called" when you want to use the algorithm that was implemented in the function Here is how the main function would call computeFactorial: int main(void) { int fact; //Factorial result int val = 5; fact = computeFactorial(val); //function call! cout << "Fact. of " << val << " is: " << fact << endl; return (0); } Fact. of 5 is: 120 EECS EECS 402 EECS 402 Andrew M. Morgan 6 Complete Function Program Here is the complete program layout Note: The order shown is important #include <iostream> //Need for cout. using namespace std; //Function: computeFacorial - Computes //factorial of num and returns it. int computeFactorial(int num); //proto int main(void) { int fact; //Factorial result int val = 5; fact = computeFactorial(val); //call cout << "Fact. of " << val << " is: " << fact << endl; return (0); } int computeFactorial(int num) //header { int result = 1; //Value to return.. int i; //Loop variable for (i = 1; i <= num; i++) { result *= i; } return (result); //Return factorial } Fact. of 5 is: 120

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

2 EECS EECS 402 402 EECS 402 Andrew M. Morgan 7 Multiple Parameters Often, multiple values from the calling function are needed Any number of parameters can be passed in to a function #include <iostream> //Need for cout. using namespace std; //Function: addNums - Computes sum //of 3 ints and returns it int addNums(int a, int b, int c); int main(void) { int num1 = 5; //Integer for test int num2 = 3; //Integer for test int result; //Result of call result = addNums(num1, 6, num2); cout << "Result is: " << result; cout << endl; return (0); } int addNums(int a, int b, int c) {
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern