150 likes | 388 Views
Announcements. Midterm Dates First Midterm Exam: November 22, 2008, Saturday, 10:40 – 12:20 Second Midterm Exam: December 26, 2008, Friday, 17:40 – 19:20 First homework is due on October 1 5 , Wednesday, 19 :00 Submit to SUCourse
E N D
Announcements • Midterm Dates • First Midterm Exam: November 22, 2008, Saturday, 10:40 – 12:20 • Second Midterm Exam: December 26, 2008, Friday, 17:40 – 19:20 • First homework is due on October 15, Wednesday, 19:00 • Submit to SUCourse • Strictly follow the submission guideline provided in the homework specification • New (Second) homework will be assigned next week • Email list is active • CS201-AB-200801@sucourse.sabanciuniv.edu • A service provided by SUCourse • Your sabanciuniv accounts must be in this list • We’ve already sent some emails to this list. Did you get them? • We will make announcements to the list and sometimes to SUCourse • Check your emails and SUCourse regularly • Please do not subscribe this list address to some other lists
Chapter 2 – Continued / Functions • Main function • A program is a collection of functions and classes • main and other programmer defined functions • only main is executed automatically when the program starts • other functions must be called to be executed • Programmer-defined functions • piece of code to do a specific job • must be declared before using (this is called calling a function) return-typefunction-name(parameters) { local variables statements } • to execute a function just write its name (and arguments for parameters) • When a function is called, execution order temporarily jumps to function • after the function ends, execution order goes back to the caller • For the time being • return type is void – that means returns nothing • no parameters (we will see parametric functions today)
Why Functions? • Divide and Conquer technique • divide your problem into small ones • write one function for each • combine them in the main program • Provides modularity • when you need to replace a piece of code, you just change a function • you may not need to change the main program • Reuse the same code piece several times • avoid repeating the same code
With no Functions #include <iostream> using namespace std; int main() { cout << " |||||||||||||||| " << endl; cout << " | | " << endl; cout << " | o o | " << endl; cout << " _| |_ " << endl; cout << "|_ _|" << endl; cout << " | |______| | " << endl; cout << " | | " << endl; return 0; } • Prints head, but not so modular • changing the head style requires an update in main. Is this bad? • in this “small” example, need for modularity may not be clear, but in long programs you will need it. • Several heads (like a totem)? • you have to duplicate the same code
With Functions Seeparts.cppfor the entire program #include <iostream> using namespace std; // functions appear here int main() { Hair(); Sides(); Eyes(); Ears(); Smile(); Sides(); return 0; } • Although more complicated, What are advantages of this main over one in which several output statements appear in main? • modularity • changing the heads = changing function(s) • what about eyeglasses? mustache? (let’s do one) • multiple heads (totem poles)? (let’s do one)
Functions with Parameters • Functions are useful, parameterized functions might be more useful • You may need to pass data into a function when you call it • Consider a generic function to calculate the area of any circle and display it • how will the function know the radius? • solution: parameters • function is defined without knowing the value of the radius • value of radius is passed to the function when it is called • parameters are defined similar to variables type name float radius
Area calculation using parameterized function #include <iostream> using namespace std; // area calculation program that employs functions void calculate_area (float radius) { float area; area = 3.14*radius*radius; cout << "The area of a circle with radius " << radius << " is " << area << endl; } int main() { float r; r = 3; calculate_area(r); calculate_area(2.5); calculate_area(r/2); return 0; }
Area calculation using parameterized function #include <iostream> using namespace std; // area calculation program that employs functions void calculate_area (float radius) { float area; area = 3.14*radius*radius; cout << "The area of a circle with radius " << radius << " is " << area << endl; } int main() { float r; r = 3; calculate_area(r); calculate_area(2.5); calculate_area(r/2); return 0; } radius is 3 Output Screen The area of a circle with radius 3 is 28.26
Area calculation using parameterized function #include <iostream> using namespace std; // area calculation program that employs functions void calculate_area (float radius) { float area; area = 3.14*radius*radius; cout << "The area of a circle with radius " << radius << " is " << area << endl; } int main() { float r; r = 3; calculate_area(r); calculate_area(2.5); calculate_area(r/2); return 0; } radius is 2.5 Output Screen The area of a circle with radius 3 is 28.26 The area of a circle with radius 2.5 is 19.625
Area calculation using parameterized function #include <iostream> using namespace std; // area calculation program that employs functions void calculate_area (float radius) { float area; area = 3.14*radius*radius; cout << "The area of a circle with radius " << radius << " is " << area << endl; } int main() { float r; r = 3; calculate_area(r); calculate_area(2.5); calculate_area(r/2); return 0; } radius is 1.5 Output Screen The area of a circle with radius 3 is 28.26 The area of a circle with radius 2.5 is 19.625 The area of a circle with radius 1.5 is 7.065
Functions with Parameters (continued) • Parameters and Arguments • parameter is the generic name used in function • radius in the calculate_area function • argument is the value passed to function while it is called • 2.5 • r (actually current value of r is passed) • r/2 (actually current value of r/2 is passed) • Parameter list provides type and name of the parameters • Argument type must match parameter type • Functions may have multiple parameters • corresponding arguments are separated by commas • in the same order of parameter list • be careful about the type matching • we will see examples later on • Functions may call other functions (have seen in the totem example and will see in the next example)
Functions with Parameters (continued) • Parameters versus Local Variables • Parameters and defined and used locally, but their initial value comes from the caller function via arguments • Local variables are defined and used in the functionlocally • But the initial value does not come from the caller function • While designing your functions, think carefully about local variables and parameters • If you need to pass the initial value from the caller function, then it should be a parameter. • If you won’t pass the initial value, then it should be a local variable. • Example: in calculate_area function • area is a local variable since we do not pass its initial value from main • radius is a parameter since we need to pass its initial value from main • Unnecessary parameters may cause problems and grade reduction (for homework)
Old McDonald’s Farm • Aim is to have a modular program (with functions) to display the song • each verse repeats using a different animal • refrain parts repeat within verses • A partial output (with two animals only) below Old MacDonald had a farm, Ee-igh, Ee-igh, oh! And on his farm he had a cow, Ee-igh, Ee-igh, oh! With a moo moo here And a moo moo there Here a moo, there a moo, everywhere a moo moo Old MacDonald had a farm, Ee-igh, Ee-igh, oh! Old MacDonald had a farm, Ee-igh, Ee-igh, oh! And on his farm he had a cat, Ee-igh, Ee-igh, oh! With a meowmeow here And a meowmeow there Here a oink, there a oink, everywhere a meowmeow Old MacDonald had a farm, Ee-igh, Ee-igh, oh!
Old McDonald’s Farm (Design of Functions) • Which functions do we need? • a function for only “Ee-igh, Ee-igh, oh!” • a function for the refrain (nakarat) • a function for “And on his farm ...” line • animal is parameter • a function for “With a ...”, “And a ...”, “Here a ...” lines • noise of the animal is a parameter • a function for the whole verse • in the main program call the verse function with several animal and noise arguments
Old McDonald’s Farm (Program) • See oldmac2.cpp • Remarks • functions call functions • Refrain calls EiEio • Verse calls Refrain • Verse has two parameters • therefore it is called using two arguments –first one is for animal, second is for noise • order of arguments is important • animal is used as parameter in two functions. • Should we need to use the same parameter name in both cases? Or can we use different names in two different functions? • Same questions are valid for noise as well. • Answer is “Yes we can use different names”. Let’s do and see. • Can we input animal and noise? • of course yes (will do)