Presentation is loading. Please wait.

Presentation is loading. Please wait.

CGS 3460 Functions – Definition n One named program module that does one primary task n It consists of a set of statements that have been grouped together.

Similar presentations


Presentation on theme: "CGS 3460 Functions – Definition n One named program module that does one primary task n It consists of a set of statements that have been grouped together."— Presentation transcript:

1 CGS 3460 Functions – Definition n One named program module that does one primary task n It consists of a set of statements that have been grouped together and given a name.

2 CGS 3460 Functions – Motivation n Break a large problem into smaller parts n Easier to read, manage, and maintain. n A function can often be reused in other programming problems. n Functions help reduce redundancy and make programs shorter. n Examples lmain lprintf lscanf

3 CGS 3460 Declaring and Defining n Declaring a function – must be done before main if used return_value_type function_name( parameter-list); n How to define a function lIf you declare the function, you can define it after main lIf you don’t declare the function you must define it before main return_value_type function_name( parameter-list) { Declarations & Definitions; Statements; } n Parameter-list format type1 variable_name1, type2 variable_name2,…typeN variable_nameN

4 CGS 3460 Examples n Definitions int main() { …;} double calcMax(double a, double b) {…;} n Special cases lUse void for return_value_type if no value is needed to be returned. lDon’t need to put anything for parameter-list if no parameters are needed to pass to the function (you can add void if you like)

5 CGS 3460 Arguments n Arguments: lSpecific values for a particular function call lParameters – variables passed in to a function n Example double CalcMax(double a[10]); lThe values assigned to the array a are passed to the function at runtime lIncrease usefulness and flexibility

6 CGS 3460 Function – III n How to return a value in a function lreturn; // for void return type lreturn expression; // to return the value to expression to the caller n How to call/invoke a function lfunction_name(…); // for function with no return value lvariable_name = function_name(…); // for function with return value

7 CGS 3460 Calculating Area - Example n Create a function to calculate the area of a rectangle given its length and width lWhat does it need to calculate the area? Length floating point type Width floating point type lWhat will it give back Area floating point type CalcArea( ); length,width double

8 CGS 3460 Example: Area Calculation - 1 double CalcArea(double height, double width) { return height * width; } int main() { double h, w, area; printf(“Please input height and width\n”); scanf(“%f, %f”, &h, &w); area = CalcArea(h, w); printf(“The area is %f”, area); return 0; }

9 CGS 3460 Example: Area Calculation - 2 double CalcArea(double height, double width) ; int main() { double h, w, area; printf(“Please input height and width\n”); scanf(“%f, %f”, &h, &w); area = CalcArea(h, w); printf(“The area is %f”, area); return 0; } double CalcArea(double height, double width) { return height * width; }

10 CGS 3460 Calculating Squareroot of x - Example n Create a function to calculate the squareroot of a number lWhat does it need to calculate the area? Value lWhat will it give back Square root of x n Pseudocode 1.Set the value of guess to 1 2.If |guess 2 - x| <, proceed to step 4 3.Set the value of guess to (x / guess + guess)/2 and return to 2 4.The guess is the approximation of the square root squareRoot( ); x double

11 CGS 3460 Example I – code float squareRoot (float x) { const float epsilon = 0.0001; float guess = 1.0; while ( guess * guess – x >= epsilon || guess * guess – x <= -epsilon ) guess = ( x / guess + guess ) / 2.0; return guess; }

12 CGS 3460 Calculating Factorial (5! = 5 * 4 * 3 * 2 * 1) n Create a function to calculate the factorial of a number lWhat does it need to calculate the area? Value lWhat will it give back x! factorial( ); x int

13 CGS 3460 Example – II n Compute the factorials int factorial(int n) { int result = 1; int i; for( i = 2; i <= n; i++) result = result * i; return result ; }

14 CGS 3460 Function that receives no input and returns no output n Don’t put anything in parenthesis (no arguments) n Put key word void as return type n return is optional void sayHelloHal () { printf (“Hello Hal \n"); } return;

15 CGS 3460 Function that receives input but returns no output n Print the square of an integer n Saves arguments in given variable n Put key word void as return type n return is optional void square (int x) { int ans = x * x; printf ("The square of %d is %d\n", x, ans); }

16 CGS 3460 Function that receives input and returns an output n This is the most common type of function. n Consider a square function that returns the squared value n Put actual return type n return is mandatory int square (int x) { return x*x; }

17 CGS 3460 Function that receives multiple inputs and returns an output n Consider a max function lTakes 2 values lReturns the max of the two n Put actual return type n return is mandatory int max (int x, int y) { return (x > y ? x: y); }

18 CGS 3460 Function that receives no input but returns an output n Rare n For the purpose of an example consider a function that returns the value of the speed of light n Put actual return type n return is mandatory int speedOfLight () { return 299792458; //speed of light in m/s }

19 CGS 3460 Pass by Value n Passing a copy of the value as an argument lParameters receive a distinct copy of the caller's arguments, as if the parameters were assigned from the arguments lChanges made to parameters have no effect on the caller’s arguments n Examples: h = 3; w = 4; area = CalcArea(h, w); double CalcArea(double height, double width) { …; } height = 3, width = 4 3, 4


Download ppt "CGS 3460 Functions – Definition n One named program module that does one primary task n It consists of a set of statements that have been grouped together."

Similar presentations


Ads by Google