what is a function
play

What is a function? Functions are like the buttons on a calculator. - PowerPoint PPT Presentation

Class 3 What is a function? Functions are like the buttons on a calculator. What are some of the functions of a calculator? The actual code that the function executes is in its own separate spot then, you call it by name (kind of


  1. Class 3 What is a function? • Functions are like the buttons on a calculator. • What are some of the functions of a calculator? • The actual code that the function executes is in its own separate spot— then, you call it by name (kind of like a button!) in your program when you want to use it.

  2. #include <iostream> � • This small program using namespace std; � � demonstrates a simple //function declaration (prototype) � void printHello(); � function called printHello(). � int main(int argc, const char * argv[]) � { � • Good function are simple, � //function call � printHello(); � concise and have clear � names. return 0; � } � � //function definition � • When the function is called void printHello(){ // ß ß function header � in main, the program //function body � cout << "Hi there Nina!"; � jumps to the code within return; � } � the function body below ¡ and runs it. Hi there Nina! • Once the function is done running, the program jumps back to main and continues.

  3. Functions are a form of abstraction.

  4. Abstraction means that you don’t need to understand how everything works “under the hood” in order to interact with a system.

  5. Building a function: #include <iostream> � using namespace std; � 1. Declare your function outside of any � //function declaration (prototype) � other functions or brackets using a void printHello(); � prototype*. � int main(int argc, const char * argv[]) � *It’s actually optional, but de fi nitely recommended. { � Syntax: • //function call � returnType functionName(parameters); printHello(); � � return 0; � } � 2. De fi ne your function. � //function definition � What does your function do? • void printHello(){ // ß ß function header � Syntax: //function body � cout << "Hi there Nina!"; � return; � returnTypefunctionName(parameterType } � parameterName){ Block of code to execute. Hi there Nina! return; } 1. Where do you want to use your function? I want this greeting to print when I run my • program, so I call printHello() in main.

  6. • When do you think functions will be useful to you? • Can you think of any functions you might want to write? • What is the simplest way you can think of to describe what functions do?

  7. Why should I use functions? Are they versatile? Functions are just like verbs!

  8. Functions are amazing! 1. They boil down complex instructions into one function call, like a button. This is great when you need to do one task many times. • 1. You can feed them data to work with by using parameters and arguments. You have seen this with length(string), which counts the • number of characters in the string you specify between the parenthesis. 1. They can spit data back out at you by using return statements. length(string) returns the number of characters in a string. •

  9. Parameters • Func&ons ¡are ¡able ¡to ¡use ¡values ¡from ¡ #include <iostream> � using namespace std; � the ¡outside ¡by ¡defining ¡ parameters ¡ � (highlighted) . ¡ //function declaration (prototype) � void printName( string ); � ¡ � • Parameters ¡live ¡in ¡the ¡parenthesis ¡ int main(int argc, const char * argv[]) � { � following ¡a ¡func&on’s ¡name. ¡ string nameInput; � ¡ � cout << "Enter your name:\n"; � • You ¡pass ¡ arguments ¡ to ¡func&ons ¡ cin >> nameInput; � according ¡to ¡the ¡parameter’s ¡data ¡ � //function call � type. ¡ printName( nameInput ); � � return 0; � • The ¡func&on ¡makes ¡a ¡copy ¡of ¡the ¡ } � value ¡you ¡pass ¡to ¡it ¡in ¡order ¡to ¡use ¡it. ¡ � //function definition � void printName( string n ){ //function header � Enter your name: //function body � Emmett cout << "Hi there " + n + "!\n"; � Hi there Emmett! return; � } � ¡

  10. Parameters printName() has a string • #include <iostream> � parameter. using namespace std; � � //function declaration (prototype) � • printName() accepts nameInput void printName( string ); � � because it is a string, which int main(int argc, const char * argv[]) � matches the parameter type. { � string nameInput; � � • nameInput is an argument cout << "Enter your name:\n"; � cin >> nameInput; � passed by value into printName() � as n. //function call � printName( nameInput ); � � • To pass by value means that return 0; � } � nameInput was copied and � assigned to n. //function definition � void printName( string n ){ //function header � //function body � Enter your name: cout << "Hi there " + n + "!\n"; � Emmett return; � Hi there Emmett! } � ¡

  11. Newspaper content is passed to a printer, just like a function parameter!

  12. So…what happens to all of those parameter variables that were passed by value? I hope you didn’t get too attached to that string n we passed to printName(), because… What t lives ves in a functi tion on, dies es in a functi tion on.

  13. Don’t ¡worry—those ¡variables ¡aren’t ¡just ¡lying ¡all ¡over ¡the ¡ place. ¡ ¡ When ¡a ¡func&on ¡ends, ¡all ¡of ¡the ¡variables ¡declared ¡within ¡ it ¡are ¡cleared ¡from ¡memory. ¡ ¡ ¡

  14. Return Values The return statement allows a • function to send data back when it is called. #include <iostream> � using namespace std; � � You need to specify your return • //function declaration (prototype) � type before the function name int playerStrength(int); � � in both the prototype and int main(int argc, const char * argv[]) � function header. { � int age; � � Of course, the actual return • cout << "How old are you?\n"; � value’s data type must also cin >> age; � � match. cout << "\nWow, you can carry\n"; � //function call � cout << playerStrength(age) << " items!\n"; � playerStrength() returns an int • � value. return 0; � } � � It takes the player’s age input, • //function definition � multiplies it by 5 and returns int playerStrength(int n){ � n *= 5; � the new value. return n; //return value � } � How old are you? 69 Wow, you can carry 345 items!

  15. Return Values Two important notes about return values: #include <iostream> � using namespace std; � � 1. The return statement is a stop //function declaration (prototype) � void printHello(); � sign for the function. � int main(int argc, const char * argv[]) � { � 2. You don’t have to return any � //function call � data, but you do need to have a printHello(); � � return statement. return 0; � } � � 3. To stop a function without a //function definition � void printHello(){ // ß function header � return type, you just use: //function body � return; cout << "Hi there Nina!"; � return; � } � 4. This is the old printHello() ¡ example, which is a function that returns “void,” aka, no Hi there Nina! values.

  16. Does anyone want to write a function that will add two numbers together and return the sum? 1. Prompt user to enter one number. 2. Prompt user to enter a second number. 3. Output the sum of the two numbers by passing those two values into a function. Hint: The return type will be an int!

  17. So what’s all this fuss about parameters and return values—why can’t my program just see ALL of my code? Well… do you remember abstraction? You don’t need to know what makes a laptop tick, because you can interact with it via UI, a keyboard, a screen, etc.

  18. “ Abstraction saves you from worrying about the details, while encapsulation hides the details from you” (p160). ¡ • Encapsulation dictates that data is only visible within the scope (aka any set of curly brackets) that it lives in. • Think about encapsulation in terms of bundling—each object is a separate bundle of instructions that are wrapped up and hidden from the user. • Functions need return values and parameters so that the user can interact with a program without having to worry about each line of code required to execute a task.

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend