You are on page 1of 19

F 2037 PROGRAMMING FUNDAMENTALS WITH C++

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

of lines in a program can be reduced.


One long program willl be divided into smaller parts and

each of these parts is programmed separately

There are several motivations for functionalizing a program:


The divide-and conquer approach makes program

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 function_name (parameter-list) { variable declaration; return expression; }

function_type = type of data in C++ function_name = variable name parameter-list passing parameter value when

invoked return return value or control

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.

The most common form of a function prototype is :

returnDataType functionName(argument data type list);


If the called function is placed physically above the

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);

Calling and Passing Data to a Function:


Syntax :

functionName(data passed to function);


A function is invoked by a function call.
The function call mentions the function by name and

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

char response(char ch)


Created by programmer ownself

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; }

Penggunaan operator & atau address of

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

Example Factorial Program :


#include<iostream> #include<iomanip> using namespace std; unsigned long factorial(unsigned long); //function prototype

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..

Example Factorial Program (cont..):


//recursive defintion of function factorial unsigned long factorial(unsigned long number) {

//base class
if(number<=1) return 1;

//recursive step
else

return number*factorial(number-1); }//end function factorial

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