Programming Fundamentals
(COMP1112)
Function in C++
Course Instructor
Dr. Aftab Akram
PhD CS
Assistant Professor
Department of Information Sciences, Division of Science &
Technology
University of Education, Lahore
Functions
• A function is a block of code that performs a specific
task.
• Suppose we need to create a program to create a circle
and color it.
• We can create two functions to solve this problem:
• a function to draw the circle
• a function to color the circle
• Dividing a complex problem into smaller chunks makes
our program easy to understand and reusable.
• There are two types of function:
• Standard Library Functions: Predefined in C++
• User-defined Function: Created by users
C++ User-defined Function
• C++ allows the programmer to define their own
function.
• A user-defined function groups code to perform a
specific task and that group of code is given a name
(identifier).
• When the function is invoked from any part of the
program, it all executes the codes defined in the
body of the function.
Using Functions in C++
• Three things needed for using functions in C++:
• Function Prototype
• Function Definition
• Function Call
• Function Declaration is made before main().
• Function Definition is made after main().
• Function Call is made inside main().
• Function Declaration and Function Definition can
be combined, but it must be placed before
main().
C++ Function Prototype
• The syntax to declare a function prototype is:
• returnType functionName (parameter1,
parameter2,...);
• Here's an example of a function prototype
declaration without definition.
//function declaration (without definition)
void greet(void);
void add(int,int);
int addTwoNums(int,int);
• Note that at this stage, only need to mention input
datatypes. Exact names of variables are not
required.
C++ Function Prototype
C++ Function Prototype with
Definition
• The syntax to declare a function prototype is:
returnType functionName (parameter1, parameter2,...)
{
// function body
}
• Here's an example of a function prototype
declaration with and without definition.
// function declaration with definition
void greet() {
cout << "Hello World";
}
C++ Function Declaration
• Here,
• The name of the function is greet()
• The return type of the function is void
• The empty parentheses mean it doesn't have any
parameters or you can use void
• The function body is written inside {}
Calling a Function
• In the above program, we have declared a function
named greet().
• To use the greet() function, we need to call it.
• Calling a function, temporarily shifts control to the
called function and after its execution is completed
back to main().
• Here's how we can call the above greet() function.
int main() {
// calling a function
greet();
}
Calling a Function
Calling a Function
Function Parameters
• As mentioned above, a function can be declared
with parameters (arguments).
• A parameter is a value that is passed when calling a
function.
• For example, let us consider the function below:
void printNum(int num) {
cout << num;
}
Function Parameters
• We pass a value to the function parameter while
calling the function.
Function with Parameters
Function with Parameters
• In the above program, we have used a function that
has one int parameter and one double
parameter.
• We then pass num1 and num2 as arguments.
• These values are stored by the function parameters
n1 and n2 respectively.
Function with Parameters
return Statement
• In the above programs, we have used void in the function
declaration. For example,
void displayNumber() {
// code
}
• This means the function is not returning any value.
• It's also possible to return a value from a function.
• For this, we need to specify the returnType of the
function during function declaration.
• Then, the return statement can be used to return a
value from a function.
• For example,
int add (int a, int b) {
return (a + b);
}
return Statement
• Here, we have the data type int instead of void.
This means that the function returns an int value.
• The code return (a + b); returns the sum
of the two parameters as the function value.
• The return statement denotes that the function
has ended.
• Any code after return inside the function is not
executed.
return Statement
return Statement
• In the above program, the add() function is used
to find the sum of two numbers.
• We pass two int literals 100 and 78 while calling
the function.
• We store the returned value of the function in the
variable sum, and then we print it.
return Statement
Benefits of Using User-Defined
Functions
• Functions make the code reusable. We can declare
them once and use them multiple times.
• Functions make the program easier as each small
task is divided into a function.
• Functions increase readability.