C++ Functions
Function is known as the block of code that runs when it is called. It can
also be use to pass data into the function using the parameter.
Objectives
Students are able to implement the C++ function into the program
Know how to pass data among the function
Understand the importance of implementing the function into the particular
programming problem.
Identify and describe the two types of functions
Two types of Function
Standard Library Functions
User-define function
Standard Library Functions
The standard library function is a function that is already defined in the program.
Example: int main() {
}
The int main is the standard function in the C++ programming language. You don’t need to define
this function anymore. Since int main() is the standard in the programming language so, you cannot
remove or change it to whatever you want to name it.
User –define Function
A user-define function is a function that is being defined by the programmer to perform a specific
task in the program. When the function is invoked from any part of the program, it all executes the
codes defined in the body of the function.
Benefits of using user-defined functions
1. Functions make the code reusable. We can declare them once and use them multiple times.
2. Functions make the program easier as each small task is divided into a function.
3. Function increase readability.
C++ Function Declaration
Syntax:
returnType functionName (parameter 1, parameter 2, …….) {
// function body
}
Sample function declaration.
// function declaration
void result() {
cout<<“Welcome to the club”;
}
Calling a Function
In the previews slide, we have declared a function named result(). In order to use the result()
function, we are going to call it.
This will demonstrate how we can call the above result() function.
int main() {
//calling a function
result();
}
Calling a Function
#include <iostream> #include <iostream>
using namespace std; using namespace std;
void result() { void result() { // declaration
//code cout<<“Welcome to NORSU”;
} function call }
int main() { int main() {
… .. … result(); // calling the function
result();
… .. … return 0;
} }
Individual Activity
ACTIVITY 1
Create at-least one program demonstrating the concept of calling a
function.
Function parameters
A function can also be declared with parameters (arguments). A parameter is a value declaring a
function.
Example: void printValue(int a, float b) { // Declaration
cout<<“Your daily salary is: ”<<a<<endl; // Definition
cout<<“Your monthly income isa: ”<<b;
}
The definition will define how the function works within the program.
Function parameters
The int and float data type, and variable a & b inside the function is what we called parameter
void printValue(int a, float b) {
} Parameter
The parameter is used to be a storage for all the data that is being pass into the function.
Function parameters
Sample program:
#include <iostream>
using namespace std;
void displayValue( int a, float b ) {
cout<<"The value of variable a is: "<<a<<endl;
cout<<"The value of variable b is: "<<b;
}
int main() {
int num1 = 5;
float num2 = 2.3;
displayValue(num1, num2);
return 0;
}
Individual Activity
ACTIVITY 2
1. Demonstrate on how to create a function with a parameter.
2.Provide a sample program that demonstrate the process of passing the
data into the function using parameter.
Document your demonstration
Note: Do not copy the given sample in the presentation. Create your own program that will ask a
value from the user.
Thank you for listening
Next topic:
1. Function with return statement
2. Function Prototype