ICT 104, Programming 2
PARAMETER
PASSING
MECHANISMS
Powered by:
Arruejo, Richard C.
Callejo, Arpee M.
DIT I
Saint Paul University, Tuguegarao, Cagayan
STRATEGY
▸Gamification
▸Hands-on Activity(Peer-Programming)
OBJECTIVES
At the end of lesson, the student is expected to learn
how to pass by parameter and call functions.
GAMIFICATION
GAMIFICATION
GAMIFICATION
GAMIFICATION
GAMIFICATION
GAMIFICATION
GAMIFICATION
GAMIFICATION
GAMIFICATION
PROCEDURES
▸Functions allow to structure programs in segments of code to perform
individual tasks.
A group of statements that is given a name, and which can be called from
some point of the program. The most common syntax to define a function is:
type name ( parameter1, parameter2, ...) { statements }
Where:
- type is the type of the value returned by the function.
- name is the identifier by which the function can be called.
- parameters (as many as needed): Each parameter consists of a type
followed by an identifier, with each parameter being separated from the next
by a comma
- statements is the function's body. It is a block of statements surrounded by
braces { } that specify what the function actually does.
EXAMPLES
// No & means: pass-by-value
double fun ( double a, double b )
{
double m;
a = a + 1;
b = b + 2;
m = a + b;
return(m);
}
int main(int argc, char **argv)
{
double x = 1.0, y = 4.0;;
double r;
r = fun( x, y );
cout << x << endl;
cout << y << endl;
cout << r << endl;
}
// With & means: pass-by-reference
double fun ( double & a, double & b )
{
double m;
a = a + 1;
b = b + 2;
m = a + b;
return(m);
}
int main(int argc, char **argv)
{
double x = 1.0, y = 4.0;;
double r;
r = fun( x, y );
cout << x << endl;
cout << y << endl;
cout << r << endl;
}
EXERCISE
▸Exercise 3.1
▸Write a program with a function that takes two int parameters, adds them
together, then returns the sum. The program should ask the user for two
numbers, then call the function with the numbers as arguments, and tell the user
the sum.
ASSESSMENT
Correctness of the code 60%
Structure 40%
Total 100%
thanks!
Powered by:
Arruejo, Richard C.
Callejo, Arpee M.
DIT I, Saint Paul University, Tuguegarao, Cagayan

PARAMETER PASSING MECHANISMS

  • 1.
    ICT 104, Programming2 PARAMETER PASSING MECHANISMS Powered by: Arruejo, Richard C. Callejo, Arpee M. DIT I Saint Paul University, Tuguegarao, Cagayan
  • 2.
  • 3.
    OBJECTIVES At the endof lesson, the student is expected to learn how to pass by parameter and call functions.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
    PROCEDURES ▸Functions allow tostructure programs in segments of code to perform individual tasks. A group of statements that is given a name, and which can be called from some point of the program. The most common syntax to define a function is: type name ( parameter1, parameter2, ...) { statements } Where: - type is the type of the value returned by the function. - name is the identifier by which the function can be called. - parameters (as many as needed): Each parameter consists of a type followed by an identifier, with each parameter being separated from the next by a comma - statements is the function's body. It is a block of statements surrounded by braces { } that specify what the function actually does.
  • 14.
    EXAMPLES // No &means: pass-by-value double fun ( double a, double b ) { double m; a = a + 1; b = b + 2; m = a + b; return(m); } int main(int argc, char **argv) { double x = 1.0, y = 4.0;; double r; r = fun( x, y ); cout << x << endl; cout << y << endl; cout << r << endl; } // With & means: pass-by-reference double fun ( double & a, double & b ) { double m; a = a + 1; b = b + 2; m = a + b; return(m); } int main(int argc, char **argv) { double x = 1.0, y = 4.0;; double r; r = fun( x, y ); cout << x << endl; cout << y << endl; cout << r << endl; }
  • 15.
    EXERCISE ▸Exercise 3.1 ▸Write aprogram with a function that takes two int parameters, adds them together, then returns the sum. The program should ask the user for two numbers, then call the function with the numbers as arguments, and tell the user the sum.
  • 16.
    ASSESSMENT Correctness of thecode 60% Structure 40% Total 100%
  • 17.
    thanks! Powered by: Arruejo, RichardC. Callejo, Arpee M. DIT I, Saint Paul University, Tuguegarao, Cagayan