SlideShare a Scribd company logo
1 of 16
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

What's hot (20)

functions
functionsfunctions
functions
 
Function in c
Function in cFunction in c
Function in c
 
Function in C Language
Function in C Language Function in C Language
Function in C Language
 
lets play with "c"..!!! :):)
lets play with "c"..!!! :):)lets play with "c"..!!! :):)
lets play with "c"..!!! :):)
 
Lecture 11 - Functions
Lecture 11 - FunctionsLecture 11 - Functions
Lecture 11 - Functions
 
C pointer basics
C pointer basicsC pointer basics
C pointer basics
 
User defined functions in C
User defined functions in CUser defined functions in C
User defined functions in C
 
C programming day#2.
C programming day#2.C programming day#2.
C programming day#2.
 
C++ lecture 03
C++   lecture 03C++   lecture 03
C++ lecture 03
 
RECURSION IN C
RECURSION IN C RECURSION IN C
RECURSION IN C
 
Lecture 14 - Scope Rules
Lecture 14 - Scope RulesLecture 14 - Scope Rules
Lecture 14 - Scope Rules
 
Functions in c
Functions in cFunctions in c
Functions in c
 
Function lecture
Function lectureFunction lecture
Function lecture
 
Function in c language(defination and declaration)
Function in c language(defination and declaration)Function in c language(defination and declaration)
Function in c language(defination and declaration)
 
Pointer in C
Pointer in CPointer in C
Pointer in C
 
Function in c
Function in cFunction in c
Function in c
 
Function in c program
Function in c programFunction in c program
Function in c program
 
C function presentation
C function presentationC function presentation
C function presentation
 
Learn c++ (functions) with nauman ur rehman
Learn  c++ (functions) with nauman ur rehmanLearn  c++ (functions) with nauman ur rehman
Learn c++ (functions) with nauman ur rehman
 
Function in C
Function in CFunction in C
Function in C
 

Viewers also liked

Viewers also liked (6)

Intro to c++
Intro to c++Intro to c++
Intro to c++
 
C++
C++C++
C++
 
C++ programming function
C++ programming functionC++ programming function
C++ programming function
 
Inline function
Inline functionInline function
Inline function
 
C++ Function
C++ FunctionC++ Function
C++ Function
 
functions of C++
functions of C++functions of C++
functions of C++
 

Similar to Function (20)

Function
FunctionFunction
Function
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
C structure
C structureC structure
C structure
 
Chapter 1.ppt
Chapter 1.pptChapter 1.ppt
Chapter 1.ppt
 
Chapter 4
Chapter 4Chapter 4
Chapter 4
 
Fp201 unit5 1
Fp201 unit5 1Fp201 unit5 1
Fp201 unit5 1
 
Chap 5 c++
Chap 5 c++Chap 5 c++
Chap 5 c++
 
Lecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptxLecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptx
 
Basic information of function in cpu
Basic information of function in cpuBasic information of function in cpu
Basic information of function in cpu
 
Chap 5 c++
Chap 5 c++Chap 5 c++
Chap 5 c++
 
Unit 3 (1)
Unit 3 (1)Unit 3 (1)
Unit 3 (1)
 
Function C++
Function C++ Function C++
Function C++
 
FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
 FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM) FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
 
Unit-III.pptx
Unit-III.pptxUnit-III.pptx
Unit-III.pptx
 
Functions assignment
Functions assignmentFunctions assignment
Functions assignment
 
C++ Functions
C++ FunctionsC++ Functions
C++ Functions
 
C FUNCTIONS
C FUNCTIONSC FUNCTIONS
C FUNCTIONS
 
Cpp
CppCpp
Cpp
 
C function
C functionC function
C function
 
VIT351 Software Development VI Unit1
VIT351 Software Development VI Unit1VIT351 Software Development VI Unit1
VIT351 Software Development VI Unit1
 

Recently uploaded

Harnessing ChatGPT - Elevating Productivity in Today's Agile Environment
Harnessing ChatGPT  - Elevating Productivity in Today's Agile EnvironmentHarnessing ChatGPT  - Elevating Productivity in Today's Agile Environment
Harnessing ChatGPT - Elevating Productivity in Today's Agile EnvironmentVictorSzoltysek
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...Health
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...masabamasaba
 
%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Hararemasabamasaba
 
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplatePresentation.STUDIO
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...SelfMade bd
 
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrainmasabamasaba
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyviewmasabamasaba
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Steffen Staab
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech studentsHimanshiGarg82
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareJim McKeeth
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisamasabamasaba
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdfPearlKirahMaeRagusta1
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...Jittipong Loespradit
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...masabamasaba
 

Recently uploaded (20)

Harnessing ChatGPT - Elevating Productivity in Today's Agile Environment
Harnessing ChatGPT  - Elevating Productivity in Today's Agile EnvironmentHarnessing ChatGPT  - Elevating Productivity in Today's Agile Environment
Harnessing ChatGPT - Elevating Productivity in Today's Agile Environment
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 
%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare
 
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdf
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
 

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