SlideShare a Scribd company logo
C++ Programming
Functions – Part #1
2
C++ Programming
Objectives
‱ Learn about standard (predefined) functions and
discover how to use them in a program
‱ Learn about user-defined functions
‱ Examine value-returning functions, including actual
and formal parameters
‱ Explore how to construct and use a value-returning,
user-defined function in a program
3
C++ Programming
Functions
problem
Subprobelm1 Subprobelm2 Subprobelm3
Subprobelm1.1 Subprobelm1.2
Subprobelm3.1 Subprobelm3.3
Subprobelm3.2
4
C++ Programming
output
input
Functions (Continued)
function
5
C++ Programming
8
3
2
Functions (Continued)
pow(x, y)
17
2
5x + 7
6
C++ Programming
‱Functions are like building blocks
‱They allow complicated programs to be
divided into manageable pieces
‱Some advantages of functions:
– A programmer can focus on just that part of
the program and construct it, debug it, and
perfect it
– Different people can work on different
functions simultaneously
– Can be used in more than one place in a
program or in different programs
Functions (Continued)
7
C++ Programming
‱ A function has a name.
‱A function may has argument(s).
‱A function may produce a result.
Functions (Continued)
8
C++ Programming
Predefined Functions
‱ In algebra, a function is defined as a rule
or correspondence between values, called
the function’s arguments, and the unique
value of the function associated with the
arguments
‱ If f(x) = 2x + 5, then f(1) = 7, f(2) = 9, and
f(3) = 11
‱ 1, 2, and 3 are arguments
‱ 7, 9, and 11 are the corresponding values
9
C++ Programming
Predefined Functions (Continued)
‱ Some of the predefined mathematical
functions are:
– sqrt(x)
– pow(x,y)
– floor(x)
– rand()
10
C++ Programming
Predefined Functions (Continued)
11
C++ Programming
Predefined Functions (Continued)
‱ Predefined functions are used in a
program as if they are single variables.
‱ When a function is used, the compiler
uses to the value that the function
produces and returns.
‱ A function value can be printed, used in
an assignment statement, or used as part
of an expression.
12
C++ Programming
Predefined Functions (Continued)
‱ cout << cos(3.1); print the value of
cos(3.1).
‱ y = 5*cos(3.1)–7; multiply cos(3.1) by 5
and subtract 7 to, then assign the result
to y.
‱ You cannot extract / assign a value to a
function.
cin >> cos(3.1); is illegal.
cos(3.1) = 5 * x + 2; is illegal.
13
C++ Programming
Predefined Functions (Continued)
‱ Predefined functions are organized into separate
libraries.
‱ To use a predefined function, the program must
include the header file that contains the
function’s specifications.
‱ To use the predefined math functions sqrt(),
pow(), 
 must have #include <cmath>
‱ To use the I/O functions, must have
#include <iostream>
‱ Example of another header file that you have
seen so far is 
.
14
C++ Programming
Predefined Functions (Continued)
‱ The function rand() generates random one of the
numbers 0, 1, 2, 3, 4, 

