You are on page 1of 20

Becoming a Functional C++

Programmer

Specific Objectives
At the end of the session the students should be
able to:
Define function
Identify the function structure
Identify function prototype
Declaration of function
Distinguish and apply the 4 ways of function
Naming functions with different arguments

LET US BEGIN WITH WORD

FUNCTION

A function is a group of statements that is executed


when it is called from some point of the program.

TEACHERS open the


door, but you must enter
YOURSELF

C++ allows programmers to divide their code


into exactly such chunks (known as functions).
This divide-and-conquer approach reduces the
difficulty of creating a working program of
significant size.

USING FUNCTION
#include<iostream>
Using namespace std;
Func3(){
}
int main(){
}
Func1(){
}
Func2(){
}

Structure of a Function
Attributes of a function
The type of the value returned by the function
A name that identifies it
The functions parameters and their data types
Int Func1(parameter type) {
Processing statements
}

Calling a Function(1)
main()
{
Func1();
}
void Func1(){
cout<<hello world!;
}

1st WAY FUNCTION


#include<iostream>
using namespace std;
//1st way
void print(){
cout<<"HELLO WORLD!";
}
int main(){
print();
system("pause");
return 0;
function with
}

no return type

2nd WAY FUNCTION


#include<iostream>
using namespace std;
//2nd way
float pie(){
return(3.1416);
}
int main(){
float num1;
num1 = pie();
cout<<num1;
system("pause");
return 0;
}

function with return type

2nd WAY FUNCTION


#include<iostream>
using namespace std;
//2nd way
int wee(){
return(123);
}
int main(){
cout<<wee();
system("pause");
return 0;
}

function with return type

3rd WAY FUNCTION


#include<iostream>
using namespace std;

int main(){
void back(int); defining function prototype
int num;
cin>>num;
back(num);
system("pause");
return 0;
}
void back(int wew){
wew = wew/2;
cout<<wew;
}

function with parameters but with no return type

3rd WAY FUNCTION


#include<iostream>
using namespace std;

int main(){
void sum(int,int);
int num, num1;
cin>>num;
cin>>num1;
sum(num,num1);
system("pause");
return 0;
}
void sum(int wew,int wow){
int sum = wew + wow;
cout<<sum;
}

defining function prototype

function with parameters but with no return type

4th WAY FUNCTION


#include<iostream>
using namespace std;

int main(){
int sum(int,int); defining function prototype
int num, num1;
cin>>num;
cin>>num1;
cout<<sum(num,num1);
system("pause");
return 0;
}

int sum(int wew,int wow){


int sum = wew + wow;
return(sum);
}

function with parameters with return type

BENEFITS
large tasks can be broken up into manageable
units
repetitive tasks can be simplified

Specific Objectives
At the end of the session the students should be
able to:
Define function
Identify the function structure
Identify function prototype
Declaration of function
Distinguish and apply the 4 ways functions
Naming functions with different arguments

If debugging is the process of removing


software bugs, then programming must be the
process of putting them in.
- Edsger Dijkstra

Activity #2
Using 1st and 2nd way of Function:
Menu System
1 converting ft to inch (1st way)
2 Area of a circle (2nd way)
3 Exit
Do You Want to Try again? (y/n)

Assignment#1
Give 2 example of 3rd and 4th way of function
program.
*code
*output
*printed

You might also like