03.Functions (1)
03.Functions (1)
SoftUni Team
Technical Trainers
Software University
https://softuni.bg
Table of Contents
1. What is a Function
Calling, Defining, Implementing
2. Declaring vs. Defining
3. Functions with Parameters
4. Returning Values from Functions
5. Static Variables Inside Functions
6. Value vs. Reference Types
Memory Stack and Heap 2
Have a Question?
sli.do
#cpp-fundamentals
3
Functions
Calling, Defining, Implementing
What is a Function?
Named block of code, that performs a specific task
Can take parameters and return a value
Sample function definition: Function named
printHelloWorld
void printHelloWorld ()
{
Function body
cout << "Hello World!" <<
always
endl; surrounded
}
Also known as methods by { }
main() is a function
5
Why Use Functions?
More manageable programming
Splits large problems into small pieces
Better organization of the program
Improves code readability
Improves code understandability
Avoiding repeating code
Improves code maintainability
Code reusability
Using existing methods several times
6
{…}
Declaring and Calling Functions
Declaring Functions
Declaration – function's name, return type and parameters
Can be separate from definition (which includes
the code block)
Parameters: empty, single, or several separated by ,
Type Function name Parameters
void PrintNumber(int
number) Function
{ body
cout << number << endl;
} 8
Calling Functions
Using functions is almost like using variables, however:
You write () after them, which could contain parameters
Most functions return a value – you can use it in an expression
void functions don't have values
void HelloWorld()
{
std::cout << "Hello World!" <<
std::endl;
}
int main() {
HelloWorld();
return 0; 9
}
{…}
Declaring and Calling Functions
LIVE DEMO
Declaring vs. Defining Functions
Declaring vs. Defining Functions
Declaration – tells the compiler there #include<iostream>
is certain a function using namespace std;
int sbyte
19
Parameters and Returning Values
LIVE DEMO
Overloading Functions
Overloaded Functions
Using the same function name and return type but with
different parameter list
Different number or types of parameters
int getMax(int a, int b) {
if (a > b) {
return a;
}
return b;
}
29
Value vs. Reference Types
30
Passing By Value vs. Passing By Reference
Parameters are normally copies int square(int num) {
num = num * num;
of their originals return num;
}
Changing them does NOT change void swap(int& a, int& b) {
the caller's variables int oldA = a; a = b; b =
oldA;
"Passing by value" }
int main() {
To access the caller's variables int x = 5;
std::cout << square(x);
directly, use references //25
std::cout << x; //5
Syntax: DataType& param int y = 42;
swap(x, y);
"Passing by reference" std::cout << x; //42 31
Value vs. Reference Types
LIVE DEMO
Summary
Break
… large programs into simple
functions
… that solve small sub-problems
Functions consist of declaration and body
…
Functions are called by their name + ()
Functions can accept parameters
Functions can return a value or
nothing (void)
33
Questions?
© SoftUni – https://about.softuni.bg. Copyrighted document. Unauthorized copy, reproduction or use is not permitted.
SoftUni Diamond Partners
Educational Partners
36
License
37
Trainings @ Software University (SoftUni)
Software University – High-Quality Education,
Profession and Job for Software Developers
softuni.bg, about.softuni.bg
Software University Foundation
softuni.foundation
Software University @ Facebook
facebook.com/SoftwareUniversity
Software University Forums
forum.softuni.bg
38