Chapter 1 Section 1.4 Subprograms or functions 0
Functions Functions are essential in writing structured and well-organized code. Functions help for code to be reused. Functions help to reduce errors and bugs. When to create a function: When you repeat to write the same code multiple times If a function gets too long, split the code into several smaller functions and call these smaller functions instead. Write a function for a logical self-contained section of code, For example: calculation the maximum of two variables. 1
Example of a function return type function name parameters function body int square(int num) { int result; result = num * num return result; Function name: Use a name that meaninfully describes what your function does return type: the data type your function produces, for example: int, char, etc. parameters: these are 0..n variables that are passed to the function function body: this is the code of the function. You can use return to exit there function and a variable or value, like result if your function returns a value. 2
Function Components A function consists of the following components: Function header: it consists of the function type, the function s name (an identifier), optionally parameters, a pair of parentheses. int slicecake( double cakeradius, int count ) {... Type: the type of the function s return value. Type void: This means that the function does not return a value. name: the name of the function describes what the function does parameters: data can be passed into and even out of a function. Parameters are optional and can be left out. Function body or function block: { and the braces mark the beginning and the end of the function. The task that the function performs must be written inside this block. 3
Questions? 1. Can you have a function that has no parameters and does not return anything? 2. Can a function return multiple values? 3. How many statements are allowed inside the function body? 4. Write a function that adds two int numbers. What would that function look like? 4
Answers 1. Sure, you can but it may not be useful most of the time. The function would look like this: void donothing() {... 2. No, each function may return none (void as return type) or a single value. 3. As many as you like but each function should only perform one task or one idea. 4. The function return type would be int, and we need two parameters for the two numbers to be added which also would be of type int: int add(int num1, int num2) { return num1 + num2; 5
Examples of function signatures // this function returns int and has no parameters int readnumber() {... // this function the average of the three given numbers double calculateavg(double num1, double num2, double num3) {... // this function calculates the maximum value in the given array int calcmax(int[] numberarray) {... // this function prints the name, gender, and age to the console void printperson(char[] name, char gender, int age) {... 6
Function Body The function body is the code inside a function. The body is surrounded by braces, { and The return keyword is used to stop execution of the function and returns a value of the defined return type, for example: int getmax(int num1, int num2) { if (num1 > num2) { return num1; else { return num2; Note: If the function's return type is void, using return; without a value is enough. 7
Comments Comments are used to make notes or describe code Multi line comments start with /* and end with */ Single line comments start with // and end with the end of the line Comments are ignored by the compiler A good programmer uses comments to describe his code to make it easier for others to understand 8
Comments Example /* This program asks the user for two numbers and prints out the maximum of these two numbers */ // declare variables int num1, num2; int result; // ask user for input num1 = readinput(); num2 = readinput(); // calculate maximum result = calculatemax(num1, num2); printf("%d", result); 9
Main Function The main() function is special as it defines the start of the program. int main() { // // write your code here // return 0; The return type must be int. Return 0 at the end of your program to show the compiler that everything is fine. We will not go into the details of the return value in this chapter. 10
Calling a function We learned how to write a function but how to call and execute it? A function call consists of the function name followed by ( and ). If the function has parameters, we also need to pass in the arguments that match the expected parameters. For example: // this function adds two numbers int add(int num1, num2) { return num1 + num 2; /* we just write the function name and pass in two numbers as arguments */ add(1, 2); 11
Calling a function Every function must be declared before we can call it. Therefore, it is good practice to declare all functions before the main() function. On the right is an example of a nice program that calls multiple functions. Note: the function bodies are left empty for clarity: // this function adds two numbers int add(int num1, num2) {... // ask the user for a number int readinput() {... // print the result to the console void print(int result) {... int main() { int num1, num2, result; num1 = readinput(); // calling readinput() above num2 = readinput(); result = add(num1, num2); // calling add(..) above print(result); // calling print(..) above 12
Questions? 1. Do you need to write the return type when you call a function? 2. Do you have to assign the returned value to a variable? 3. Do you need a cell phone to call a function? 13
Answers 1. No, you don't write the return type when calling a function but the return type in the function's declaration helps you to know what you can expect. 2. No, if you have a function like int add(int num1, num2) {... which returns int, you can call it and assign the return value or you could just ignore it: int main() {... result = add(1, 5); // assign returned value to result add(1, 5); // ignore returned value... 3. No, you don't need a cell phone, you don't even need network coverage! 14