1 / 20

CSIS 113A

CSIS 113A. Lecture 5 Functions. Introduction to Functions. Building Blocks of Programs Other terminology in other languages: Procedures, subprograms, methods In C++: functions I-P-O Input – Process – Output Basic subparts to any program Use functions for these ‘pieces’.

parisp
Télécharger la présentation

CSIS 113A

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSIS 113A Lecture 5 Functions

  2. Introduction to Functions • Building Blocks of Programs • Other terminology in other languages: • Procedures, subprograms, methods • In C++: functions • I-P-O • Input – Process – Output • Basic subparts to any program • Use functions for these ‘pieces’

  3. Predefined Functions • Libraries full of functions for our use! • Two types: • Those that return a value • Those that do not (void) • Must ‘#include’ appropriate library • e.g.: • <cmath>, <cstdlib> (Original ‘C’ libraries) • <iostream> (for cout, cin)

  4. Using Predefined Functions • Math functions very plentiful • Found in library <cmath.h> • Most return a value (the ‘answer’) • Example: theRoot = sqrt(9.0); • Components:sqrt = name of library functiontheRoot = variable used to assign ‘answer’ to9.0 = argument or ‘starting input’ for function • In I-P-O: • I = 9.0 • P = ‘compute the square root’ • O = 3, which is returned & assigned to theRoot

  5. The Function Call • Back to this assignment:theRoot = sqrt(9.0); • The expression ‘sqrt(9.0)’ is known as afunction call, or function invocation • The argument in a function call (9.0) can be aliteral, a variable, or an expression • The call itself can be part of an expression: • bonus = sqrt(sales)/10; • A function call is allowed wherever it’s legal to usean expression of the function’s return type

  6. An Example #include <iostream>#include <cmath> using namespace std;int main(){int numLoop;   cout << "Enter the number of iterations ";   cin >> numLoop;    for(int i = 0; i < numLoop; i++)    {      cout << "The square of " << i << "is " << pow(i, 2) << endl;      cout << "The cube of " << i << " is " << pow(i, 3) << endl;      cout << "The sqrt of " << i << " is " << sqrt(i) << endl;    }    return 0; }

  7. More Predefined Functions • #include <cstdlib> • Library contains functions like: • abs() // Returns absolute value of an int • labs() // Returns absolute value of a long int • *fabs() // Returns absolute value of a float • *fabs() is actually in library <cmath>! • Can be confusing • Remember: libraries were added after C++ was‘born’, in incremental phases • Refer to appendices/manuals for details

  8. More Math Functions • pow(x, y) • Returns x to the power ydouble result, x = 3.0, y = 2.0;result = pow(x, y);cout << result; • Here 9.0 is displayed since 3.02.0 = 9.0 • Notice this function receives twoarguments • A function can have any number ofarguments, of varying data types

  9. Even More Math Functions

  10. Predefined Void Functions • No returned value • Performs an action, but sends no ‘answer’ • When called, it’s a statement itself • exit(1); // No return value, so not assigned • This call terminates program • void functions can still have arguments • All aspects same as functions that ‘returna value’ • They just don’t return a value!

  11. Ok, So What Is This void Thing? • Some functions don't return anything • An example from the previous table is the exit function. • Notice that the return type is void. void is a type in C++ and it is used to indicate nothing. • So, why do you need void it if it means nothing? • Well that is a tough question. It all comes down to the fact that the compiler needs a definition of the function before you can use it. • To make all functions similar (meaning they all have a return type) void is used to indicate the function returns nothing.

  12. Programmer Defined Functions • Like small sub programs that allow you to organize your code in a logical fashion. • What are you really doing when you write a piece of software? • The answer is that you are solving some sort of problem. • The easiest way to tackle the problem is to break the large problem into several smaller problems and then tackle them one at a time. • In computer science, this is called stepwise refinement

  13. Function Syntax

  14. User Defined Functions II • Return Type – • All functions declarations must have a return type. If the function doesn't return anything then you need to use the keyword void. You can return any valid C++ type from a function. Later on you will learn that you can return you own custom made types. • Function Name – • You can name a function anything that you like but you must also follow the same rules for naming variables. • Formal Parameters – • The formal parameters (formal arguments) must be enclosed inside the parentheses following the function name. If you write a function that doesn't need any parameters then you just leave the parentheses empty. If you need multiple parameters sent to your function they must be separated by commas. • Curyly Braces – • The body of the function is always surrounded by open and closed curly braces. If you omit one or all of the braces then you will get a big syntax error. • Function Body – • All of the code you want executed when the function is called (invoked) goes between the open and closed curly braces.

  15. Simple Example #include <iostream> using namespace std; void helloWorld(); int main(){    helloWorld();   return 0; } void helloWorld(){   cout << "Hello World" << endl;}

  16. The return Statement • Two ways that program can branch back to where it came from. • 1. when the closing brace of the function is reached. • In this case the flow of the program will branch back to where it was called from. • 2. The return statement. You can place the return statement anywhere you want within a function and the program will branch back to where it came from.

  17. Another Example #include <iostream> using namespace std; void helloWorld(); int main(){    helloWorld();   return 0; } void helloWorld(){   cout << "Hello World" << endl;   return; // Force the flow of the program to brach back to where it came from }

  18. Returning A Value • Some functions produce values. • Data gets returned to calling function • Place a value after the return statement • return10; • Value must match the return type • Value returned gets assigned through equal sign • int x = foo(); // Assigned just like value to a variable

  19. Another Example #include <iostream> using namespace std; int getValue(); int main(){int someValue;    someValue = getValue();   cout << "The value entered was " << someValue << endl; return 0; } int getValue(){int val;   cout << "Please Enter an Integer " << endl;   cin >> val;   return val;}

  20. Modified Example #include <iostream> using namespace std; int getValue(); int main(){   cout << "The value entered was " << getValue() << endl; return 0; } int getValue(){int val;   cout << "Please Enter an Integer " << endl;   cin >> val;   return val;}

More Related