Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
3 views28 pages

Programming With C++ Week Seven

Uploaded by

xahoxe9550
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views28 pages

Programming With C++ Week Seven

Uploaded by

xahoxe9550
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

Programming with C++

Lecturer:
Dr. Emmanuel Freeman. PhD, MSc, BSc.

Graduate Assistant:
Wonder Kwaku Susuassey (MSc.,BSc.)

1
Objectives
In this week you will:
• Learn Functions, Function Declarations and
Definitions
• Learn Argument passing, default arguments,
value return, and Function Names
• Understand Function Overload and Pointer
Functions
• Learn User-Defined Functions
3
Functions
• Functions in C++ are blocks of code
designed to perform specific tasks. They are
fundamental for organizing code, promoting
reusability, and making programs easier to
manage.
Types of Functions
• User-Defined Functions:
Created by programmers to perform specific
tasks.

• Standard Library Functions:


Pre-defined functions available in C++ libraries
(e.g., std::cout, std::sqrt).
Types of Functions
• Virtual Functions:
Functions in base classes that can be
overridden in derived classes, enabling
polymorphism.

• Inline Functions:
Functions where the compiler replaces the
function call with the function's code, potentially
improving performance for small functions.
Benefits of Functions

• Modularity: Break down complex programs


into smaller, manageable units.

• Reusability: Write code once and use it


multiple times.
Benefits of Functions

• Abstraction: Hide implementation details,


focusing on what the function does rather
than how it does it.

• Improved readability: Makes code easier to


understand and maintain.
Function Declaration
• Also known as a function prototype, it declares
the function's name, return type, and parameters
before the function is defined.

• It allows the compiler to verify correct function


calls.

• Syntax:
Function Declaration
#include <iostream>

using namespace std;

int main(){

cout << findsum(1, 2)<< endl;

return 0;

}
Function Definition

• A function definition is a function declaration in


which the body of the function is presented.

• Structure: A function definition includes a


return type, a name, a parameter list (optional),
and a body enclosed in curly braces.
Functions Definition - Structure

returnType functionName(parameter1, parameter2,


...) {

// Function body

// Statements to perform a task

return value; // Optional return statement

}
Function Definition - Keywords

• Return Type: Specifies the data type of the


value the function will return. Use if the
function doesn't return a value.

• Function Name: An identifier that uniquely


names the function.
Function Definition - Keywords

• Parameters: Input values passed to the


function, declared with their and .

• Function Body: Contains the code that


executes when the function is called.

• Return Statement: Used to send a value back


to the caller ( ).
Function Definition - Code
#include <iostream>

using namespace std;

int main(){

return 0;

}
Parameters
• Variables in the function definition that receive
the arguments.
• Syntax:
return_type name( ){
// Function body
return value;
}
Arguments
• Actual values passed to a function when it's
called.
• Syntax:
function_name( );
Example:
addNum(1, 2, 3);
Arguments
Other Arguments:
• References as Arguments
• Pointers as Arguments
• Arrays as Arguments
Default Arguments
• Actual values assigned to parameters during
function definition or declaration;
• Syntax:
• return_type function_name(type1
name1, type2 name2 = value)
Default Argument
#include <iostream>

using namespace std;

// Function with default arguments

int getSum(int x, int y = 20) {

return x+y;

.....
Default Argument
.....

int main() {

cout << getSum(5) << endl;

cout << getSum(5, 15);

return 0;

}
Function Calling
• To execute a function, you call it by its name,
followed by parentheses and any required
arguments.

• Syntax:
Function Overloading

• Defining multiple functions with the same


name but different parameters.

• Used in cases where there is a need for one


function name but differences in parameters.
Function Overloading

A function in C++ can be overloaded in three


different ways:

• By having different number of parameters.

• By having different types of parameters.

• By having both different number and types of


parameters.
Function Overloading - Case 1
#include <iostream>

using namespace std;

int add(int a, int b) {


return a + b;
}

int add( ) {
return a + b + c;
}
int main(){
return 0;
}
Function Overloading - Case 2
#include <iostream>

using namespace std;

int add(int a, int b) {


return a + b;
}

double add( a, b) {
return a + b;
}
int main(){
return 0;
}
Function Overloading - Case 3
#include <iostream>

using namespace std;

int add(int a, double b) {


return a + b;
}

double add( ) {
return a + b + c;
}
int main(){
return 0;
}
Week Seven Task

1. Rewrite given assignment on GPA calculator


to use functions.

2. Will be sent to the class rep for


communication.

28

You might also like