You are on page 1of 47

Programming Support Session

OKERR@UCLAN.AC.UK
Functions

Function
Invoke
function
Functions

void func()
{
cout << “Hello World”;
}

int main()
{
//do something
func();
}
Functions

void func()
{
cout << “Hello World”;
}

int main()
{
//do something
func();
//do something else
}
Functions

void func()
{
cout << “Hello World”;
}

int main()
{
//do something
func();
//do something else
}
Functions

void func()
{
cout << “Hello World”;
}

int main()
{
//do something
func();
//do something else
}
Functions

void func()
{
cout << “Hello World”;
}

int main()
{
//do something
func();
//do something else
}
Functions - return

 Functions can return a result


 The result must be a single value of the data type indicated in the function definition

int calc()
{
return 5;
}
Functions - return

 Functions can return a result


 The result must be a single value of the data type indicated in the function definition

int calc()
{
return 5;
}
Functions - return

 Functions can return a result


 The result must be a single value of the data type indicated in the function definition
 Can be any standard data type (string, int, double, char, etc.)
 Can be pointers
 Can be structs
 Can return nothing (void)
 Cannot be arrays
Functions - parameters

 Used to pass values into a function


 Specified in function definition
 Can be any type of variable
Functions - parameters

float CalcArea(float width, float height)


{
float area;
area = width * height;
return area;
}

int main()
{
float area = CalcArea( 32.5, 17.0 );
cout << area << endl
}
Functions - parameters

float CalcArea(float width, float height)


{
float area;
area = width * height;
return area;
}

int main()
{
float area = CalcArea( 32.5, 17.0 );
cout << area << endl
}
Functions - parameters

float CalcArea(float width, float height)


{
float area;
area = width * height;
return area;
}

int main()
{
float area = CalcArea( 32.5, 17.0 );
cout << area << endl
}
Functions - parameters

32.5 17.0
float CalcArea(float width, float height)
{
float area;
area = width * height;
return area;
}

int main()
{
float area = CalcArea( 32.5, 17.0 );
cout << area << endl
}
Functions - parameters

32.5 17.0
float CalcArea(float width, float height)
{
float area;
area = width * height;
return area;
}

int main()
{
float area = CalcArea( 32.5, 17.0 );
cout << area << endl
}
Functions - parameters

