0% found this document useful (0 votes)
7 views58 pages

Introduction to Programming Concepts

The document provides an introduction to programming, defining it as the process of solving problems using computers through programming languages. It outlines essential skills for programmers, techniques for problem-solving, types of programming languages, and the compilation process of C++ programs. Additionally, it covers fundamental data types, variable declaration and initialization, and operators in programming.

Uploaded by

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

Introduction to Programming Concepts

The document provides an introduction to programming, defining it as the process of solving problems using computers through programming languages. It outlines essential skills for programmers, techniques for problem-solving, types of programming languages, and the compilation process of C++ programs. Additionally, it covers fundamental data types, variable declaration and initialization, and operators in programming.

Uploaded by

tage198927
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Chapter One: Introduction

DEFINITION:

PROGRAMMING: is fundamentally the process of solving a problem

using computers.

It involves giving a machine the necessary instructions (a program)


and inputs to perform a particular task.

program is a detailed, executed set of instructions written in a


programming language that directs the computer to solve a problem
in a precise order until completion.

it is the method of communicating with a computer to tell it what to do.


A Programming Language: is an artificial language—a
set of written symbols—that expresses computations and
instructs computer hardware to perform specific operations.

Is a set different category of written symbols that instruct

computer hardware to perform specified operations


required by the designer.

What skills do we need to be a programmer ?


 Problem Solving Skill: The ability to solve a real-world
problem and represent the solution logically
 Algorithm Development: The skill to create a sequence
of simple, understandable steps that show the method of
solving a problem.
 An algorithm: is a finite sequence of steps in a specific
logical order
Techniques of problem solving are:
1. Pseudocode:

2. Flowcharting

3 Algorithm

1. Pseudocode:
 A semiformal, English-like language used to design and describe the procedural
logic of an algorithm in a simple, easy-to-understand way

A pseudocode can be used for:

 Designing algorithms

 Communicating algorithms to users

 Debugging logic errors in program

 Documenting programs for future maintenance and expansion purposes


2. Flowcharting:

 A flowchart is a graph consisting of geometrical shapes that are


connected by flow lines.

 The geometrical shapes in a flowchart represent the types of


statements in an algorithm.

 The details of statements are written inside the shapes


3. Algorithm: An algorithm is a sequence of a finite number of steps
arranged in a specific logical order which is used to develop the solution for
a problem.
 An algorithm must satisfy the following requirements:
 Unambiguousness: It must not be ambiguous.
 Generality: It must have generality.
 Correctness: It must be correct and must solve the problem for which
it is designed.
 Finiteness: It must execute its steps and terminate in finite time.
In general, algorithms have input and produce some output.

 Types [Levels] of Programming Languages


1. Low-level languages
2. High-level languages
1. Low-level languages:
 Low-level languages are closely tied to the computer hardware and architecture,
offering high efficiency but low portability.

 A program written in a low-level language can be extremely efficient,

making optimum use of both computer memory and processing time.

 Characteristics of LOW Level Languages:

 Machine Oriented: An assembly language program written for one machine


will generally not work on any other type of machine unless they happen to
use the same processor chip.

 Highly Efficient: Programs written in a low-level language are typically


extremely efficient, making optimum use of both computer memory and
processing time.
 Difficult and Time-Consuming to Create:

2. High-level languages:

 They are not machine oriented: they are portable, meaning that a program
written for one machine will run on any other machine for which the
appropriate compiler or interpreter is available.

 High-level languages are designed to be more understandable to humans and


are independent of the specific computer.

 Characteristics of HIGH Level Languages:

 Machine Independence (Portability):A program written for one machine is


generally portable and can run on another, provided the appropriate
language translator (compiler or interpreter) is available.
 Human Readability: They use statements that resemble English
sentences or common mathematical expressions, making them
much easier to read, understand, and use than low-level languages.

Assembly level language


Low level language
MOV AX, 01
0111 0001 0000 1111 MOV BX, 02
1001 1101 1011 0001 ADD AX, BX
1110 0001 0011 1110

Generations of programming languages


Generation Name Key Features

This is the lowest possible level of language and the


only one that a computer can actually understand
1st Generation Machine Language and execute directly.
It consists of binary code (0s and 1s).
It executes directly without translation.

Developed to address the difficulty of machine


