Calling Functions: ITP 165 - Fall 2015 Week 5, Lecture 1
Calling Functions: ITP 165 - Fall 2015 Week 5, Lecture 1
All that code went inside main, and the code started becoming
lengthy and maybe confusing
Ideally, we want to separate the code into several logical parts in
our file. This makes it easier to follow and maintain
Functions
A function allows us to break up the program into named subsections
Functions often give back (or return) a value though they arent
required to
Using Functions
Youve already used a function, you just didnt know at the time!
In Homework #2, you had to include the cmath library:
#include <cmath>
And then you used std::pow, like this:
double result = std::pow(2.0, 5.0);
Calling std::pow
Name of function
(std::pow)
Purpose
Parameter(s)
std::pow
std::sqrt
1 a number
std::cos
std::sin
std::log10
1 a number
std::floor
1 a number
std::ceil
1 a number
std::strlen Example
char str[] = "Testing";
int x = std::strlen(str);
std::cout << x << std::endl;
A Program w/ a Function
#include <iostream>
void SayHello()
{
std::cout << "Hello" << std::endl;
}
int main()
{
SayHello();
return 0;
}
In Action
Function Declaration
A function declaration is what defines a custom function
Body of
function, in
braces
Name of function
(in this case, SayHello)
Parameters, in
parenthesis (this
function takes no
parameters)
void SayHello()
{
std::cout << "Hello" << std::endl;
}
return 0;
}
SayHello has to be
declared before main,
since we use it in main
Error!
It will say that it doesnt
know what SayHello is
at this line.
void SayHello()
{
std::cout << "Hello" << std::endl;
}
(To save space I omit the SayHello comments, but they should be
there!)
return 0;
}
return 0;
}
We have a call
to the SayHello
function
return 0;
}
return 0;
}
Just a normal
cout
return 0;
}
We reached the
end of SayHello
return 0;
}
When SayHello
ends, we resume
main, right after the
point where SayHello
was called
return 0;
}
The return in
main means
the program
is over!
std::getline()
For Homework 4, youll need another function to get more robust
input:
#include <iostream>
#include <string>
int main()
{
std::string mySentence;
std::cout << Please enter a sentence: ;
std::getline (std::cin, mySentence);
std::getline()
Previously, we used std::cin to get input from the user
But we were limited to single words (like in our cipher lab)
Anything after the space character was not included, and it caused
some very strange errors in our code
Now, we have the ability to get an entire line of text (until the user
presses the return key)
std::getline()
std::getline function name
std::getline(std::cin, mySentence);
std::getline()
If we use std::cin and then use std::getline, we get issues
int myNum;
std::string mySentence
std::cout << "Enter a number: ";
std::cin >> myNum;
std::cout << "Enter a sentence: ";
std::getline(std::cin, mySentence);
std::getline()
std::getline()
In order to make sure we dont get this error, we must clear the
leftovers from our std::cin call
int myNum;
std::string mySentence;
std::cout << "Enter a number: ";
std::cin >> myNum;
std::cin.ignore();
std::cout << "Enter a sentence: ";
std::getline(std::cin, mySentence);
std::getline()
Lab Practical #7