Presentation is loading. Please wait.

Presentation is loading. Please wait.

Functions. What is a function? It’s a group of statements that has a specific purpose and can be be repeatedly executed as needed. By using functions:

Similar presentations


Presentation on theme: "Functions. What is a function? It’s a group of statements that has a specific purpose and can be be repeatedly executed as needed. By using functions:"— Presentation transcript:

1 Functions

2 What is a function? It’s a group of statements that has a specific purpose and can be be repeatedly executed as needed. By using functions: We can write less code, a lot less We can reuse our code, including in different programs We improve readability We can divide our code between different files It’s the foundation of code sharing in projects, where there are multiple developers etc. 20152

3 What makes a function return_type FunctionName(parameters) { function statements; } Functions have prototypes. They are placed before the main() function return_type FunctionName(parameters); 20153

4 main() is also a function int main(void) { printf("Hello world\n"); return 0; } 20154

5 Function properties Function prototype Return type void (no return) int (function must return an integer) double (function must return a double) char (function must return a character) e.t.c. Functions have headers (with parameters) Functions have calls (with arguments) 20155

6 Sample (1) – passing an argument #include void PrintNum(int number); // function prototype int main(void) // main() function header with return type and no parameters { int nr; printf(“Hello, give me a number!\n"); scanf("%d", &nr); PrintNum(nr); // call function PrintNum() with arguments return 0; // main function return } void PrintNum(int number) // PrintNum() function header with return type and parameters { printf(“You gave me: %d", number); } 20156

7 Sample (2) – multiple parameters #include void greet(int age, char name[]); //function prototype int main(void) { int number; char name[15]; printf(“Hi, what’s Your name?\n"); scanf("%s", name); printf("How old are You?\n"); scanf("%d", &number); greet(number, name); //call to function greet(), passing 2 arguments return 0; } void greet(int age, char name[]) // function greet() header, taking 2 parameters { printf(“Hi, %s. You’re %d year(s) old.", name, age); } 20157

8 Using variables in functions Every variable and their value has a lifetime and visibility scope! The variable and its value can only be used within the function it is created in The variables declared inside of a function will be “forgotten” after the function terminates. This also happens to the values they stored. The same variable names can be used in different functions – this doesn’t make them the same 20158

9 Copy or an original? (simplified) Copies: single variables By changing the value in a subfunction, the original stays unchanged int number; char singleLetter; float itemCost; Original data: arrays By changing a member of an array in a subfunction, the original is modified int numArray[10]; char name[15]; float invoiceItemCosts[5]; 20159 We can pass multiple values to a function, but only return It’s important to know whether the value passed is a copy or related to the original data (by value, by reference)

10 Sample (3) – code reuse #include void printArray(int numArray[], int arrayLength); int main(void) { int firstNumArray[] = {5, 8, -2, 65, 1, 36}; int secondNumArray[] = {7, 97, -24, -74}; printArray(firstNumArray, sizeof(firstNumArray) / sizeof(int)); printArray(secondNumArray, sizeof(secondNumArray) / sizeof(int)); return 0; } void printArray(int numArray[], int arrayLength) { int i; for(i = 0; i < arrayLenght; i++) { printf("%d\n", numArray[i]); } 201510

11 Sample (4) – function returning a value #include int giveRandomNum(int numMin, int numMax); int main(void) { int num; srand(time(NULL)); num = giveRandomNum(5, 25); // function call and storing the result of the return printf("Got num %d\n", num); num = giveRandomNum(75, 100); // function call and storing the result of the return printf("Got num %d\n", num); return 0; } int giveRandomNum(int numMin, int numMax) // a function returning an integer { return (rand() % (numMax - numMin + 1)) + numMin; // return } 201511

12 Lab task 1 (calling functions with parameters) This task will use the voluntary homework that we gave you as base Let’s improve on our long and cumbersome main() function and divide it into multiple functions 1.Output (without return, code reuse) 2.Sorting (without return) 3.Combining the warehouses (without return) If You haven’t done the task, You can use our base code from the web 201512

13 Advanced lab task 1 Ask for how many items the warehouse will store (in between EMPTY … MAX_UNITS) Use the same function for both of the warehouses, choose a return type. Use a function to fill the warehouses with cargo. Use the same function for both of the warehouses, choose suitable arguments. The function used to fill the warehouses should also call out a function to generate the cargo main() calls the fill function, the fill function calls the function to generate a random number Create a limit for the cargo exiting the Intermediary storage (so-called sorting center). E.g. a truck can deliver up to 1500kg at once. Fit heavier items first, but how many as possible – e.g. when one item doesn’t fit, look for smaller items that may still fit. 201513

14 Lab task 2 (functions with return) Create a program that will: Ask the user for 3 numbers, all greater than 0 Find the sum of the number Find the average of the numbers (don’t lose the fractions!) Find the greatest number Find the smallest number All of the functions must return the result to the main() function, where they will be printed out. There can’t be any output in the functions themselves 201514

15 Advanced lab task 2 Modify the functions so they would work with N digits. Instead of 3 numbers, use N numbers. Create a menu structure where the user can select the action and if necessary, create new numbers. Use random numbers for generation Find both of the extreme values in the same function. No sorting allowed. Also remember, that you can only return a single value (find help in the by reference sample) 201515


Download ppt "Functions. What is a function? It’s a group of statements that has a specific purpose and can be be repeatedly executed as needed. By using functions:"

Similar presentations


Ads by Google