‱ The function rand() %2 generates 0 or 1
‱ The function 1 + rand() %6 generates 1, 2, 3, 4, 5, 6
‱ The function m + rand() % (n-m+1) generates a
random number in the interval [m, n]
‱ To use rand() use the library #include<cstdlib>
‱ To generate different random numbers use
srand(time(0)) on a line by itself
‱ To use time(0) use the library #include<ctime>
15
C++ Programming
Write a program that reads the (x, y) coordinates of a point in the Cartesian
coordinates, converts to polar coordinates (r,  ), and prints the polar
coordinates.
𝑟 = đ‘„2 + 𝑩2, 𝜃 = 𝑡𝑎𝑛−1
𝑩
đ‘„
Program: toPolar.cpp
Program: toPolar.exe
Program: CartesiantoPolar()
16
C++ Programming
User-Defined Functions
‱ User defined functions are like predefined
functions but the programmer has to write
them.
‱ Example:
– Find the max of two numbers.
17
C++ Programming
#include <iostream>
using namespace std;
double larger(double x, double y);
int main()
{
double num1, num2, maximum;
cin >> num1 >> num2;
maximum = larger(num1, num2);
cout << "The maiximum is " << maximum << endl;
return 0;
}
double larger(double x, double y)
{
double max;
if (x >= y)
max = x;
else
max = y;
return max;
}
18
C++ Programming
#include <iostream>
using namespace std;
double larger(double x, double y);
int main()
{
double num1, num2, largest;
cin >> num1 >> num2;
largest = larger(num1, num2);
cout << "The largest is " << largest << endl;
return 0;
}
double larger(double x, double y)
{ double max;
if (x >= y)
max = x;
else
max = y;
return max;
}
Function
header/heading
Function body
Function formal
parameters
Function actual
parameters
Function
type
Function prototype
Function definition
19
C++ Programming
User-Defined Functions (Continued)
‱ The number of formal parameter must
match the number of actual parameters
‱ The data type of each actual parameter
must match its corresponding formal
parameter.
‱ A function call in a program results in
the execution of the body of the called
function.
20
C++ Programming
User-Defined Functions (Continued)
‱ The function syntax:
functionType functionName(formal parameters)
{
statements
}
‱ Some functions have no parameters,
such as int main().
‱ Some functions return nothing, so the
return value is void.
21
C++ Programming
User-Defined Functions (Continued)
‱ A C++ program is made out of a set of
functions.
‱ main() is just a user defined function.
‱ Execution always begins at the first statement
in the function main.
‱ Other functions are executed only when they
are called.
22
C++ Programming
User-Defined Functions (Continued)
‱ When a return statement executes
– Function immediately terminates.
– Control goes back to the caller.
– The function call statement is replaced by
the returned value.
‱ When a return statement executes in the
function main(), the program terminates.
23
C++ Programming
User-Defined Functions (Continued)
‱ A user-defined function can be placed before
the function main. In that case the function
prototype is not necessary.
‱ A function that returns a value is called a value-
returning functions and it must have a data
type.
‱ A function that does not return a value (i.e.
does not have a return statement) is called a
void function and it does not have a type (its
type is void).
void funtionName(formal parameters)
24
C++ Programming
Write a program that reads a positive integer num, calls the function
isPerfect() which checks, and prints whether num is a perfect number or not.
A perfect number is a positive integer that is equal to the sum of its proper
divisors. For example, 6 is a perfect number because 6 = 1 + 2 + 3, also 28
is a perfect number because 28 = 1 + 2 + 4 + 7 + 14.
Program: isPerfect.cpp
Program: isPerfect.exe
Program: isPerfect()
25
C++ Programming
Write a program that reads a positive integer num, calls the function
isPrime() which checks, and prints whether num is a prime number or not.
A prime number is a positive integer that is divisible by 1 and itself only. For
example, 7, 11, 13, 17, 
 are prime numbers, while 15 = 3 * 5 is not a prime
number.
Program: isPrime.cpp
Program: isPrime.exe
Program: isPrime()
26
C++ Programming
Write a program that reads a positive integer N, calls the function Fibonacci()
which calculates, and prints the Nth Fibonacci number.
Fibonacci numbers satisfy the following recurrence relation
𝑎0 = 1, 𝑎1 = 1, 𝑎𝑛+2 = 𝑎𝑛+1 + 𝑎𝑛, 𝑛 0
Each Fibonacci number is the sum of the two previous Fibonacci numbers.
Program: Fibonacci.cpp
Program: Fibonacci.exe
Program: Fibonacci()
27
C++ Programming
Write a program that reads two positive integers n and k, calls the function
binomial() which calculates, and prints the binomial coefficient of n and k.
Fibonacci numbers satisfy the following recurrence relation
đ¶đ‘˜
𝑛
=
𝑛
𝑘
=
𝑛!
𝑛 − 𝑘 ! 𝑘!
Program: binomial.cpp
Program: binomial.exe
Program: binomial()
28
C++ Programming
Write a program that prints a menu and based on the user choice, the
program preforms a job.
Program: menu.cpp
Program: myLibrary.h
Program: menu.exe
Program: Menu