code , it uses symbolic codes or mnemonics (e.g.,
MOV AX, 01) instead of binary instructions.
2nd Generation Assembly Language Each line of code generally corresponds to one
machine code instruction.
It requires a translator called an assembler to
become executable
Generation Name Key Features

These are independent of the particular


computer and are not machine oriented.
They are portable, meaning a program written
for one machine can run on another, provided a
3rd
High Level Languages compiler or interpreter is available.
Generation
Their statements often resemble English
sentences or mathematical expressions13.
Examples include PASCAL (general-purpose)
and FORTRAN (mathematical problems).

These are modern high-level languages where


the user concentrates on what is to be done,
4th Fourth Generation Languages
leaving the how part to the 4GL.
Generation (4GLs)
They permit the faster development of large
programs.

These are used in Artificial Intelligence (AI)


and expert systems.
5th
Fifth Generation Languages
Generation (5GLs) They are considered "natural" languages
whose instructions closely resemble human
speech.
Language Translators

 Language Translators:- refers to the system software programs responsible for


converting a program written in a human-readable language (like Assembly or a
High-Level Language) into the computer's native Machine Code (binary 0s and
1s), which is the only language the computer hardware can directly execute.

 There are three main types of language translators: Assemblers, Compilers, and
Interpreters.
1. Compilers

A Compiler: is a program that reads an entire program written in a high-level


language (source code) and converts the entire program into an executable
machine code file (object code) before the program runs.
N.B Source code : Is the code that is input a translator.
Object code : is the code that is output from the translator.
Advantages Disadvantages

 Fast in Execution: Once  Harder to Debug: Debugging a


compiled, the resulting object compiled program is much harder,
code runs very quickly. and they are "not so good at
finding errors" compared to
interpreters.

 No Re-compilation Needed: The  Slower Development Cycle: When an


executable object program can be used error is found, the whole program has
whenever required without the need to to be re-compiled to apply the fix.
re-compile it.

 Standalone Execution: The resulting


executable code can be distributed or
executed without having to have the
compiler program present.

Examples: C++, Pascal, FORTRAN.


2. Interpreters
An Interpreter is a program that translates the high-level source code and converts
Advantages Disadvantages
and executes it one line at a time. It does not produce a separate object code file.
 Slow Execution: Execution of an
 Easier Debugging: They are "good at interpreted program is much slower than
locating errors in programs". Debugging for a compiled program because the
is easier because the interpreter stops interpreter has to translate each line every
when it encounters an error. time it is run.

 Faster Development Cycle: If an error  Requires Interpreter to Run: No object


