23
FUNCTIONS - What Is A Function? - Advantages - Function Declaration - Function Definition - Variable scope - Function Arguments - Passing Arguments by value - Passing Arguments by Reference - Calling other Functions / Recursive

FUNCTIONS

Embed Size (px)

DESCRIPTION

FUNCTIONS. - What Is A Function? - Advantages Function Declaration Function Definition Variable scope Function Arguments Passing Arguments by value Passing Arguments by Reference Calling other Functions / Recursive. What is a Function?. - PowerPoint PPT Presentation

Citation preview

Page 1: FUNCTIONS

FUNCTIONS- What Is A Function?- Advantages - Function Declaration - Function Definition- Variable scope- Function Arguments- Passing Arguments by value- Passing Arguments by Reference- Calling other Functions / Recursive

Page 2: FUNCTIONS

What is a Function?

It is a section of code that is enclosed and that provides specific functionality to a program.

i.e.- it is enclosed outside the main program- therefore it is called from outside the main

program or other functions to provide a service / functionality

Page 3: FUNCTIONS

What is a Function?

• 3 Main Advantages– Code Easier :- They make the program code easier

to understand and to maintain– Code Reuse :Tried and tested function can be

reused by other programs– Division of labour : several programmers can

divide the workload in a large project by working on different functions for the program

Page 4: FUNCTIONS

What is a Function?

• Function Declaration– functions must be declared early in the program

code, just as it is done for primitive variables – It is declared as a prototype

Page 5: FUNCTIONS

What is a Function?• Declaration syntax

return-data-type function-name (arguments-list)

Where return-data-type can be intstringchardouble void

among others

Page 6: FUNCTIONS

What is a Function?

where arguments-list - are values to be passed as arguments from

the caller - and these can be of any quantity and data

type- they must agree with those specified in the

prototype function declaration

Page 7: FUNCTIONS

What is a Function?

• Function Prototype declaration

Egvoid calcsum();

int calcmean();

double calcstdev();

Page 8: FUNCTIONS

What is a Function?

void calcsum();

declares a function named calcsum();

that accepts no argument -- () is emptyand

returns no value : because type is void

Page 9: FUNCTIONS

What is a Function?

• Definition- Appears later in the program code- Comprises a repeat of the prototype together

with the function body

where the function body is a pair of braces { }surrounding the statements that are to

be executed when the function is called.

Page 10: FUNCTIONS

Examples #Include<iostream>using namespace std; //declare the function prototype earlyvoid calcsum();void writetitle();int sum;int main(){

writetitle(); // function calls from main rsum = calcsum();

cout << “The Sum is = “ << rsum << endl ;system(“pause”);return 0;

}

Page 11: FUNCTIONS

// function definition

//define the function writetitle()void writetitle(){

string dept = “Computer Science”;cout << dept << endl;

}

Page 12: FUNCTIONS

// function definition

//define the function calcsum()void calcsum(){

int val1 = 20;int val2 = 35;int tot = val1 + val2;cout << “The sum is = “ << tot endln;

}

Page 13: FUNCTIONS

Passing values to functions

• Functions can be passed values• The caller function passes value in the form of

arguments to a function that it calls• These can be of any quantity and data type• But they must agree with those specified in

the prototype function declaration

Page 14: FUNCTIONS

Functions can return a value

• return- the function can return a value of any data-

type, as long as it is of the type specified in the function prototype

Page 15: FUNCTIONS

// Function arguments

Note the FUNCTIONthe Caller FUNCTION

Int main(){ ................ writeline();

calcsum();....................

}

Page 16: FUNCTIONS

// Function arguments#Include<iostream>using namespace std; //declare the function prototype earlyvoid calcsum();void writetitle();int sum;int main(){

writetitle(); // function calls from main calcsum();system(“pause”);return 0;

}//define the function writetitle()

void writetitle(){string dept = “Computer Science”;cout << dept << endl;

}//define the function calcsum()

void calcsum(){int val1 = 20;int val2 = 35;int tot = val1 + val2;cout << “The sum is = “ << tot endln;

}

Pfunction.txt

Page 17: FUNCTIONS

// Function passing argumentsby value

• Note the Function Prototype arguments and the Function definition arguments

// Function Prototype declaration int getmax(int n1, int n2); int getnum();

Page 18: FUNCTIONS

// Function passing argumentsby value

• // function definition int getnum()

{ int numcout << “Enter a number”;cin >> num;return num;

}

Page 19: FUNCTIONS

// Function passing argumentsby value

• // function definition int getmax(int n1, int n2)

{ return (n1 > n2) ? n1 : n2;

}

Page 20: FUNCTIONS

// Function passing argumentsby value

#include<iostream>using namespace std;// function declaration prototypeint getnum();int getmax(int n1 , int n2);//main function int main(){ int num1 , num2; num1 = getnum(); num2 = getnum();// determine the maximum of num1 and num2 cout << “Max number : “ << getmax(num1, num2)

<< endl;}

Page 21: FUNCTIONS

// Function passing argumentsby value

int getnum(){ int num

cout << “Enter a number”;cin >> num;return num;

}

Page 22: FUNCTIONS

// Function passing argumentsby value

• // function definition int getmax(int n1, int n2)

{ return (n1 > n2) ? n1 : n2;

}

Page 23: FUNCTIONS

// Function passing argumentsby value

• Passing arguments by value when arguments are passed to a function

it is the value that is passed, not the variable itself