SlideShare a Scribd company logo
Introduction to
Functions
Programming
og Fundamentals I: Introduction to Functions /Slide 2
Introduction to Functions
A complex problem is often easier to solve
by dividing it into several smaller parts,
each of which can be solved by itself.
This is called structured programming.
These parts are sometimes made into
functions in C++.
main() then uses these functions to
solve the original problem.
og Fundamentals I: Introduction to Functions /Slide 3
C++ Functions
C++ allows the use of both internal (user-
defined) and external functions.
External functions (e.g., abs, ceil, rand,
sqrt, etc.) are usually grouped into
specialized libraries (e.g., iostream,
stdlib, math, etc.)
og Fundamentals I: Introduction to Functions /Slide 4
Advantages of Functions
Functions separate the concept (what is
done) from the implementation (how it is
done).
Functions make programs easier to
understand.
Functions can be called several times in
the same program, allowing the code to be
reused.
og Fundamentals I: Introduction to Functions /Slide 5
User-Defined Functions
C++ programs usually have the following form:
// include statements
// function prototypes
// main() function
// function definitions
og Fundamentals I: Introduction to Functions /Slide 6
Function Input and Output
og Fundamentals I: Introduction to Functions /Slide 7
Function Definition
A function definition has the following syntax:
<type> <function name>(<parameter list>){
<local declarations>
<sequence of statements>
}
For example: Definition of a function that computes the
absolute value of an integer:
int absolute(int x){
if (x >= 0) return x;
else return -x;
}
og Fundamentals I: Introduction to Functions /Slide 8
Function Call
A function call has the following syntax:
<function name>(<argument list>)
Example: int distance = absolute(-5);
The result of a function call is a value of type
<type>
og Fundamentals I: Introduction to Functions /Slide 9
Arguments/Parameters
one-to-one correspondence between the
arguments in a function call and the
parameters in the function definition.
int argument1;
double argument2;
// function call (in another function, such as main)
result = thefunctionname(argument1, argument2);
// function definition
int thefunctionname(int parameter1, double parameter2){
// Now the function can use the two parameters
// parameter1 = argument 1, parameter2 = argument2
og Fundamentals I: Introduction to Functions /Slide 10
Absolute Value
#include <iostream>
using namespace std;
int absolute (int);// function prototype for absolute()
int main(){
int num, answer;
cout << "Enter an integer (0 to stop): ";
cin >> num;
while (num!=0){
answer = absolute(num);
cout << "The absolute value of " << num
<< " is: " << answer << endl;
cin >> num; }
return 0; }
// Define a function to take absolute value of an integer
int absolute(int x){
if (x >= 0) return x;
else return -x; }
og Fundamentals I: Introduction to Functions /Slide 11
Function Prototype
The function prototype declares the input and
output parameters of the function.
The function prototype has the following syntax:
<type> <function name>(<type list>);
Example: A function that returns the absolute
value of an integer is: int absolute(int);
og Fundamentals I: Introduction to Functions /Slide 12
Function Definition
The function definition can be placed anywhere
in the program after the function prototypes.
If a function definition is placed in front of
main(), there is no need to include its function
prototype.
og Fundamentals I: Introduction to Functions /Slide 13
Absolute Value (alternative)
Note that it is possible to omit the function prototype if the function is placed
before it is called.
#include <iostream>
using namespace std;
int absolute(int x){
if (x >= 0) return x;
else return -x; }
int main(){
int num, answer;
cout << "Enter an integer (0 to stop): ";
cin >> num;
while (num!=0){
answer = absolute(num);
cout << "The absolute value of " << num
<< " is: " << answer << endl;
cin >> num; }
return 0; }
og Fundamentals I: Introduction to Functions /Slide 14
Function of three parameters
#include <iostream>
using namespace std;
double total_second(int, double ,double );
int main(){
cout << total_second(1,1.5, 2) << endl;
return 0;
}
double total_second( int hour, double minutes,
double second)
{
return hour*3600 + minutes * 60 + second;
}
og Fundamentals I: Introduction to Functions /Slide 15
Printing the Diamond Pattern as a
Function
void diamond(int size)
{
int row, space, star;
for(row=1; row<=size; row++){ //top half
for(space=1; space<=size-row; space++)
cout << " ";
for(star=1; star<=2*row-1; star++)
cout << "*";
cout << endl ;
}
for(row=size -1; row>=1; row--){ //bottom half
for(space=1; space<=size-row; space++)
cout << " ";
for(star=1; star<=2*row-1; star++)
cout << "*";
cout << endl ;
} }
og Fundamentals I: Introduction to Functions /Slide 16
Calculating the Area of a Circle
with a Function

More Related Content

PPT
Function in cpu 2
PPTX
Programming in c function
PPTX
Function in c program
PPTX
Functions in C
PPT
Pointer in C
PPSX
Functions in c
PDF
1 introducing c language
PPTX
Functions
Function in cpu 2
Programming in c function
Function in c program
Functions in C
Pointer in C
Functions in c
1 introducing c language
Functions

What's hot (20)

PPTX
functions
PPTX
PPT
Function in C Language
PPT
lets play with "c"..!!! :):)
PPT
Lecture 11 - Functions
PPTX
User defined functions in C
PDF
C programming day#2.
PPTX
C++ lecture 03
PPT
RECURSION IN C
PPT
Lecture 14 - Scope Rules
PPT
Functions in c
PDF
Function lecture
PPTX
Function in c language(defination and declaration)
PPTX
Pointer in C
PPTX
Function in c
PPTX
Function in c program
PPTX
C function presentation
PPTX
Learn c++ (functions) with nauman ur rehman
PDF
Function in C
functions
Function in C Language
lets play with "c"..!!! :):)
Lecture 11 - Functions
User defined functions in C
C programming day#2.
C++ lecture 03
RECURSION IN C
Lecture 14 - Scope Rules
Functions in c
Function lecture
Function in c language(defination and declaration)
Pointer in C
Function in c
Function in c program
C function presentation
Learn c++ (functions) with nauman ur rehman
Function in C
Ad

