Professional Documents
Culture Documents
TOPIC 5 : FUNCTION
LEARNING OUTCOMES :
Understand the uses of function Understand the Built-in function and User-defined
Function Describe the components of functions. Identify the function call : call by value, call by reference Understand the concepts of recursion.
Introduction
A function is a set of statements that performs a specific
task.
It can be called from any section in the program and
executed.
The main advantage in using functions is that the number
development more manageable. Software reusability using existing functions as building blocks to create new programs. To avoid repeating code in a program To keep program organized and readable Dividing a program into functions is one of the major principles in structured programming Another reason to use function is to reduce program size
Function Declaration :
Syntax:
function_type = type of data in C++ function_name = variable name parameter-list passing parameter value when
Function Prototypes :
Functions can be declared to all calling functions by
means of a prototype. The prototypes provides a declarations for a function that specifies the data type returned by the function, its name, and the data types of the arguments expected by the function. A with all declarations, a function prototype is terminated with a semicolon and may be included with local variable declaration or as a global declaration.
calling function, no further declaration is required, since the functions definition serves as a global declaration to all following functions. Example : void maximum(double, double, double);
provides information (as arguments) that the called function needs to perform its task
Scope of variable :
The scope of a variable is determined by where the
variables definition statement is placed. A local variable is defined within a function and can only be used within its defining function or block. A global variable is defined is defined outside a function and can be used in any function following the variables definition.
Types of function :
Built-in Function
strlen(fadzlina) atof(s)
User-defined Function
float purata(float x, float y) void panggilan(void)
pow(x, y)
aPart of the system / software
Example Programs :
#include<iostream> using namespace std; int square(int); // function prototype int main() { //loop 10 times and calculate and output //square of x each time for(int x=1;x<=10;x++) cout<<square(x)<<" "; cout<<endl; //function call return 0; //indicates successful termination }//end main //square function defintion returns square of an integer int square(int y) // y is a copy of argument to function { return y*y; //returns square of y as an int }//end function square
Passing Arguments:
Passing argument is a value passed to the function. If the value is passes as a parameter, a copy of that
value is passed to the function. This form of parameter passing is known as pass by value. Then function refers to that value by parameter name. A change made to that parameter will not change the original value of the variable passed.
#include <iostream> using namespace std; main() { void max_min(int, int, int&, int&); int x,y,max,min; cout <<"enter two numbers:\n" ; cin >>x>>y; max_min(x,y,max,min); cout << "Maksimum : " << max; cout << "\nMinimum : " << min; return 0; } void max_min(int x, int y, int& max, int& min) { if (x>y) max = x; else max = y; if (x<y) min = x; else min = y; return; }
seolah menghantar alamat pembolehubah kepada fungsi. jadi, nilai boleh berubah.
Tujuan : - untuk mengubah lebih drpd 1 pembolehubah dalam fungsi yang dipanggil.
Recursion :
A recursive function is a function that calls itself
either directly or indirectly A recursive solution is one in which the solution can be expressed in terms of a recurring version of the basic solution algorithm. A recursive algorithm must always specify :
The first case or class How the nth case is related to the (n-1) case
int main() { //loop 10 times. During each iteration, calculate //factorial(i) and display result for (int i=0; i<=10; i++) cout<<setw(2)<<i<<"!= "<<factorial(i)<<endl; return 0; //indicate successful termination }//end main
Cont..
//base class
if(number<=1) return 1;
//recursive step
else
Output :
Summary :
Function prototype used to check if the argument
and types in calling function correspond to the number of parameter when the function is called. Function definition a function must be defined before it can be used Call Function call another function by passing value or reference return return the control back to the function that invoked it