Presentation is loading. Please wait.

Presentation is loading. Please wait.

FUNCTION CSC128.

Similar presentations


Presentation on theme: "FUNCTION CSC128."— Presentation transcript:

1 FUNCTION CSC128

2 User – defined Function
Contents Predefined Function User – defined Function

3 Predefined function

4 Functions that have been defined by the producer of a compiler.
Predefined Function Functions that have been defined by the producer of a compiler. Declaration of the functions are stored in header file. (.h extension)

5 Predefined Function Header files Functions Descriptions math.h
pow(x, y) Raise to power. sqrt(x) Compute square root. floor(x) Round down value. ceil(x) Round up value. abs(x) Absolute value string.h strcpy(x,y) Copy sequence of character strcmp(x,y) To compare two sequence of character strcat(x,y) To concatenate sequence of character

6 Function call: is used to call or to invoke a function.
Predefined Function # include<iostream.h> # include<math.h> void main() { int x,y,z; y = 2, z = 4; x = pow(z,y); cout<<“x:”<<x; } Function call: is used to call or to invoke a function. z and y are called parameters or arguments of the function pow. Function call

7 Variables

8 Variables Two types of variables: Local variable Global variable

9 Variables – local variable
Variable that is declared within a body of a function definition. Variable that is declared within the main() function are said to be local to main function.

10 Variables – local variable
Example: Local variable #include <iostream.h> void main() { int a; }

11 Variables – global variable
Variable that is declared outside and above main function. Accessible to all function in the program. It is not wise to use global variables any more than you have to.

12 Variables – global variable
Example: global variable #include <iostream.h> int a; void main() { }

13 User – defined function

14 User-defined function
Functions that are defined / written by the programmer / user in a program. Characteristics of user-defined function: A function name is named with unique name. Can be called from other function A function performs a specific task. Task is a discrete job that the program must perform as part of its overall operation. A function is independent. A function may receive values from the calling program (function call). A function may return value to the calling program (function call).

15 User-defined function
In general a function has the following syntax: returnValueType functionName(list of parameters) { }

16 User-defined function
void/ value returning function. With/ without parameters. returnValueType functionName(list of parameters) { }

17 User-defined function
To create a user defined function, you must write the following: Function prototype (function’s declaration) Function definition Function call If the function definition is before the main, you do not need the function prototype.

18 Function prototype Is used to declare a function. Problem statement: to create a function to calculate total of 2 numbers. Steps: Identify function name. void/ value returning function? With/ without parameter?

19 void function without parameter void function with parameter
Function prototype void function without parameter void function with parameter Value returning function, without paramter. Value returning function, with parameter. void calTotal(); void calTotal(int a, int b); int calTotal(); int calTotal(int a, int b);

20 Is placed in between preprocessor directives and main function.
Function prototype Is placed in between preprocessor directives and main function. #include <iostream.h> void calcTotal (); void main() { }

21 Function definition Is a block of code (statements) that perform specific task. Statements are usually declaration and/or executable statements.

22 void function without parameter
General syntax:

23 void function without parameter
Example: parameter_list return_type function_name void calTotal() { int a, b, result; a = 1, b = 2; result = a + b; cout<<result; } Function header function_body

24 void function without parameter
Function call: is used to invoke/ to call a function to execute task. Is placed inside main function. Example: void main() { calTotal(); }

25 void function without parameter
Full code: #include<iostream.h> void calTotal(); void main() { calTotal(); } Function prototype Function call Function header void calTotal() { int a, b, result; a = 1, b = 2; result = a + b; cout<<result; } Function definition

26 void function without parameter
Exercise 1: using void function, write a program to calculate an average of 3 numbers.

27 void function with parameter
Parameter is ways how calling function and called function are communicated. Through parameter, data can pass from calling function to called function and from called function to calling function. Formal parameters are optional Two types of parameter: Value parameter Reference parameter

28 void function with parameter
Value parameter A formal parameter that receives a copy of the content of the corresponding actual parameter. Reference parameter* A formal parameter that receives the location of the corresponding parameter.

29 void function with parameter (value)
void calTotal(int x, int y) { int result; result = x + y; cout<<result; } Formal parameter

30 void function with parameter (value)
#include<iostream.h> void calTotal(int, int); void main() { int a, int b; a = 2, b = 3 calTotal(a, b); } void calTotal(int x, int y) { int result; result = x + y; cout<<result; } Actual parameter