Similar to Function (20)

PPT
Function
PPT
Functions in C++
PPTX
Silde of the cse fundamentals a deep analysis
PPTX
C structure
PPT
Chapter 1.ppt
PPTX
Chapter 4
PPTX
Chp8_C++_Functions_Part2_User-defined functions.pptx
PPT
Fp201 unit5 1
PDF
PPTX
Lecture 1_Functions in C.pptx
PPT
Basic information of function in cpu
PPT
PDF
Unit 3 (1)
PPTX
Chapter 1 (2) array and structure r.pptx
PPTX
Lecture_5_-_Functions_in_C_Detailed.pptx
PPTX
Function C++
PPTX
Study of Inline Function in C++ with examples
PDF
FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
PPTX
Unit-III.pptx
DOCX
Functions assignment
Function
Functions in C++
Silde of the cse fundamentals a deep analysis
C structure
Chapter 1.ppt
Chapter 4
Chp8_C++_Functions_Part2_User-defined functions.pptx
Fp201 unit5 1
Lecture 1_Functions in C.pptx
Basic information of function in cpu
Unit 3 (1)
Chapter 1 (2) array and structure r.pptx
Lecture_5_-_Functions_in_C_Detailed.pptx
Function C++
Study of Inline Function in C++ with examples
FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
Unit-III.pptx
Functions assignment
Ad

Recently uploaded (20)

PDF
Design an Analysis of Algorithms I-SECS-1021-03
PPTX
Log360_SIEM_Solutions Overview PPT_Feb 2020.pptx
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
iTop VPN Crack Latest Version Full Key 2025
PDF
CCleaner Pro 6.38.11537 Crack Final Latest Version 2025
PPTX
Advanced SystemCare Ultimate Crack + Portable (2025)
PDF
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
PPTX
assetexplorer- product-overview - presentation
PDF
Download FL Studio Crack Latest version 2025 ?
PPTX
Weekly report ppt - harsh dattuprasad patel.pptx
PDF
Autodesk AutoCAD Crack Free Download 2025
PDF
Digital Systems & Binary Numbers (comprehensive )
PPTX
Computer Software and OS of computer science of grade 11.pptx
PDF
Nekopoi APK 2025 free lastest update
PPTX
WiFi Honeypot Detecscfddssdffsedfseztor.pptx
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PDF
17 Powerful Integrations Your Next-Gen MLM Software Needs
PDF
How AI/LLM recommend to you ? GDG meetup 16 Aug by Fariman Guliev
DOCX
Greta — No-Code AI for Building Full-Stack Web & Mobile Apps
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
Design an Analysis of Algorithms I-SECS-1021-03
Log360_SIEM_Solutions Overview PPT_Feb 2020.pptx
Navsoft: AI-Powered Business Solutions & Custom Software Development
iTop VPN Crack Latest Version Full Key 2025
CCleaner Pro 6.38.11537 Crack Final Latest Version 2025
Advanced SystemCare Ultimate Crack + Portable (2025)
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
assetexplorer- product-overview - presentation
Download FL Studio Crack Latest version 2025 ?
Weekly report ppt - harsh dattuprasad patel.pptx
Autodesk AutoCAD Crack Free Download 2025
Digital Systems & Binary Numbers (comprehensive )
Computer Software and OS of computer science of grade 11.pptx
Nekopoi APK 2025 free lastest update
WiFi Honeypot Detecscfddssdffsedfseztor.pptx
Adobe Illustrator 28.6 Crack My Vision of Vector Design
17 Powerful Integrations Your Next-Gen MLM Software Needs
How AI/LLM recommend to you ? GDG meetup 16 Aug by Fariman Guliev
Greta — No-Code AI for Building Full-Stack Web & Mobile Apps
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free