More Related Content

PDF
Programming Fundamentals Functions in C and types
PPT
chapter_2_-_basic_c_elements (1)_c++_c++_Wadee3.ppt
 
PDF
Functionssssssssssssssssssssssssssss.pdf
PPTX
Chp8_C++_Functions_Part2_User-defined functions.pptx
PPT
C++ functions
PPTX
Functions
PDF
5. Functions in C.pdf
PPT
Review chapter 1 2-3
Programming Fundamentals Functions in C and types
chapter_2_-_basic_c_elements (1)_c++_c++_Wadee3.ppt
 
Functionssssssssssssssssssssssssssss.pdf
Chp8_C++_Functions_Part2_User-defined functions.pptx
C++ functions
Functions
5. Functions in C.pdf
Review chapter 1 2-3

Similar to Programming For Engineers Functions - Part #1.pptx (20)

PPT
C++ Functions
PPT
C++ functions presentation by DHEERAJ KATARIA
PPTX
Functions in C-csvsvvcvxcvxvxcvxcvvsvsvsfvsfvd
PPT
Chap 5 c++
PPT
Material 3 (4).ppt this ppt is about the
PDF
Chap 5 c++
PDF
P3
 
PPTX
PPTX
Presentation on Function in C Programming
PDF
Introduction to cpp
PPTX
B.sc CSIT 2nd semester C++ Unit2
PPT
L4 functions
PPT
Functions in C++
PPT
Function
PPTX
PDF
Getting Started Cpp
PDF
Functions in Pythons UDF and Functions Concepts
PPT
eee2-day4-structures engineering college
PPTX
unit_2 (1).pptx
C++ Functions
C++ functions presentation by DHEERAJ KATARIA
Functions in C-csvsvvcvxcvxvxcvxcvvsvsvsfvsfvd
Chap 5 c++
Material 3 (4).ppt this ppt is about the
Chap 5 c++
P3
 
Presentation on Function in C Programming
Introduction to cpp
B.sc CSIT 2nd semester C++ Unit2
L4 functions
Functions in C++
Function
Getting Started Cpp
Functions in Pythons UDF and Functions Concepts
eee2-day4-structures engineering college
unit_2 (1).pptx
Ad

Recently uploaded (20)

PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PPTX
Odoo POS Development Services by CandidRoot Solutions
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PPTX
Online Work Permit System for Fast Permit Processing
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
 
