Part Six - Sen 101 Pop 1
Part Six - Sen 101 Pop 1
Abdulhamid Idris
(PRINCIPLES OF PROGRAMMING 1)
-- 6 --
SECTION SIX: FUNCTION
A function is a block of code that performs a specific task.
Functions allow you to organize your code, break it into smaller reusable pieces, and improve
code readability and maintainability.
With functions, you only need to define the code once, and you can use it many times.
A function only gets executed when it is called.
Data (parameters) can be passed into a function for it to work with.
The line in the definition that reads int main() is called the function header .
A void function doesn’t return any value to its caller.
FUNCTION CONTD.
Calling a Function
Functions are invoked/called by referring to them by their names and passing any
arguments defined in the function’s header.
FUNCTION CONTD.
Function prototype
#include <iostream>
// Function prototype
int main()
std::cout << "The sum is: " << result << std::endl;
return 0;
// Function definition
Function argument is the actual value or reference that you pass to the function when you call it. The
argument corresponds to the function's parameter and is used to provide real data that the function will
process.
Example
int main()
{
printSum(3, 5);
return 0;
}
Here, 3 and 5 are function arguments. They are the actual values passed to the printSum function,
corresponding to the parameters a and b inside the function.
FUNCTION CONTD.
Default argument
A default argument allows you to provide default values for one or more parameters of a function.
This means that if a caller does not supply a value for that parameter when calling the function, the
function will use the default value instead.
Default arguments make your functions more flexible, as they allow them to be called with fewer
arguments.
Default arguments are specified in the function declaration (or prototype), not in the definition(body of
the function).
They must appear from right to left. This means that you cannot provide a default argument for a
parameter without providing default values for all the parameters that come after it.
A default argument can only be provided once, either in the function declaration or in the function
definition, but not both.
FUNCTION CONTD.
Default argument contd.
#include <iostream>
// Function with default arguments
void printMessage(std::string message = "Hello, World!", int times = 1)
{
for (int i = 0; i < times; i++)
{
std::cout << message << std::endl;
}
}
// program continues on next slide
FUNCTION CONTD.
Default argument contd.
int main()
{
// Using both default arguments
printMessage();
// Prints "Hello, World!" once
// Using the default value for 'message', but specifying 'times' printMessage("Goodbye!", 2);
// Prints "Goodbye!" twice
return 0;
}
FUNCTION CONTD.
Default argument contd.
Explanation
void myFunction()
int x = 5;
int main()
myFunction();
return 0;
}
FUNCTION CONTD.
Local variable contd.
Scope: A local variable is only visible within the function or block where it is
declared. Once the program execution leaves that scope, the variable is no longer
accessible.
Lifetime: The lifetime of a local variable is limited to the duration of the function or
block. It is created when the function is called and destroyed when the function
returns.
Default Initialization: Local variables are not automatically initialized. If you do not
assign a value to them, their value will be undefined.
FUNCTION CONTD.
Global variable
A global variable is declared outside of all functions, typically at the top of a file, and
it is accessible from any function within the same program (or file, if declared extern
in others).
FUNCTION CONTD.
Global variable contd.
#include <iostream>
int x = 10;
void myFunction()
int main()
myFunction();
return 0;
}
FUNCTION CONTD.
Global variable contd.
Scope: A global variable has a program-wide scope. It is visible and accessible from
any function within the same program (or across multiple files if declared extern).
Lifetime: The lifetime of a global variable is the entire duration of the program’s
execution. It is created when the program starts and destroyed when the program
ends.
Default Initialization: Global variables are automatically initialized to zero (for
basic types like int, float, etc.) if no explicit initialization is provided.
FUNCTION CONTD.
Function overloading
Function overloading in C++ allows you to define multiple functions with the same
name but with different parameter lists.
The function signature (name + parameters) must be different, and it enables you to
call the same function with different numbers or types of arguments.
Function overloading works by distinguishing functions based on the number of
arguments or the types of arguments passed.
C++ will automatically pick the correct function based on the call, as long as there is
no ambiguity.
FUNCTION CONTD.
Function overloading contd.
Consider a simple sum function that calculates the sum of numbers. We can overload
this function in several ways by changing the number or types of arguments.
Examples
FUNCTION CONTD.
Function overloading contd.
Example 1: Overloading based on the number of arguments
Here, we define multiple versions of the sum function to add:
Two integers
Three integers
FUNCTION CONTD.
Function overloading contd.
#include <iostream>
int main()
std::cout << "Sum of two integers: " << result1 << std::endl; // Outputs 30
std::cout << "Sum of three integers: " << result2 << std::endl; // Outputs 60
return 0;
}
FUNCTION CONTD.
Function overloading contd.
Explanation
The sum function is overloaded: one version takes two integers and the other takes
three integers.
int main()
std::cout << "Sum of integers: " << intResult << std::endl; // Outputs 30
std::cout << "Sum of doubles: " << doubleResult << std::endl; // Outputs 31.0
return 0;
}
FUNCTION CONTD.
Function overloading contd.
Explanation
The sum function is overloaded: one version takes integers and the other takes
numbers with decimal points.
This code defines a function named sum that accepts two int arguments.
The arguments are passed into the parameter variables num1 and num2 . Inside the function, a
variable, result is defined.
After the variable definition, the parameter variables num1 and num2 are added, and their sum is
assigned to the result variable.
The last statement in the function is return result;
This statement causes the function to end, and it sends the value of the result variable back to the
statement that called the function.
EXERCISE
1. Write a function that displays the message “Welcome”
2. Write a function that accepts a user’s name and number of times the user wants to be greeted. The
function should then displays the message “Welcome, users-name” as many times as the number
entered by the user. (Hint: use default arguments to set the number of times to 1)
3. Write a function to compute the product of two integer numbers.
4. Overload the function in example three to allow the program to compute product of two integers
and a floating-point number
5. Write a function to calculate the average of three numbers.
6. Overload the average function in exercise 5
ASSIGNMENT
1. The formula for converting a temperature from Fahrenheit to Celsius is
The program should ask for the following data if the patient was an out-patient:
Charges for hospital services (lab tests, etc.)
Hospital medication charges
The program should use two overloaded functions to calculate the total charges. One of the functions should
accept arguments for the in-patient data, while the other function accepts arguments for out-patient
information.
Both functions should return the total charges.