Function

  • 2. og Fundamentals I: Introduction to Functions /Slide 2 Introduction to Functions A complex problem is often easier to solve by dividing it into several smaller parts, each of which can be solved by itself. This is called structured programming. These parts are sometimes made into functions in C++. main() then uses these functions to solve the original problem.
  • 3. og Fundamentals I: Introduction to Functions /Slide 3 C++ Functions C++ allows the use of both internal (user- defined) and external functions. External functions (e.g., abs, ceil, rand, sqrt, etc.) are usually grouped into specialized libraries (e.g., iostream, stdlib, math, etc.)
  • 4. og Fundamentals I: Introduction to Functions /Slide 4 Advantages of Functions Functions separate the concept (what is done) from the implementation (how it is done). Functions make programs easier to understand. Functions can be called several times in the same program, allowing the code to be reused.
  • 5. og Fundamentals I: Introduction to Functions /Slide 5 User-Defined Functions C++ programs usually have the following form: // include statements // function prototypes // main() function // function definitions
  • 6. og Fundamentals I: Introduction to Functions /Slide 6 Function Input and Output
  • 7. og Fundamentals I: Introduction to Functions /Slide 7 Function Definition A function definition has the following syntax: <type> <function name>(<parameter list>){ <local declarations> <sequence of statements> } For example: Definition of a function that computes the absolute value of an integer: int absolute(int x){ if (x >= 0) return x; else return -x; }
  • 8. og Fundamentals I: Introduction to Functions /Slide 8 Function Call A function call has the following syntax: <function name>(<argument list>) Example: int distance = absolute(-5); The result of a function call is a value of type <type>
  • 9. og Fundamentals I: Introduction to Functions /Slide 9 Arguments/Parameters one-to-one correspondence between the arguments in a function call and the parameters in the function definition. int argument1; double argument2; // function call (in another function, such as main) result = thefunctionname(argument1, argument2); // function definition int thefunctionname(int parameter1, double parameter2){ // Now the function can use the two parameters // parameter1 = argument 1, parameter2 = argument2
  • 10. og Fundamentals I: Introduction to Functions /Slide 10 Absolute Value #include <iostream> using namespace std; int absolute (int);// function prototype for absolute() int main(){ int num, answer; cout << "Enter an integer (0 to stop): "; cin >> num; while (num!=0){ answer = absolute(num); cout << "The absolute value of " << num << " is: " << answer << endl; cin >> num; } return 0; } // Define a function to take absolute value of an integer int absolute(int x){ if (x >= 0) return x; else return -x; }
  • 11. og Fundamentals I: Introduction to Functions /Slide 11 Function Prototype The function prototype declares the input and output parameters of the function. The function prototype has the following syntax: <type> <function name>(<type list>); Example: A function that returns the absolute value of an integer is: int absolute(int);
  • 12. og Fundamentals I: Introduction to Functions /Slide 12 Function Definition The function definition can be placed anywhere in the program after the function prototypes. If a function definition is placed in front of main(), there is no need to include its function prototype.
  • 13. og Fundamentals I: Introduction to Functions /Slide 13 Absolute Value (alternative) Note that it is possible to omit the function prototype if the function is placed before it is called. #include <iostream> using namespace std; int absolute(int x){ if (x >= 0) return x; else return -x; } int main(){ int num, answer; cout << "Enter an integer (0 to stop): "; cin >> num; while (num!=0){ answer = absolute(num); cout << "The absolute value of " << num << " is: " << answer << endl; cin >> num; } return 0; }
  • 14. og Fundamentals I: Introduction to Functions /Slide 14 Function of three parameters #include <iostream> using namespace std; double total_second(int, double ,double ); int main(){ cout << total_second(1,1.5, 2) << endl; return 0; } double total_second( int hour, double minutes, double second) { return hour*3600 + minutes * 60 + second; }
  • 15. og Fundamentals I: Introduction to Functions /Slide 15 Printing the Diamond Pattern as a Function void diamond(int size) { int row, space, star; for(row=1; row<=size; row++){ //top half for(space=1; space<=size-row; space++) cout << " "; for(star=1; star<=2*row-1; star++) cout << "*"; cout << endl ; } for(row=size -1; row>=1; row--){ //bottom half for(space=1; space<=size-row; space++) cout << " "; for(star=1; star<=2*row-1; star++) cout << "*"; cout << endl ; } }
  • 16. og Fundamentals I: Introduction to Functions /Slide 16 Calculating the Area of a Circle with a Function