32.5 17.0
float CalcArea(float width, float height)
{
float area;
area = width * height;
return area; 552.5

int main()
{
float area = CalcArea( 32.5, 17.0 );
cout << area << endl
}
Functions - parameters

32.5 17.0
float CalcArea(float width, float height)
{
float area;
area = width * height;
return area; 552.5

int main()
{ 552.5

float area = CalcArea( 32.5, 17.0 );


cout << area << endl
}
Functions - parameters

32.5 17.0
float CalcArea(float width, float height)
{
float area;
area = width * height;
return area; 552.5

int main()
{ 552.5

float area = CalcArea( 32.5, 17.0 );


cout << area << endl
}
Functions - parameters

 Two types of parameters


 Call by copy
 Call by reference
Functions - parameters

 Two types of parameters


 Call by copy
 Creates a local version of the variables within the function
 Literally a copy of the original variables
 Any changes made within the function does not affect the original variables
 No additional syntax needed
 Call by reference
Functions - parameters

 Two types of parameters


 Call by copy
 Call by reference
 Function refers to the original variables
 Faster, more memory efficient than creating copies of the variables
 Any changes made within the function will be applied to the original variables
Arrays

 Arrays can hold many variables of the same type


 Can be of any data type (including structs)

int arr[10];

arr[0] arr[1] arr[2] arr[3] arr[4] arr[5] arr[6] arr[7] arr[8] arr[9]
13 11 5 2 82 13 53 2 8 23

 What would happen when you access arr[10]?


Arrays

 You can assign values to individual elements or initialise the entire array

arr[3] = 43;
int arr[7] = {1,2,3,4,5,6,7};
Nested statements

How many times will true and false be outputted?


sum = 0;

for (int i = 1; i <= 4; i++) {


      sum = sum + i;
      if (sum > 3) {
          cout << “true”
      }
      else {
          cout << “false”
      }
}
Nested statements

How many times will true and false be outputted?


sum = 0;

for (int i = 1; i <= 4; i++) {


      sum = sum + i;
      if (sum > 3) {
          cout << “true”
      }
      else {
          cout << “false”
      }
}
Nested statements

How many times will true and false be outputted?


sum = 0;

for (int i = 1; i <= 4; i++) {


      sum = sum + i;
      if (sum > 3) {
          cout << “true”
      }
      else {
          cout << “false”
      }
}
Nested statements

How many times will true and false be outputted?


sum = 0; 1

for (int i = 1; i <= 4; i++) {


      sum = sum + i;
      if (sum > 3) {
          cout << “true”
      }
      else {
          cout << “false”
      }
}
Nested statements

How many times will true and false be outputted?


sum = 0; 1

for (int i = 1; i <= 4; i++) {


      sum = sum + i;
      if (sum > 3) {
          cout << “true”
      }
      else {
          cout << “false”
      }
}
Nested statements

How many times will true and false be outputted?


sum = 0; 1

for (int i = 1; i <= 4; i++) {


      sum = sum + i;
      if (sum > 3) {
          cout << “true”
      }
      else {
          cout << “false”
      }
}
Nested statements

How many times will true and false be outputted?


sum = 0; 1

for (int i = 1; i <= 4; i++) {


      sum = sum + i;
      if (sum > 3) {
          cout << “true”
      }
      else {
          cout << “false”
      }
}
Nested statements

How many times will true and false be outputted?


sum = 0; 1

for (int i = 1; i <= 4; i++) {


      sum = sum + i;
      if (sum > 3) {
          cout << “true”
      }
      else {
          cout << “false”
      }
}

false
Nested statements

How many times will true and false be outputted?


sum = 0; 1

for (int i = 1; i <= 4; i++) {


      sum = sum + i;
      if (sum > 3) {
          cout << “true”
      }
      else {
          cout << “false”
      }
}

false
Nested statements

How many times will true and false be outputted?


sum = 0; 3

for (int i = 1; i <= 4; i++) {


      sum = sum + i;
      if (sum > 3) {
          cout << “true”
      }
      else {
          cout << “false”
      }
}

false
Nested statements

How many times will true and false be outputted?


sum = 0; 3

for (int i = 1; i <= 4; i++) {


      sum = sum + i;
      if (sum > 3) {
          cout << “true”
      }
      else {
          cout << “false”
      }
}

false
Nested statements

How many times will true and false be outputted?


sum = 0; 3

for (int i = 1; i <= 4; i++) {


      sum = sum + i;
      if (sum > 3) {
          cout << “true”
      }
      else {
          cout << “false”
      }
}

false
Nested statements

How many times will true and false be outputted?


sum = 0; 3

for (int i = 1; i <= 4; i++) {


      sum = sum + i;
      if (sum > 3) {
          cout << “true”
      }
      else {
          cout << “false”
      }
}

false false
Nested statements

How many times will true and false be outputted?


sum = 0; 3

for (int i = 1; i <= 4; i++) {


      sum = sum + i;
      if (sum > 3) {
          cout << “true”
      }
      else {
          cout << “false”
      }
}

false false
Nested statements

How many times will true and false be outputted?


sum = 0; 7

for (int i = 1; i <= 4; i++) {


      sum = sum + i;
      if (sum > 3) {
          cout << “true”
      }
      else {
          cout << “false”
      }
}

false false
Nested statements

How many times will true and false be outputted?


sum = 0; 7

for (int i = 1; i <= 4; i++) {


      sum = sum + i;
      if (sum > 3) {
          cout << “true”
      }
      else {
          cout << “false”
      }
}

false false
Nested statements

How many times will true and false be outputted?


sum = 0; 7

for (int i = 1; i <= 4; i++) {


      sum = sum + i;
      if (sum > 3) {
          cout << “true”
      }
      else {
          cout << “false”
      }
}

false false
Nested statements

How many times will true and false be outputted?


sum = 0; 7

for (int i = 1; i <= 4; i++) {


      sum = sum + i;
      if (sum > 3) {
          cout << “true”
      }
      else {
          cout << “false”
      }
}

false false true


Nested statements

How many times will true and false be outputted?


sum = 0; 7

for (int i = 1; i <= 4; i++) {


      sum = sum + i;
      if (sum > 3) {
          cout << “true”
      }
      else {
          cout << “false”
      }
}

false false true


Nested statements

How many times will true and false be outputted?


sum = 0; 11

for (int i = 1; i <= 4; i++) {


      sum = sum + i;
      if (sum > 3) {
          cout << “true”
      }
      else {
          cout << “false”
      }
}

false false true


Nested statements

How many times will true and false be outputted?


sum = 0; 11

for (int i = 1; i <= 4; i++) {


      sum = sum + i;
      if (sum > 3) {
          cout << “true”
      }
      else {
          cout << “false”
      }
}

false false true


Nested statements

How many times will true and false be outputted?


sum = 0; 11

for (int i = 1; i <= 4; i++) {


      sum = sum + i;
      if (sum > 3) {
          cout << “true”
      }
      else {
          cout << “false”
      }
}

false false true


Nested statements

How many times will true and false be outputted?


sum = 0; 11

for (int i = 1; i <= 4; i++) {


      sum = sum + i;
      if (sum > 3) {
          cout << “true”
      }
      else {
          cout << “false”
      }
}

false false true true

You might also like