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.
#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.
Functions are a form of abstraction.
Abstraction means that you don’t need to understand how everything works “under the hood” in order to interact with a system.
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.
• 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?
Why should I use functions? Are they versatile? Functions are just like verbs!
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. •
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; � } � ¡
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! } � ¡
Newspaper content is passed to a printer, just like a function parameter!
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.
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. ¡ ¡ ¡
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!
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.
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!
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.
“ 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.
Recommend
More recommend