Programming
Fundamental
s
By Engr. Sher Mohammad
Contents of Lecture
• Functions
• Why functions?
• Types of Functions
• Standard Library/Built-in Functions
• User defined Functions
• Declaration/Prototyping and Definition of functions
• Function Parameters and Arguments
• Function Overloading
Functions
• A group of statements that takes input, processes it, and returns an
output.
• The idea behind a function is to combine common tasks that are done
repeatedly.
• If you have different inputs, you will not write the same code again. You
will simply call the function with a different set of data called
parameters.
• Each C++ program has at least one function, the main() function.
• You can divide your code into different functions.
• This division should be such that every function does a specific task.
Functions
• A function has:
– A name
– Zero or more input parameters
– 0 or 1 return (output) values
• We only specify the type
• The signature (or prototype) of a function
specifies these aspects so others know how
to "call" the function
Why use
functions?
• Code Organization
• Elimination of Duplicates
• Easy Testing and Debugging
• Readability
• Code Interoperability.
• Code Reusability
Function Definition and Prototype/Declaration
#include <iostream>
• A user-defined function refers to a using namespace std;
function that users create/ define / / prototype / declaratoin
i n t max1(int a, i n t b);
on their own to meet the needs of
int main()
the program. {
int x, y;
• In doing so, the user must specify cin >> x >> y;
the name, parameters, and return max1(5,6);
/* Code for main */
type in C++.
}
• This function can then be invoked
from any section of the program / / Defi nition
int max1(int a, int b)
to carry out a certain job. {
if(a > b)
return a;
else
return b;
}
Declaration/
Prototype
• Function declaration refers to the act of
creating a function, which may then be
called upon from other parts of the
program.
• The function declaration consists of the
name of the function, any applicable
argument data types, and any applicable
return type.
• Good practice is to "declare" your
function by placing the
prototype/signature at the top of your
code.
Example
#include <iostream>
Using namespace std;
int sum(int num1, int num2); // Function declaration
int main( ) {
int a = 5;
int b = 3;
// Function call
int result = sum(a, b);
cout << "The sum of " << a << " and " << b << " is: " << result << endl;
return 0;
}
// Function definition
int sum(int num1, int num2) {
return num1 + num2;
}
Definition
"Define" the function (actual code implementation) anywhere by placing the
code in { }.
Example
#include <iostream>
int multiply(int num1, int num2); // Declaration specifies return type, function name, and
parameters
int main( ) {
int a = 5;
int b = 3;
int result = multiply(a, b); // Function call
cout << "The product of " << a << " and " << b << " is: " << result << endl;
return 0;
}
int multiply(int num1, int num2) { // Definition includes return type, function name, and parameters
int product = num1 * num2;
return product;
}
Types of Function: User defined
Functions # include <iostream>
• A user-defined function refers to a using namespace std;
function that users create/ define int max (int a, int b)
on their own to meet the needs of {
if(a > b)
the program. return a;
else
• In doing so, the user must specify return b;
the name, parameters, and return }
type in C++. int main()
{
• This function can then be invoked int x, y, mx;
from any section of the program cin >> x >> y;
/* Code for main */
to carry out a certain job.
}
Type Of C++ Functions Based On Return Value
• Void Function In C++ (Function With No
Return Value)
• Functions that don't return a value are #include <iostream>
Using namespace std;
known as void functions.
void displayMessage() {
• They can execute a sequence of statements cout << “Void function." << endl;
or carry out a specified operation without }
having to deliver a result.
int main() {
• Void functions are frequently used for tasks displayMessage();
return 0;
like output display or carrying out a }
sequence of instructions etc.
C++ Functions With Return
Values
• When you wish to carry
out a certain activity
and produce a value as
a result of that
operation, you utilize
functions with return
values.
• These functions may
utilize fundamental
data types (int, float,
etc.), user-defined
types, or even pointers
as their data types.
Default Arguments
Default Arguments
Example
#include <iostream>
Using namespace std;
int addNumbers(int a, int b) {
int sum = a + b;
return sum;
}
int main( ) {
int result = addNumbers(5, 3);
cout << "The sum is: " << result << endl;
return 0;
}
Example 1
Example 2
C++ Standard Library Functions
(In-built Functions)
• There are a number of built-in functions included in the standard library of C++.
• These functions are present in the corresponding header files and offer a variety of
functionality.
• <cmath>
• <ctime>
• <cstdio>
• <cstring>
• <cctype>
• <iostream>
• <cstdlib>
Example 1:
#include <iostream>
#include <cmath>
int main() {
double number = 16.0;
double squareRoot = sqrt(number);
cout << "The square root of " << number << " is: " << squareRoot
<< endl;
return 0;
}
Example 2:
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double x = 7.5, y = 2.1;
double result = remainder(x, y);
cout << "Remainder of " << x << "/" << y << " = " << result << endl;
x = -17.50, y=2.0;
result = remainder(x, y);
cout << "Remainder of " << x << "/" << y << " = " << result << endl;
y=0;
result = remainder(x, y);
cout << "Remainder of " << x << "/" << y << " = " << result << endl;
return 0;
}
Function Overloading
• The C++ language's function overloading feature enables the coexistence of
many functions with the same name but various arguments or argument types.
• Function overloading allows you to
create functions that carry out
equivalent actions on several data
types or in different argument
configurations.
• This enhances the readability, reuse,
and flexibility of the code
Syntax
return_type function_name(parameter_list_1) {
// Function implementation
}
return_type function_name(parameter_list_2) {
// Function implementation
}
// Additional overloaded functions with different parameter lists
Example
#include <iostream>
Using namespace std;
int sum(int a, int b) {
return a + b; }
int sum(int a, int b, int c) {
return a + b + c; }
int main() {
int result1 = sum(5, 10); // Call the first overloaded function
cout << "Sum of two integers: " << result1 << endl;
int result2 = sum(2, 4, 6); // Call the second overloaded function
cout << "Sum of three integers: " << result2 << endl;
return 0;
}
ClassWork
• A person is eligible to vote if his/her age is greater than or equal to 18. Define a
function to find out if he/she is eligible to vote.
• Define two functions to print the maximum and the minimum number respectively
among three numbers entered by user.
• Define a function that returns the product of two numbers entered by user.
• Write a program that calculates 6^5. Declare your own function to do this.
• Write a C++ program that take a number from user then output the square of this
number.