FUNCTION CSC128
User – defined Function Contents Predefined Function User – defined Function
Predefined function
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)
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
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
Variables
Variables Two types of variables: Local variable Global variable
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.
Variables – local variable Example: Local variable #include <iostream.h> void main() { int a; … }
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.
Variables – global variable Example: global variable #include <iostream.h> int a; void main() { … }
User – defined function
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).
User-defined function In general a function has the following syntax: returnValueType functionName(list of parameters) { … }
User-defined function void/ value returning function. With/ without parameters. returnValueType functionName(list of parameters) { … }
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.
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?
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);
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() { … }
Function definition Is a block of code (statements) that perform specific task. Statements are usually declaration and/or executable statements.
void function without parameter General syntax:
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
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(); }
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
void function without parameter Exercise 1: using void function, write a program to calculate an average of 3 numbers.
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
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.
void function with parameter (value) void calTotal(int x, int y) { int result; result = x + y; cout<<result; } Formal parameter
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
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”; }
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.
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.
Value returning function Function definition: Return type or data type int calTotal(int x, int y) { int result; result = x + y; return result; }
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; }
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; }
Value returning function Exercise 3: using value returning function, write a program to determine maximum value of two numbers.
Value returning function Exercise 4: using value returning function write a program to calculate an average of three numbers.
Function (reference parameter)
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
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.
Function – reference parameter
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
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
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!!
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!!
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()
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.
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
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
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
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
Thank You !