Bahasa
Pemograman
Komputer (BPK) 2
Riki Afriansyah
Contents
Contents
What is a
function?
Syntax of
Function
Void
Functions
Local VS
Global
Variable
What is a function?
 A large program in c can be divided to many
subprogram. The subprogram is called as a
function
 A function is a block of code that performs a
specific task
 Creating an application using function
makes it easier to understand, edit, check
errors etc.
 A function is a set of statements that take
inputs, do some specific computation and
produces output
Syntax of Function
#include <iostream>
using namespace std;
int sum (int x, int y); //declaring function
int main()
{
int a = 10;
int b = 20;
int c = sum (a, b); //calling function
cout << c;
}
int sum (int x, int y) //defining function
{
return (x + y);
}
Declaring, Defining and Calling Function
Void Functions
A void function returns values by modifying one or more parameters rather than
using a return statement. For example:
Void Functions
Example 2.
Local Variable VS Global Variable
A local variable is a variable that is declared inside a
function. A global variable is a variable that is declared
outside all functions. A local variable can only be used in
the function where it is declared. A global variable can be
used in all functions.
Local Variable VS Global Variable
EXERCISES 1
Write a program that calculates (Add, Subtract, Multiply,
Divide and Modulus) using functions.
EXERCISES 2
Write a program that If f(x) = x2+ 3x + 1, then find the values of f(0), f(1), f(2), f(3).
EXERCISES 3
Write a program that ask for three numbers, compare them and show the
maximum. Declare a function called max_three that compares the numbers and
returns the maximum. See the sample below.
EXERCISES 4
Write a function that uses n (integer) and prints out a triangle as the following
example
(for n = 5) :
EXERCISES 5
Write a program that calculate surface area and volume of cubes. You must be
using:
• Local variabel for the side of a cube (int S) and
• Void with two functions (surface area and volume).
Function BPK2

Function BPK2

  • 1.
  • 2.
    Contents Contents What is a function? Syntaxof Function Void Functions Local VS Global Variable
  • 3.
    What is afunction?  A large program in c can be divided to many subprogram. The subprogram is called as a function  A function is a block of code that performs a specific task  Creating an application using function makes it easier to understand, edit, check errors etc.  A function is a set of statements that take inputs, do some specific computation and produces output
  • 4.
  • 5.
    #include <iostream> using namespacestd; int sum (int x, int y); //declaring function int main() { int a = 10; int b = 20; int c = sum (a, b); //calling function cout << c; } int sum (int x, int y) //defining function { return (x + y); } Declaring, Defining and Calling Function
  • 6.
    Void Functions A voidfunction returns values by modifying one or more parameters rather than using a return statement. For example:
  • 7.
  • 8.
    Local Variable VSGlobal Variable A local variable is a variable that is declared inside a function. A global variable is a variable that is declared outside all functions. A local variable can only be used in the function where it is declared. A global variable can be used in all functions.
  • 9.
    Local Variable VSGlobal Variable
  • 10.
    EXERCISES 1 Write aprogram that calculates (Add, Subtract, Multiply, Divide and Modulus) using functions.
  • 11.
    EXERCISES 2 Write aprogram that If f(x) = x2+ 3x + 1, then find the values of f(0), f(1), f(2), f(3).
  • 12.
    EXERCISES 3 Write aprogram that ask for three numbers, compare them and show the maximum. Declare a function called max_three that compares the numbers and returns the maximum. See the sample below.
  • 13.
    EXERCISES 4 Write afunction that uses n (integer) and prints out a triangle as the following example (for n = 5) :
  • 14.
    EXERCISES 5 Write aprogram that calculate surface area and volume of cubes. You must be using: • Local variabel for the side of a cube (int S) and • Void with two functions (surface area and volume).