PDF
Softaken Excel to vCard Converter Software.pdf
PDF
AI in Product Development-omnex systems
PPTX
ManageIQ - Sprint 268 Review - Slide Deck
PPTX
Introduction to Artificial Intelligence
PPTX
ai tools demonstartion for schools and inter college
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PDF
System and Network Administraation Chapter 3
PPTX
L1 - Introduction to python Backend.pptx
PDF
Digital Strategies for Manufacturing Companies
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PDF
PTS Company Brochure 2025 (1).pdf.......
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
How to Choose the Right IT Partner for Your Business in Malaysia
Wondershare Filmora 15 Crack With Activation Key [2025
Odoo POS Development Services by CandidRoot Solutions
VVF-Customer-Presentation2025-Ver1.9.pptx
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
Online Work Permit System for Fast Permit Processing
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
 
Softaken Excel to vCard Converter Software.pdf
AI in Product Development-omnex systems
ManageIQ - Sprint 268 Review - Slide Deck
Introduction to Artificial Intelligence
ai tools demonstartion for schools and inter college
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
System and Network Administraation Chapter 3
L1 - Introduction to python Backend.pptx
Digital Strategies for Manufacturing Companies
Design an Analysis of Algorithms II-SECS-1021-03
PTS Company Brochure 2025 (1).pdf.......
2025 Textile ERP Trends: SAP, Odoo & Oracle
Ad

Programming For Engineers Functions - Part #1.pptx

  • 2. 2 C++ Programming Objectives ‱ Learn about standard (predefined) functions and discover how to use them in a program ‱ Learn about user-defined functions ‱ Examine value-returning functions, including actual and formal parameters ‱ Explore how to construct and use a value-returning, user-defined function in a program
  • 3. 3 C++ Programming Functions problem Subprobelm1 Subprobelm2 Subprobelm3 Subprobelm1.1 Subprobelm1.2 Subprobelm3.1 Subprobelm3.3 Subprobelm3.2
  • 6. 6 C++ Programming ‱Functions are like building blocks ‱They allow complicated programs to be divided into manageable pieces ‱Some advantages of functions: – A programmer can focus on just that part of the program and construct it, debug it, and perfect it – Different people can work on different functions simultaneously – Can be used in more than one place in a program or in different programs Functions (Continued)
  • 7. 7 C++ Programming ‱ A function has a name. ‱A function may has argument(s). ‱A function may produce a result. Functions (Continued)
  • 8. 8 C++ Programming Predefined Functions ‱ In algebra, a function is defined as a rule or correspondence between values, called the function’s arguments, and the unique value of the function associated with the arguments ‱ If f(x) = 2x + 5, then f(1) = 7, f(2) = 9, and f(3) = 11 ‱ 1, 2, and 3 are arguments ‱ 7, 9, and 11 are the corresponding values
  • 9. 9 C++ Programming Predefined Functions (Continued) ‱ Some of the predefined mathematical functions are: – sqrt(x) – pow(x,y) – floor(x) – rand()
  • 11. 11 C++ Programming Predefined Functions (Continued) ‱ Predefined functions are used in a program as if they are single variables. ‱ When a function is used, the compiler uses to the value that the function produces and returns. ‱ A function value can be printed, used in an assignment statement, or used as part of an expression.
  • 12. 12 C++ Programming Predefined Functions (Continued) ‱ cout << cos(3.1); print the value of cos(3.1). ‱ y = 5*cos(3.1)–7; multiply cos(3.1) by 5 and subtract 7 to, then assign the result to y. ‱ You cannot extract / assign a value to a function. cin >> cos(3.1); is illegal. cos(3.1) = 5 * x + 2; is illegal.
  • 13. 13 C++ Programming Predefined Functions (Continued) ‱ Predefined functions are organized into separate libraries. ‱ To use a predefined function, the program must include the header file that contains the function’s specifications. ‱ To use the predefined math functions sqrt(), pow(), 
 must have #include <cmath> ‱ To use the I/O functions, must have #include <iostream> ‱ Example of another header file that you have seen so far is 
.
  • 14. 14 C++ Programming Predefined Functions (Continued) ‱ The function rand() generates random one of the numbers 0, 1, 2, 3, 4, 
 ‱ The function rand() %2 generates 0 or 1 ‱ The function 1 + rand() %6 generates 1, 2, 3, 4, 5, 6 ‱ The function m + rand() % (n-m+1) generates a random number in the interval [m, n] ‱ To use rand() use the library #include<cstdlib> ‱ To generate different random numbers use srand(time(0)) on a line by itself ‱ To use time(0) use the library #include<ctime>
  • 15. 15 C++ Programming Write a program that reads the (x, y) coordinates of a point in the Cartesian coordinates, converts to polar coordinates (r,  ), and prints the polar coordinates. 𝑟 = đ‘„2 + 𝑩2, 𝜃 = 𝑡𝑎𝑛−1 𝑩 đ‘„ Program: toPolar.cpp Program: toPolar.exe Program: CartesiantoPolar()
  • 16. 16 C++ Programming User-Defined Functions ‱ User defined functions are like predefined functions but the programmer has to write them. ‱ Example: – Find the max of two numbers.
  • 17. 17 C++ Programming #include <iostream> using namespace std; double larger(double x, double y); int main() { double num1, num2, maximum; cin >> num1 >> num2; maximum = larger(num1, num2); cout << "The maiximum is " << maximum << endl; return 0; } double larger(double x, double y) { double max; if (x >= y) max = x; else max = y; return max; }
  • 18. 18 C++ Programming #include <iostream> using namespace std; double larger(double x, double y); int main() { double num1, num2, largest; cin >> num1 >> num2; largest = larger(num1, num2); cout << "The largest is " << largest << endl; return 0; } double larger(double x, double y) { double max; if (x >= y) max = x; else max = y; return max; } Function header/heading Function body Function formal parameters Function actual parameters Function type Function prototype Function definition
  • 19. 19 C++ Programming User-Defined Functions (Continued) ‱ The number of formal parameter must match the number of actual parameters ‱ The data type of each actual parameter must match its corresponding formal parameter. ‱ A function call in a program results in the execution of the body of the called function.
  • 20. 20 C++ Programming User-Defined Functions (Continued) ‱ The function syntax: functionType functionName(formal parameters) { statements } ‱ Some functions have no parameters, such as int main(). ‱ Some functions return nothing, so the return value is void.
  • 21. 21 C++ Programming User-Defined Functions (Continued) ‱ A C++ program is made out of a set of functions. ‱ main() is just a user defined function. ‱ Execution always begins at the first statement in the function main. ‱ Other functions are executed only when they are called.
  • 22. 22 C++ Programming User-Defined Functions (Continued) ‱ When a return statement executes – Function immediately terminates. – Control goes back to the caller. – The function call statement is replaced by the returned value. ‱ When a return statement executes in the function main(), the program terminates.
  • 23. 23 C++ Programming User-Defined Functions (Continued) ‱ A user-defined function can be placed before the function main. In that case the function prototype is not necessary. ‱ A function that returns a value is called a value- returning functions and it must have a data type. ‱ A function that does not return a value (i.e. does not have a return statement) is called a void function and it does not have a type (its type is void). void funtionName(formal parameters)
  • 24. 24 C++ Programming Write a program that reads a positive integer num, calls the function isPerfect() which checks, and prints whether num is a perfect number or not. A perfect number is a positive integer that is equal to the sum of its proper divisors. For example, 6 is a perfect number because 6 = 1 + 2 + 3, also 28 is a perfect number because 28 = 1 + 2 + 4 + 7 + 14. Program: isPerfect.cpp Program: isPerfect.exe Program: isPerfect()
  • 25. 25 C++ Programming Write a program that reads a positive integer num, calls the function isPrime() which checks, and prints whether num is a prime number or not. A prime number is a positive integer that is divisible by 1 and itself only. For example, 7, 11, 13, 17, 
 are prime numbers, while 15 = 3 * 5 is not a prime number. Program: isPrime.cpp Program: isPrime.exe Program: isPrime()
  • 26. 26 C++ Programming Write a program that reads a positive integer N, calls the function Fibonacci() which calculates, and prints the Nth Fibonacci number. Fibonacci numbers satisfy the following recurrence relation 𝑎0 = 1, 𝑎1 = 1, 𝑎𝑛+2 = 𝑎𝑛+1 + 𝑎𝑛, 𝑛 0 Each Fibonacci number is the sum of the two previous Fibonacci numbers. Program: Fibonacci.cpp Program: Fibonacci.exe Program: Fibonacci()
  • 27. 27 C++ Programming Write a program that reads two positive integers n and k, calls the function binomial() which calculates, and prints the binomial coefficient of n and k. Fibonacci numbers satisfy the following recurrence relation đ¶đ‘˜ 𝑛 = 𝑛 𝑘 = 𝑛! 𝑛 − 𝑘 ! 𝑘! Program: binomial.cpp Program: binomial.exe Program: binomial()
  • 28. 28 C++ Programming Write a program that prints a menu and based on the user choice, the program preforms a job. Program: menu.cpp Program: myLibrary.h Program: menu.exe Program: Menu