0% found this document useful (0 votes)
6 views38 pages

03.Functions (1)

The document provides an overview of functions in C++, covering their definition, declaration, and usage. It explains the importance of functions in programming for code organization, readability, and reusability, as well as details on parameters, return values, and the difference between value and reference types. Additionally, it discusses function overloading and the use of static variables within functions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views38 pages

03.Functions (1)

The document provides an overview of functions in C++, covering their definition, declaration, and usage. It explains the importance of functions in programming for code organization, readability, and reusability, as well as details on parameters, return values, and the difference between value and reference types. Additionally, it discusses function overloading and the use of static variables within functions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 38

Functions

Defining and Using C++ Functions

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;

 Can be anywhere void HelloWorld();

 Can appear multiple times int main() {


 Same visibility rules as for variables HelloWorld();
 Definition – function's execution }
return 0;

 Can be declared but not defined –


void HelloWorld()
compilation error if called {
cout << "Hello World!" <<
endl;
} 12
Declaring vs. Defining Functions
LIVE DEMO
st r i n g
uint
long
byte short
ulong ushort

int sbyte

Functions with Parameters


Function Parameters
 Function parameters can be of any data type
 Parameters are just variables living in the function’s block
void printNumbers(int start, int Multiple parameters
end) separated by comma
{
for (int i = start; i <= end; i+
+)
{
std::cout << i << std::endl;
}
 Call the method with certain values (arguments)
}
int main() Passing arguments
{
printNumbers(5, 10);
when called
return 0;
} 15
Parameters & Default Values
 Parameters with default values can be omitted
by the caller
#include <iostream>
 If omitted are void CountNumbers(int a = 1, int b =
10)
initialized with {
the default value for( int i = a; i <= b; i++ )
{
 Must be last in the std::cout << i << std::endl;
}
parameter list }
int main()
{
CountNumbers(5, 10);
return 0;
} 16
Returning Values from Functions
Returning Values from Functions
 The return keyword immediately stops the
function's execution
 Returns the specified value
 Non-void functions must have a return followed
by a value
int getMax(int a, int b) {
if (a > b) {
return a;
}
return b;
}
18
Using the Return Values
 Return value can be:
 Assigned to a variable:
int max = getMax(5,
10);
 Used in expression:
double total = getPrice() * quantity *
1.20;

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;
}

int getMax(int a, int b, int c) {


return getMax(a, getMax(b, c));
}
22
Overloading Functions
LIVE DEMO
Static Variables Inside Functions
static Variables Inside Functions
 static variables live through entire program,
initialized once
 static variables can be used inside functions to track state
 E.g. how many times a function was called
void CountNumbers( int a = 1, int b = 10 )
{
static int num = 0;
for( int i = a; i <= b; i++ )
{
cout << i << endl;
num++;
}
cout << "Static int -> " << num << endl;
} 25
Static Variables Inside Functions
LIVE DEMO
Value vs. Reference Types
Memory Stack and Heap
Value Types

 Value type variables hold directly their value


 int, float, double, bool, Stack
char… i
 Each variable has its 42 (4 bytes)
own copy of the value ch

int i = 42; A (1 bytes)

char ch = 'A'; result


bool result = true; true (1 byte)
28
Reference Types
 Reference type variables hold а reference
(pointer / memory address) of the value itself
 Two reference type variables can reference the
same variable
 Operations on both variables access/modify
the same data

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

 This course (slides, examples, demos, exercises, homework,


documents, videos and other assets) is copyrighted content
 Unauthorized copy, reproduction or use is illegal
 © SoftUni – https://about.softuni.bg/
 © Software University – https://softuni.bg

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

You might also like