is detected, there is no need to code is produced, and the interpreter must
retranslate the whole program, which be present (along with the user's
"enormously speed[s] up the program) in memory every time the
development and testing process". program is run.

 Repetitive Translation: If a section of


 Interactive Programming: Programs
code (like a loop) runs 1000 times, that
that are interpreted "lend themselves to
section is translated into machine code
interactive programming".
1000 times.
C++ Program Compilation

 The process of C++ program compilation is a multi-step procedure that


translates the code written by a programmer into an executable file that the
computer can run.

 The overall process typically involves five phases: Edit, Preprocess, Compile,
Link, and Load.
edi preprocess compile Link Loa
t d

 c++ Program Compilation Phases


1. Edit (Source Code Creation):
 The programmer uses a text editor to type the C++ statements, which are called the
source code.
 This code is saved to a source file, typically with a .cpp extension.
2. Preprocess:
 A program called the preprocessor automatically executes before the compiler
begins.
 It reads the source code and carries out commands called preprocessor directives.
 The result is a modified program text that no longer contains any directives.
3. Compile (Translation):
 The C++ compiler translates the modified program text into machine language
instructions.
 If no syntax errors (violations of language rules) are found, the compiler stores these
instructions, called object code, in an object file.
4. Link (Executable Creation):
 A program called the linker combines the incomplete object file with the necessary
prewritten code from the run-time library.
5. Load (Execution):
 The loader takes the executable file from the disk and transfers it into the computer's
memory, allowing the program to run.
Chapter Two: Basics of
Programming
1. The Programming Environment
Structure of a C++ Program: Every C++ program adheres to a specific
structure, typically including preprocessor directives (#include), a main
function ( int main()), and statements terminated by a semicolon.
Understanding this template is the first step toward writing any executable
code.
// C++ Program to print "Hello, World!" to the screen.

#include <iostream>
using namespace std;

int main()
{
// The cout object is used to display output.
cout << "Hello, World!" << endl;
}
#include <iostream> : the C++ preprocessor to include the contents of the iostream library .
Tells

Iostreame : means input , output steam I stands input , o stands output.


using namespace std : Using Directive: This line simplifies the code by telling the
compiler to look for names (like cout and endl) in the standard namespace (std).
int main() :Function Header: This is the entry point of every C++ programe.
the main function is where execution always begins.
{ - Opening Brace: Marks the beginning of the main function body.
cout << "Hello, World!" << endl; Output Statement: This is the action line.
is the standard output object (console output).
<< -operator (called the insertion operator) sends the literal string
“”Hello world
” to the screen.
Endl - inserts a newline character, moving the cursor to the next line.
returne 0; -Return Statement: This signals the operating system that the
2. The Language Vocabulary and Syntax
Keywords: These are special, reserved words in C++ like ( int , if, for , while )
that have predefined meanings and cannot be used by the programmer for
naming variables or functions.

Identifiers: These are the names the programmer chooses for things like
variables,
 Rules for Creating Identifiers
1. Valid Starting Characters: An identifier must begin with either a letter (A-Z or
a-z) or an
underscore(_).
2. Valid Subsequent Characters: After the first character, an identifier can contain
letters,
digits, or the underscore character.
3. No Reserved Keywords: Identifiers cannot be the same as any of the reserved
Example Validity Reason
keywords
studentName Valid Starts with a letter, contains letters.
in c++.
_temp_value Valid Starts with an underscore, contains letters and underscores.
4. No Spaces: Identifiers cannot contain any white space (spaces or tabs).
Starts with a letter, contains letters and a digit (not as the first
totalSum1 Valid
character).
1stPlace Invalid Starts with a digit (1).
for Invalid Is a reserved C++ keyword.
num Count Invalid Contains a space.
 Comments: Lines of text that are included in the source code but are ignored by
the compiler. They are essential for providing notes, documentation,
and explanations for programmers to understand or maintain the code.
C++ supports two ways to insert comments:
o // line comment
o /* block comment */
 Input and output ( cin and cout ): These are the primary mechanisms for interaction:
 Cout (Console Output) is used to display information (text, numbers, results) to the
user's screen.
Ex. cout << "Hello"; // prints Hello on screen
cout << Hello; // prints the content of Hello variable on screen
 Cin (Console Input): is used to read and store data that the user types into the
program.
Ex. cin >> age;
Fundamental Data Types in C++
1. Integer Types
These types are used to store whole numbers (numbers without a fractional
part)

// 1. Integer Types
cout << "Size of char: " << sizeof(char) << " byte(s)" << endl;
cout << "Size of short: " << sizeof(short) << " byte(s)" << endl;
cout << "Size of int: " << sizeof(int) << " byte(s)" << endl;
cout << "Size of long: " << sizeof(long) << " byte(s)" << endl;
cout << "Size of long long: " << sizeof(long long) << " byte(s)" << endl;
2. Floating-Point Types (Real
Numbers)

// 2. Floating-Point Types
cout << "Size of float: " << sizeof(float) << " byte(s)" << endl;
cout << "Size of double :" << sizeof(double) << " byte(s)" << endl;
cout << "Size of long double: " << sizeof(long double) << " byte(s)" << endl;
cout << "Size of bool: " << sizeof(bool) << " byte(s)" << endl;
//C++ program to demonstrate the 'sizeof' operator and display the memory size
// (in bytes) of several fundamental data types.

#include <iostream>
using namespace std;

int main()
{
cout << "--- Fundamental Data Type Sizes ---" << endl;
cout << "Size of char:\t\t" << sizeof(char) << " byte(s)" << endl;
cout << "Size of short:\t\t" << sizeof(short) << " byte(s)" << endl;
cout << "Size of int:\t\t" << sizeof(int) << " byte(s)" << endl;
cout << "Size of long:\t\t" << sizeof(long) << " byte(s)" << endl;
cout << "Size of long long:\t" << sizeof(long long) << " byte(s)" << endl;
cout << "Size of float:\t\t" << sizeof(float) << " byte(s)" << endl;
cout << "Size of double:\t\t" << sizeof(double) << " byte(s)" << endl;
cout << "Size of long double:\t" << sizeof(long double) << " byte(s)" << endl;
cout << "Size of bool:\t\t" << sizeof(bool) << " byte(s)" << endl;

return 0;
}
1. Variable Declaration
 Declaration is the act of telling the compiler two things: the name of the
variable and the data type of the data it will hold.
Syntax:
int num1;
int score;

2. Variable Initialization
 Initialization is the act of giving a variable its first, initial value. This should
ideally be done immediately upon declaration to prevent the use of garbage
values. #include <iostream>
using namespace std;
int main()
syntax {
int score; //declaration only
int count = 0;
score = 95; // initialization
double pi = 3.14159; cout << "Score initialized later:\t" << score << endl ;
return 0;
}
 Operators
 An operator is a special symbol used to perform an operation (like calculation or
comparison) on values (operands) and produce a result.
Category Purpose Key Operators

Stores a value into a


Assignment =
variable.

Performs mathematical +, -, *, /, %
Arithmetic
calculations. (Modulus/Remainder)

Compares two values;


Relational returns true (1) or false ==, !=, >, <, >=, <=
(0).

Increment and
++, --
decrement

Combines Boolean
Logical && (AND), **`
results.
Programming Errors

Programming Errors addresses the common mistakes programmers make when


writing code. These errors are classified into three main types based on when and how
the compiler or program detects them:
 Syntax Errors (Compile-Time
Errors
 Run-Time Errors
 Logic Errors
1. Syntax Errors:
 A Syntax Error is a violation of the grammatical rules of the programming
language.
 Referred to as Compile-Time Errors because they are detected by the compiler
(or
interpreter) during the translation phases.
2. Run-Time Errors
 A Run-Time Error is an error that occurs while the program is
actually running (executing). The compiler is unable to detect
these errors because the program's logic is technically sound, but
the operation being requested is impossible or illegal during
execution.
Ex. Division by Zero ;

3. Logic Errors
A Logic Error is an error in the design or implementation of the
algorithm. The program compiles and runs successfully without
crashing, but it produces incorrect or unexpected results because
the programmer's steps (the logic) are flawed.
#include <iostream>
using namespace std;

int main()

{
int numerator = 10;
int denominator = 0;
int val1 = 3;
int val2 = 5;
int final_result = val1 + val2 * 2;
cout << "--- Logic Error Output ---" << endl;
cout << "Result should be 16, but is: " << final_result << endl;

return 0;
}
Chapter Three: Control Statements
3. Control Statement
 A Control Statement (or Control Structure) is a programming
construct that allows a programmer to specify the order of execution
of code statements.
 They dictate the flow of control within a program, moving beyond
simple,
sequential, top-to-bottom execution.

 C++, control statements are grouped into two primary categories:


Selection (Decision) and Repetition (Looping).
3.1 The if statement
 The if statement can cause other statements to execute only
under certain conditions
 These statements allow a program to choose between two or
// This program averages 3 test scores.
#include <iostream>
int main()
{
int score1, score2, score3;
double average;
cout << "Enter 3 test scores and I will average them: ";
cin >> score1 >> score2 >> score3;
average = (score1 + score2 + score3) / 3.0;
cout << "Your average is " << average << endl;
if (average == 100)
{
cout << "Congratulations! ";
cout << "That's a perfect score!\n";
}
return 0;
}
3.2 The if/else statement
 The if/else statement will execute one set of statements
when the if expression is true, and another set when the
expression is false. #include <iostream>
using namesace std;
syntax flowchart
int main()
{
int number;
cout << "Enter an integer and I will tell you if it\
n";
cout << "is odd or even. ";
cin >> number;
if (number % 2 == 0)
cout << number << " is even.\n";
else
cout << number << " is odd.\n";
return 0;
}
3.3 The if/else --- if statement

 The if/else if statement is a chain of if statements. They


perform their tests, one after the
other, until one of them is found to be true.
flowchart
Syntaxs
// This program uses an if/else if statement to assign a letter grade (A, B, C, D, or
F) to
a numeric
// test score.
#include <iostream.h>
int main()
{
int testScore;
char grade;
cout << "Enter your numeric test score and I will\n";
cout << "tell you the letter grade you earned: ";
cin >> testScore;
if (testScore < 60)
grade = 'F';
else if (testScore < 70)
grade = 'D';
else if (testScore < 80)
grade = 'C';
else if (testScore < 90)
grade = 'B';
else if (testScore <= 100)
grade = 'A';
cout << "Your grade is " << grade << ".\n";
return 0;
#include <iostream.h>
int main()
3.3.1 Using a trailing else {
int testScore;
char grade;
 A trailing else, placed at the end of int goodScore = 1;
cout << "Enter your numeric test score and I will\n";
cout << "tell you the letter grade you earned: ";
an if/else if statement, provides a cin >> testScore;
if (testScore < 60)
grade = 'F';
default set of actions else if (testScore < 70)
grade = 'D';
else if (testScore < 80)
when none of the if expressions are grade = 'C';
else if (testScore < 90)
grade = 'B';
true. else if (testScore <= 100)
Computer Programming Module
54
grade = 'A';
else
goodScore = 0;
if (goodScore)
cout << "Your grade is " << grade << ".\n";
else
{
cout << testScore << " is an invalid score.\n";
cout << "Please enter a score that is no greater than
100.\n";
}
return 0;
}
3.3.2 Nested if Statements
 A nested if statement is an if statement in the conditionally
executed code of another if statement.
 Anytime an if statement appears inside another if statement, it
is considered nested//#include
. This program demonstrates a nested if statement.
<iostream.h>
int main()
{
char employed, recentGrad;
cout << "Answer the following questions with either Y for Yes or N for No.\n";
cout << "Are you employed? ";
cin >> employed;
cout << "Have you graduated from college in the past two years? ";
cin >> recentGrad;
if (employed == 'Y')
Computer Programming Module
55
{
if (recentGrad == 'Y')
{
cout << "You qualify for the special ";
cout << "interest rate.\n";
}
}
return 0;
}
3.4 The switch statement
 The switch statement lets the value of a variable or expression determine
where the program
will branch to. A branch occurs when one part of a program causes another
part to execute.
The first line of the statement starts with the word switch, followed by an
integer expression inside parentheses. This can be either of the following:
o A variable of any of the integer data types (including char)
o An expression whose value is of any of the integer data types
 On the next line is the beginning of a block containing several case
statements.
include <iostream>
using namespace std;

int main()
{
int dayNumber;

cout << "--- Day of the Week Lookup ---" << endl;
cout << "Enter a number (1-7) to see the day: ";
cin >> dayNumber;

{
case 1:
cout << "Day 1 is: Monday" << endl;
break; // Essential: Exits the switch after a match
case 2:
cout << "Day 2 is: Tuesday" << endl;
break;
case 3:
cout << "Day 3 is: Wednesday" << endl;
break;
case 4:
cout << "Day 4 is: Thursday" << endl;
break;
case 5:
cout << "Day 5 is: Friday" << endl;
break;
case 6:
cout << "Day 6 is: Saturday" << endl;
break;
case 7:
cout << "Day 7 is: Sunday" << endl;
break;
default:
cout << "Error: You entered a number outside the 1-7 range." << endl;
break;
}
return 0;
}
3.5 Introduction to Loops
 A loop is a control structure that causes a statement or group of statements to
repeat.
 C++ has three looping control structures: the while loop, the do-while loop, and the
for loop.
 The difference between each of these is how they control the repetition

3.5.1 The while Loop


 The condition is evaluated before the body of the loop is executed.
 If the condition is false on the very first check, the code inside the loop will
never run.
 It is known as an entry-controlled loop.
 Checks the condition first, then executes the code block if the condition is true.
"Check, then execute."
#include <iostream>
using namespace std;

int main()
{
int i= 1;

while (i <= 5)
{
cout << "Current count: " << i<< endl;

i++;
}

cout << "Finished counting! Loop has terminated."


<< endl;

return 0;
}
3.5.2 The do-while Loop
 The body of the loop is executed first, and then the condition is evaluated.
 This guarantees that the code inside the loop will run at least once, even if the
condition is initially false.

syntax
•"Check, then execute."
•do-while loop: Executes the code block first, then checks the condition.
•"Execute once, then check."

#include <iostream>
using namespace std;
Difference while loop and do
int main()
while loop
{
while loop: Checks the int i = 1;

condition first, then executes the


do
code block if the condition is true.
{
Check, then execute." i++;
}
•do-while loop: Executes the code while (i <= 5);
cout << "Finished counting! Loop has terminated." <<
block first, then checks the
endl;
condition.
return 0;
• "Execute once, then check." }
3.5.3 The for Loop
The for Loop : is ideal for situations that require a counter
because it has built-in expressions that initialize and update variables.
Here is the format of the for loop. Flow diagram
It works the following steps:
Syntax:
1. initialization is executed.
for (initialization; test; update)
2. test is checked, if it is true the
{
loop continues, otherwise the
statement;
loop finishes and statement is
statement;
skipped.
}
3. statement is executed.
4. finally, whatever is specified
in the update field is executed
and the loop gets back to step
Example

#include <iostream> #include <iostream>


using namespace std;
int main() {
// Print numbers 0 to 4 int main() {
for (int i = 0; i < 5; i++) cout << "Even numbers from 1 to
{ 10:" << endl;
cout<<i<<endl;
} for (int i = 2; i <= 10; i = i + 2) {
return 0; cout << i << " ";
} }

return 0;
}
Nested for loops

 Nested for loops mean having one for loop inside another for
loop.
 This is very useful for working with grids, tables, or multi-
dimensional data.
Example : Star Pattern #include <iostream>
using namespace std;

int main() {
for (int row = 1; row <= 4; row++) Output:
{
for (int col = 1; col <= row; col+ *
+) { **
cout << "* "; ***
} ****
cout << endl;
}
return 0;
Continue and Break statement
 break and continue are control statements used to change
the normal flow of loops. Let me explain them clearly with
simple examples.
Break Statement
The break statement immediately terminates the loop and exits it
completely.
#include <iostream> //Break in For Loop

using namespace std;


int main() {
for (int i = 1; i <= 10; i++) {
if (i == 5) { output
break; // Exit loop when i becomes 5
} 1234
cout << i << " ";
Loop ended!
}
cout << "\nLoop ended!";
return 0;
}
The continue statement skips the current iteration and moves to the next iteration of the loop.
Syntax:

Continue Statement
The continue statement skips the current iteration and moves to the
next iteration of the loop.

#include <iostream>
#include <iostream>
using namespace std;
using namespace std;
int main() {
int main() {
for (int i = 1; i <= 10; i++) {
for (int i = 1; i <= 10; i++) {
if (i % 2 == 0) { // If number is even
if (i == 5) {
continue; // Skip even numbers
continue; // Skip iteration when i is
}
5
cout << i << " "; // Only odd numbers reach
}
here
cout << i << " ";
}
}
return 0;
return 0;
}
}
Feature break continue
Terminates the entire Skips only current
Action
loop iteration
Exits the loop
Flow Jumps to next iteration
completely
When you want to stop When you want to skip
When to use
the loop entirely specific cases
Loop continues with next
Result Loop ends prematurely
value
Chapter 4: Function and Passing
argument to
function
4.1 Definitions of Functions
 A function is a block of instructions that is executed when it is called from
some other point of the program.
 A function is a collection of statements that performs a specific task
There are two types of functions
1. Built-in Functions
2. User-defined Functions
1. built-in function
 Functions that are pre-written and come with the programming language
itself.
 They are stored in libraries and can be used by including the appropriate
header files.
2. User-defined Functions
Functions that are created by the programmer to perform
specific tasks required in their particular program.

Basis Built-in Functions User-defined Functions

Pre-defined in language
Definition Defined by programmer
libraries

Source Come with compiler Written in source code

Can be modified as
Modification Cannot be modified
needed

Purpose General utilities Specific to program needs

Location Header files Program source files


#include <iostream>
#include <iostream> using namespace std;
using namespace std;
// USER-DEFINED FUNCTION to add two numbers
int main() { int addNumbers(int a, int b) {
int a, b, c; return a + b;
}
cout << "Enter first number (a): ";
int main() {
cin >> a;
int a, b, c;

cout << "Enter second number (b): "; cout << "Enter first number (a): ";
cin >> b; cin >> a;

// Using built-in + operator (this is a built-in cout << "Enter second number (b): ";
function) cin >> b;
c = a + b;
// Using our user-defined function
c = addNumbers(a, b);
cout << "Using built-in + operator:" << endl;
cout << a << " + " << b << " = " << c << endl; cout << "Using user-defined function:" << endl;
cout << a << " + " << b << " = " << c << endl;
return 0;
} return 0;
}
4.2 Declaration and calling functions
 A function call is a statement that causes a function to execute.
 A function definition contains the statements that make up the function.
When creating a function, you must write its definition
All function definitions have the following parts
Name: Every function must have a name.
Parameter list: The program module that calls a function can send
data to it
Body: The body of a function is the set of statements that carry
out the task the function is performing
Return type: A function can send a value back to the program
module that called it

You might also like