31 void function with parameter (value)
Example: void findStatus(int); void main() { int mark; cout<<“enter mark:”; cin>>mark; findStatus(mark); } void findStatus(int value) { if (value>=80) cout<<“excellent”; else if(value>=60) cout<<“good”; else if(value>=50) cout<<“pass”; else cout<<“fail”; }

32 void function with parameter (value)
Exercise 2: using void function with parameter, write a program that require a user to input 2 numbers and determine the maximum number.

33 Value returning function
Is a function that after completing its assigned task, return precisely one value to the calling function (function call). Why we return a value? To use it for further calculation or process. Is used in an assignment statement or in an output statement.

34 Value returning function
Function definition: Return type or data type int calTotal(int x, int y) { int result; result = x + y; return result; }

35 Value returning function
#include<iostream.h> int calTotal(int, int); void main() { int a,b,z; a = 2, b = 3 z = calTotal(a, b); cout<<“total :”<<z; } Function prototype Function call: Assignment statement int calTotal(int x, int y) { int result; result = x + y; return result; }

36 Value returning function
#include<iostream.h> int calTotal(int, int); void main() { int a,b; a = 2, b = 3 cout<<“total :”<< calTotal(a, b); } Function call: Output statement int calTotal(int x, int y) { int result; result = x + y; return result; }

37 Value returning function
Exercise 3: using value returning function, write a program to determine maximum value of two numbers.

38 Value returning function
Exercise 4: using value returning function write a program to calculate an average of three numbers.

39 Function (reference parameter)

40 Function – reference parameter
If a formal parameter is a reference parameter It receives the address (memory location) of 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 Can pass one or more values from a function Can change the value of the actual parameter

41 Function – reference parameter
A reference parameter receives and stores the address of the corresponding actual parameter Reference parameters are useful in three situations: Returning more than one value Changing the actual parameter When passing the address would save memory space and time.

42 Function – reference parameter

43 Function – value parameter
#include <iostream.h> void addFour(int a, int b); void main() { int num1,num2; num1 = 3; num2 = 1; cout<< num1<<num2<<endl; addFour(num1,num2); cout<<num1<<num2<endl; } void addFour(int a, int b) { a = a + 4; b++; cout<<a<<b<<endl; } Value parameter In main BEFORE calling addFour function In main AFTER calling addFour function In addFour function num1 3 a 7 num1 3 num2 1 b 2 num2 1

44 Function – reference parameter
#include <iostream.h> void addFour(int& a, int b); void main() { int num1,num2; num1 = 3; num2 = 1; cout<< num1<<num2<<endl; addFour(num1,num2); cout<<num1<<num2<endl; } void addFour(int &a, int b) { a = a + 4; //line 1 b++; //line 2 cout<<a<<b<<endl; } Reference parameter When the value of a changes, value num1 changes as well Reference parameter In main BEFORE calling addFour function In main AFTER calling addFour function In addFour function num1 3 a 7 num1 7 num2 1 b 2 num2 1

45 Function – reference parameter
How it works? In main BEFORE calling addFour function In addFour function num1 3 a num2 1 b 1 Before statement in line 1 execute!!

46 In main BEFORE calling addFour function In addFour function num1 7 a num2 1 b 2 In function addFour, after statement in line 1 and line 2 execute!!

47 Scope of a local and global variable
#include <iostream.h> void calTotal(); void main() { int a; } void calTotal() { int a; } Variable a in function main() is different from variable a in Function calTotal()

48 Global variable #include <iostream.h> void calTotal(); int num1; void main() { int num2; total = num1 + num2 } void calTotal() { int num2; total = num1 – num2; } Variable num1 is a global variable that are accessible to all function definitions in the file.

49 Summary Void function: a function that does not have a data type A return statement without any value can be used in a void function to exit function early The heading of a void function starts with the word void To call a void function, you use the function name together with the actual parameters in a stand-alone statement

50 Summary (continued) Two types of formal parameters: value parameters and reference parameters A value parameter receives a copy of its corresponding actual parameter A reference parameter receives the address (memory location) of its corresponding actual parameter

51 Summary (continued) If a formal parameter needs to change the value of an actual parameter, in the function heading you must declare this formal parameter as a reference parameter Variables declared within a function (or block) are called local variables Variables declared outside of every function definition (and block) are called global variables

52 Summary (continued) If a formal parameter needs to change the value of an actual parameter, in the function heading you must declare this formal parameter as a reference parameter Variables declared within a function (or block) are called local variables Variables declared outside of every function definition (and block) are called global variables

53 Thank You !


Download ppt "FUNCTION CSC128."

Similar presentations


Ads by Google