Presentation is loading. Please wait.

Presentation is loading. Please wait.

Week 4 – Functions Coding Functions. Purpose of Coding Functions A function is written to perform a well-defined task; rather than having all logic in.

Similar presentations


Presentation on theme: "Week 4 – Functions Coding Functions. Purpose of Coding Functions A function is written to perform a well-defined task; rather than having all logic in."— Presentation transcript:

1 Week 4 – Functions Coding Functions

2 Purpose of Coding Functions A function is written to perform a well-defined task; rather than having all logic in main you should split it into smaller functions and call these functions from main A function normally requires some data to be passed to it (parameters) A function normally returns a result to where it was called from in the program If no standard function exists for the task you need to do then you will have to code your own function

3 Function Syntax datatype functioname(datatype formal_parameter1, datatype formal_parameter2, …) { function coding } eg. double f1(int val, double num) { double num2; return n2; } a function may have a datatype or may be of type void a function may have 0, 1 or more formal parameters which are used to refer to values passed to a function. Each parameter must be defined as a datatype a function can contain within { } any valid C statements a function may or may not contain a return statement

4 Return Statement: Purpose A function normally returns a result Eg. the square root function should return the square root of the value passed to it; i.e. passing a value of 9 to the sqrt function should return a value of 3 The result from a function in C is returned using the return statement A function can have 0, 1 or more than one return statement If a function does not have a return statement the datatype of the function must be void; if a function uses a return the function must be defined with the datatype that it returns When a return statement is executed control is returned back to the statement from which the function was called

5 Calling a Function Execution always starts at the beginning of "main“ A function is called or “invoked” by using its name within a program Actual values or variables must be provided corresponding to the parameters if a function has parameters Example: double f1(int val, double num) { double res = val * num; return res; } main( ) double num2, num1 = 2.2; int num3 =3; num2 = f1(num3, num1); printf(“num2:%d\n”,num2); }

6 Function Example using Return Statement The getnum function does not require any information to be passed to it so it has no parameters and therefore its parameter list is void. However the function returns a value of datatype int and therefore must be defined as a function of datatype int. int getnum(void) { int num; do { printf("Enter number(>0):"); scanf("%d",&num); } while (num <= 0); return num; }

7 Program Example using Return int getnum(void) { int num; do { printf("Enter number(>0):"); scanf("%d",&num); } while (num <= 0); return num; } main() { int age, cont; do { age = getnum(); if (age < 19) printf (“Youth\n"); else if (age < 66) printf("Adult\n"); else printf("Senior\n"); printf("Do you want to continue(1(Y)/0(N))?\n"); scanf("%d",&cont); } while (cont == 1); }

8 More Information about Function main The operating system calls the main function, so the return value and parameters of main are used to communicate with the operating system int main (int argc, char *argv[]) could be used to call your program with arguments and to return an integer return code (or exit code or exit status) By Unix/Linux standards, an exit status of "0" indicates successful program execution, anything other than zero indicates failure Therefore main normally should return 0 to the operating system

9 Program Example using Return in main int getnum(void) { int num; do { printf("Enter number(>0):"); scanf("%d",&num); } while (num <= 0); return num; } int main( ) { int age, cont; do { age = getnum(); if (age < 19) printf (“Youth\n"); else if (age < 66) printf("Adult\n"); else printf("Senior\n"); printf("Do you want to continue(1(Y)/0(N))?\n"); scanf("%d",&cont); } while (cont == 1); return 0; }


Download ppt "Week 4 – Functions Coding Functions. Purpose of Coding Functions A function is written to perform a well-defined task; rather than having all logic in."

Similar presentations


Ads by Google