SlideShare a Scribd company logo
Cin
Cin with examples
Cout
Cout
Defining a Function
return_type function_name( parameter list )
{
body of the function
}
Example
// function returning the max between two numbers
int max(int num1, int num2)
{
// local variable declaration
int result;
if (num1 > num2)
result = num1;
else
result = num2;
return result;
}
Function Declarations
return_type function_name( parameter list );
int max(int num1, int num2);
int max(int, int);
Calling a Function#include <iostream>
using namespace std;
// function declaration
int max(int num1, int num2);
int main ()
{
// local variable declaration:
int a = 100;
int b = 200;
int ret;
// calling a function to get max value.
ret = max(a, b);
cout << "Max value is : " << ret << endl;
return 0;
}
// function returning the max between two numbers
int max(int num1, int num2)
{
// local variable declaration int result;
if (num1 > num2)
result = num1;
else
result = num2;
return result;
Default Values for Parameters
#include <iostream>
using namespace std;
int sum(int a, int b = 20)
{
int result;
result = a + b;
return (result);
}
int main ()
{
// local variable declaration:
int a = 100;
int b = 200;
int result;
// calling a function to add the values.
result = sum(a, b);
cout << "Total value is :" << result << endl;
// calling a function again as follows.
result = sum(a);
cout << "Total value is :" << result << endl;
return 0; }
Default Values for Parameters
#include <iostream>
using namespace std;
int sum(int a, int b = 20)
{
int result;
result = a + b;
return (result);
}
int main ()
{
// local variable declaration:
int a = 100;
int b = 200;
int result;
// calling a function to add the values.
result = sum(a, b);
cout << "Total value is :" << result << endl;
// calling a function again as follows.
result = sum(a);
cout << "Total value is :" << result << endl;
return 0; }

More Related Content

PPTX
Javascript function
PPTX
Queue oop
PPTX
Javascript Function
PDF
Timur Shemsedinov "Пишу на колбеках, а что... (Асинхронное программирование)"
PDF
Yurii Shevtsov "V8 + libuv = Node.js. Under the hood"
PPT
Lecture#8 introduction to array with examples c++
PPTX
Dynamic Objects,Pointer to function,Array & Pointer,Character String Processing
DOC
Labsheet_3
Javascript function
Queue oop
Javascript Function
Timur Shemsedinov "Пишу на колбеках, а что... (Асинхронное программирование)"
Yurii Shevtsov "V8 + libuv = Node.js. Under the hood"
Lecture#8 introduction to array with examples c++
Dynamic Objects,Pointer to function,Array & Pointer,Character String Processing
Labsheet_3

What's hot (20)

PPT
4 Type conversion functions
PDF
All you need to know about Callbacks, Promises, Generators
PPT
PPT
Lecture05
PPTX
Javascript this keyword
PDF
C++ ARRAY WITH EXAMPLES
PPTX
Tail Recursion in data structure
KEY
GoLightly: A Go Library For Building Virtual Machines
PPTX
Function composition in Javascript
ODP
Functors, applicatives, monads
PDF
A "Do-It-Yourself" Specification Language with BeepBeep 3 (Talk @ Dagstuhl 2017)
PDF
The Big Three
PPT
Removal Of Recursion
DOCX
Jose dossantos.doc
PDF
삼성 바다 앱개발 실패 노하우 2부
DOC
Rumus VB-2
PPT
PPTX
Formatted Console I/O Operations in C++
PPT
C++totural file
PDF
String searching
4 Type conversion functions
All you need to know about Callbacks, Promises, Generators
Lecture05
Javascript this keyword
C++ ARRAY WITH EXAMPLES
Tail Recursion in data structure
GoLightly: A Go Library For Building Virtual Machines
Function composition in Javascript
Functors, applicatives, monads
A "Do-It-Yourself" Specification Language with BeepBeep 3 (Talk @ Dagstuhl 2017)
The Big Three
Removal Of Recursion
Jose dossantos.doc
삼성 바다 앱개발 실패 노하우 2부
Rumus VB-2
Formatted Console I/O Operations in C++
C++totural file
String searching
Ad

Similar to Function (20)

PPTX
functions of C++
PPTX
UNIT3.pptx
PPTX
Part 3-functions1-120315220356-phpapp01
PPT
Inbuilt Functions in C++ computer language.ppt
PPTX
C Programming Language Part 7
DOC
Functions
PPTX
Cs1123 8 functions
PDF
PPTX
C++ programming function
PPTX
C++ FUNCTIONS-1.pptx
PPTX
Silde of the cse fundamentals a deep analysis
PDF
how to reuse code
PDF
Cpp functions
PPTX
C++ Functions | Introduction to programming
PPTX
C++ lecture 03
PPTX
Classes function overloading
PPTX
Function C++
PPT
Fp201 unit5 1
PPT
Review functions
PPT
function_v1.ppt
functions of C++
UNIT3.pptx
Part 3-functions1-120315220356-phpapp01
Inbuilt Functions in C++ computer language.ppt
C Programming Language Part 7
Functions
Cs1123 8 functions
C++ programming function
C++ FUNCTIONS-1.pptx
Silde of the cse fundamentals a deep analysis
how to reuse code
Cpp functions
C++ Functions | Introduction to programming
C++ lecture 03
Classes function overloading
Function C++
Fp201 unit5 1
Review functions
function_v1.ppt
Ad

Recently uploaded (20)

PPTX
PPH.pptx obstetrics and gynecology in nursing
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PPTX
master seminar digital applications in india
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
Business Ethics Teaching Materials for college
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PPTX
Week 4 Term 3 Study Techniques revisited.pptx
PPTX
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
PDF
Insiders guide to clinical Medicine.pdf
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
01-Introduction-to-Information-Management.pdf
PDF
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
PPTX
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
PDF
TR - Agricultural Crops Production NC III.pdf
PPH.pptx obstetrics and gynecology in nursing
human mycosis Human fungal infections are called human mycosis..pptx
master seminar digital applications in india
O7-L3 Supply Chain Operations - ICLT Program
Business Ethics Teaching Materials for college
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Week 4 Term 3 Study Techniques revisited.pptx
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
Insiders guide to clinical Medicine.pdf
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
2.FourierTransform-ShortQuestionswithAnswers.pdf
Renaissance Architecture: A Journey from Faith to Humanism
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
01-Introduction-to-Information-Management.pdf
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
TR - Agricultural Crops Production NC III.pdf

Function

  • 1. Cin
  • 5. Defining a Function return_type function_name( parameter list ) { body of the function }
  • 6. Example // function returning the max between two numbers int max(int num1, int num2) { // local variable declaration int result; if (num1 > num2) result = num1; else result = num2; return result; }
  • 7. Function Declarations return_type function_name( parameter list ); int max(int num1, int num2); int max(int, int);
  • 8. Calling a Function#include <iostream> using namespace std; // function declaration int max(int num1, int num2); int main () { // local variable declaration: int a = 100; int b = 200; int ret; // calling a function to get max value. ret = max(a, b); cout << "Max value is : " << ret << endl; return 0; } // function returning the max between two numbers int max(int num1, int num2) { // local variable declaration int result; if (num1 > num2) result = num1; else result = num2; return result;
  • 9. Default Values for Parameters #include <iostream> using namespace std; int sum(int a, int b = 20) { int result; result = a + b; return (result); } int main () { // local variable declaration: int a = 100; int b = 200; int result; // calling a function to add the values. result = sum(a, b); cout << "Total value is :" << result << endl; // calling a function again as follows. result = sum(a); cout << "Total value is :" << result << endl; return 0; }
  • 10. Default Values for Parameters #include <iostream> using namespace std; int sum(int a, int b = 20) { int result; result = a + b; return (result); } int main () { // local variable declaration: int a = 100; int b = 200; int result; // calling a function to add the values. result = sum(a, b); cout << "Total value is :" << result << endl; // calling a function again as follows. result = sum(a); cout << "Total value is :" << result << endl; return 0; }