Presentation is loading. Please wait.

Presentation is loading. Please wait.

CHAPTER 5 FUNCTIONS I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)

Similar presentations


Presentation on theme: "CHAPTER 5 FUNCTIONS I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)"— Presentation transcript:

1 CHAPTER 5 FUNCTIONS I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)

2 C ONTENTS Introduction User-defined function Function without parameter Calling Functions Function with parameter Function with return value 2

3 INTRODUCTION Program without function This program has one long, complex function containing all of the statements necessary to solve a problem void main() { statement; } 3

4 INTRODUCTION Program with function In this program the problem has been divided into smaller problems, each of which is handled by a separate function int main() { statement1; function1(); function2(); function3(); } void function1() { statement; } void function2() { statement; } void function3() { statement; } 4

5 USER-DEFINED FUNCTION There are 3 elements related to functions: Function prototype/declaration Function definition Function call

6 Function Declaration/Prototype A form for called function must be declared first: function_type function_name(parameter_list); parameter_list – consists of data type for every parameter which is a function input. function_type function_name( ); USER-DEFINED FUNCTION 6

7 Function Definition Properties that form the function definition: Name of the function Number of parameters Data type of each parameter Type of the function Code required to accomplish the task (the body of the function) Syntax: function Type : type of the value returned by the function (data type) formal parameter list: a variable declared in the function heading; can be empty USER-DEFINED FUNCTION 7

8 Function Call The syntax for a function call is: A call to a value-returning function with an empty formal parameter list is: functionName() Example: USER-DEFINED FUNCTION 8

9 There are 2 ways to declare a function: i. through global variable ii. through local variable using parameter TYPES OF VARIABLE AND ITS SCOPE 9

10 T HROUGH GLOBAL VARIABLE Global variable is a variable that can be declared outside of the function (outside declaration). By declaring variable as global, variables can be used in many functions. 10

11 T HROUGH LOCAL VARIABLE Local variable is a variable that must be declared in any block. Its scope starts at its declaration point until the end of the block. It only can be used in that function. Local variable belongs to the function where it is declared and can’t be shared or used by any other function including main function. If the same variable name is declared as a local variable in few functions, the variable wil be assumed as a different one. 11

12 12 Example: #include void display(); float number = 50.6; // a global variable named number int main() { cout << “The value of number is: ”<< number << endl; display(); return 0; } void display() { float number= 34.7; // a local variable named number cout<<“The number is:”<<number<<endl; } TYPES OF VARIABLE AND ITS SCOPE

13 F UNCTION WITHOUT PARAMETER void function without parameters : Function definition syntax: void is a reserved word Function call syntax: 13

14 F UNCTION WITH PARAMETER void function with parameters : Function definition syntax: 14

15 Example: F UNCTION WITH PARAMETER 15

16 A formal parameter receives a copy of the content of corresponding actual parameter A value parameter - a formal parameter that receives a copy of the content of the corresponding actual parameter A reference parameter - a formal parameter that receives the location (memory address) of the corresponding actual parameter F UNCTION WITH PARAMETER 16

17 Value parameters : If a formal parameter is a value parameter The value of the corresponding actual parameter is copied into it The value parameter has its own copy of the data During program execution, the value parameter manipulates the data stored in its own memory space F UNCTION WITH PARAMETER 17

18 Reference parameters : If a formal parameter is a reference parameter It receives the address and stores the corresponding actual parameter During program execution to manipulate the data, the address stored in the reference parameter directs it to the memory space of the corresponding actual parameter Pass one or more values from a function Change the value of the actual parameter F UNCTION WITH PARAMETER 18

19 F UNCTION WITH PARAMETER 19

20 F UNCTION WITH PARAMETER 20

21 Cont’d F UNCTION WITH PARAMETER 21

22 F UNCTION WITH PARAMETER 22

23 23 FUNCTION WITH RETURN VALUE Void functions: do not have a return type Value-returning functions: have a data type A function needs to return a value when the function type is int, double, float or char. Once the function computes the value, the function returns the value via the return statement except void data type for function. The syntax of the return statement is: When a return statement executes Function immediately terminates Control goes back to the caller

24 24 Example : FUNCTION WITH RETURN VALUE

25 USER-DEFINED FUNCTION Example : Example

26 26 Flow of Execution Execution always begins at The first statement in the function main no matter where main is placed in the program Other functions are executed only when they are called Function prototypes appear before any function definition The compiler translates these first The compiler can then correctly translate a function call

27 27 EXERCISE Write a program to calculate grade. The program has 3 functions: 1.main a. get the course score b. print the course grade 2.getScore a. prompt the user for the input b. get the input c. print the course score 3.printGrade a. calculate the course grade b. print the course grade

28 28 EXERCISE Determine the output for the program below: #include void test(int first, int second); int main() { int num = 5; test(24, num); cout<<num<<endl; test(num, num); cout<<num<<endl; test(num*num, num); cout<<num<<endl; test(num+num, num); cout<<num<<endl; return 0; } void test(int first, int second) { int third; third = first + second * second + 2; first = second - first; second = 2 * second; cout<<first<<" " << second <<“ "<<third<<endl; }

29 29 EXERCISE Determine the output for the program below: You are required to write a function-based program that will calculate area of a circle. There will be only ONE input value respectively (radius). Area of a circle is calculated as: area = radius * radius * 3.142 (use predefined function!) Suggested name for each function:

30 30 EXERCISE - Answers Write a program to calculate grade. The program has 3 functions: Example: #include void getScore(); void printGrade(); void main() { getScore(); printGrade(); } void getScore() { cout<<“Enter score:”; cin>>score; cout<<“The score is:”<<score<<endl; }

31 31 EXERCISE - Answers Write a program to calculate grade. The program has 3 functions: Example: void printGrade() { if (score>=80) cout<<“A”<<endl; if (score>=60) cout<<“B”<<endl; if (score>=50) cout<<“C”<<endl; if (score<50) cout<<“F”<<endl; }

32 32 Determine the output for the program below: EXERCISE - Answers OUTPUT: 51 -19 10 5 32 0 10 5 52 -20 10 5 37 -5 10 5

33 33 EXERCISE Determine the output for the program below: You are required to write a function-based program that will calculate area of a circle. There will be only ONE input value respectively (radius). Area of a circle is calculated as: area = radius * radius * 3.142 (use predefined function!) Suggested name for each function:


Download ppt "CHAPTER 5 FUNCTIONS I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)"

Similar presentations


Ads by Google