Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

TECHNOLOGICAL INSTITUTE OF THE PHILIPPINES

College of Information Technology Education

Functions
Intended Learning Outcomes (ILOs)

At the end of the lesson, the students should be able to


• Distinguish the basic concepts of functions;
• Differentiate the functions that do not return a value and
functions that return a value;
• Differentiate the functions call by value and functions call by
reference; and
• Create a program using function in C++ Programming
language.
Topics

This module will cover the following topics:


1. What is a function?
2. Declaring and Defining Functions
3. Void Functions
4. Returning Values from Functions
5. Function Call by Value
6. Function Call by Reference
Functions

• A function is a block of code which only runs when it is called.


• You can pass data, known as parameters, into a function.
• Functions are used to perform certain actions, and they are important for reusing code:
Define the code once, and use it many times.
• A function is a group of statements that together perform a task. Every C++ program has at
least one function, which is main(), and all the most trivial programs can define additional
functions.
• You can divide up your code into separate functions. How you divide up your code among
different functions is up to you, but logically the division is such that each function
performs a specific task.
• The C standard library provides numerous built-in functions that your program can call. For
example, strcat() to concatenate two strings, memcpy() to copy one memory location to
another location, and many more functions.
• A function can also be referred as a method or a sub-routine or a procedure, etc.
Declaring and Defining Functions

• A function declaration tells the compiler about a function's


name, return type, and parameters. A function definition provides the
actual body of the function.
• The parameter list identifies each parameter and its type, separated by
commas.
• Here's a declaration for a function that determine a rectangle's area
using length and width parameters:
int findArea (int length, int width);
The three parts of the declaration are the following:

• The return type, int


• The name, findArea
• The type and name of two parameters, an int named length and
an int named width
Void Functions

• Void functions are functions that do not return a value.


• A common application where a void function is used in printing the
result of calculations to the screen. The calculations might be
performed elsewhere, but the results would be printed using the void
function.
• If a function is declared with a void, it means there is no return value, it
just does something.
• Here is an example of simple void function declaration:
 void displayMessage();
 void addTwoNumbers(int, int);
Example of Void Function ( no return value)

• Problem: Write a program that will display two number using void function. Create a function name
"addTwoNumbers" with the data type void..
#include<iostream> //header file
using namespace std; OUTPUT:
void addTwoNumbers(int , int); //declaring function The sum is 12

int num1 =7, num2=5, sum=0; // global variable


main()
{ addTwoNumbers(num1, num2); //calling the function addTwoNumbers
}

//Function void addTwoNumbers Codes


void addTwoNumbers (int a, int b)
{ sum= a + b;
cout<<"The sum is "<< sum; // no return value
}
Returning Values from Functions

• If you want the function to return a value, you can use a data type ( such
as int, string, etc.) instead of void, and use the return keyword inside the
function.
• Here is an example of returning values from function declaration:
 int addTwoNumbers(int, int);
Example Returning Values from Functions ( with return value)

• Problem: Write a program that will display two number using returning values from function. Create a function name
"addTwoNumbers" with the data type int.
#include<iostream> //header file
OUTPUT:
using namespace std;
The sum is 12
int addTwoNumbers(int , int); declaring function

int num1 =7, num2=5, sum=0; // global variable


main()
{ sum= addTwoNumbers(num1, num2); //calling the function addTwoNumbers
cout<<"The sum is "<< sum;
}
//Function int addTwoNumbers Code
int addTwoNumbers (int a, int b)
{ sum= a + b;
return sum; // return value
}
Function - Call by Value

• This method copies the actual value of an argument into the formal parameter of the function. In
this case, changes made to the parameter inside the function have no effect on the argument.
• Copy of the argument’s value is passed to the function.
• Changes made to the formal parameter does not affect the actual parameter
Example Function Call by Value

• Problem: Write a program that will display swap the two number using function call by value. Create a function name
“swap" with the data type int.
#include<iostream>
//Function swap Code
using namespace std;
int swap(int x, int y){
int swap(int a, int b); // declaring function
int temp=x;
x = y;
main() {
y = temp;
int a=10, b=20;
} //end function
cout<<"Before Swap\n";
cout<<a <<" " << b;
OUTPUT:
swap(a,b); //calling the function swap Before swap:
cout<<"After Swap\n"; 1020
cout<<a <<" " << b; After swap:
} //end main 1020
Function - Call by Reference

• This method copies the address of an argument into the formal


parameter.
• Inside the function, the address is used to access the actual argument
used in the call.
• This means that changes made to the parameter affect the argument.
• The address of the argument is passed to the function. Return more
than one values.
• Any changes made to the formal parameter will affect the actual
parameter
• The reference uses & symbol.
Example Function Call by Reference

• Problem: Write a program that will display swap the two number using function call by reference. Create a function name
“swap" with the data type int.
#include<iostream> //Function swap Code
using namespace std; int swap(int &x, int &y){
int swap(int &x, int &y); // declaring function int temp=x;
x = y;
main() { y = temp;
int a=10, b=20; } //end function
cout<<"Before Swap\n";
cout<<a <<" " << b;

swap(a,b); //calling the function swap OUTPUT:


cout<<"After Swap\n"; Before swap:
cout<<a <<" " << b; 1020
} //end main After swap:
2010
Quick Check

 Open your Dev C++


 Create a program that will compute the area of a circle, rectangle
 Requirements:
 Should have a menu to select such as [1] Circle [2] Rectangle
 should have user inputs e.g Enter radius, Enter length, Enter width
 Should used a function
 Should submit it in our discussion

You might also like