Lecture 12 Modular Programming with Functions Learning Objectives: Understand the purpose of functions Understand how to use functions and the vocabulary Write your own functions 1
Modularity (the purpose of functions) 1. Doing the same thing more than once: Say you are writing a program that will need to calculate the factorial of a number in 25 different places in the program. If you write some code to calculate the factorial, you can copy it to the other 24 places. But what happens when you find an error in that code? You ve got to correct it in 25 different places. Plus, this can make your program very long. Instead, use a function for calculating the factorial. 2. Readability of your Program: Say you are writing a long program, such as MS-Word. What if you need to modify the part that underlines words. Do you want to scroll through thousands of lines of code to find the right area in the program to modify? Instead use functions and classes which separate the code into sections that you can easily find. This way, if your functions are named well, you can read main() and easily see how the whole program works. 2
What is a function? Some code located outside of main() that you can use within main(). You ve already used a lot of functions that C/C++ has available to you. Example: log(x) Returns the natural logarithm of x What do I mean by returns the ln? The function call becomes that number Here, x is the argument If we had the code: double y=2.3, a=1.2, answer; answer = log(y); printf( %lf \n, answer); answer = log(a); printf( %lf \n, answer); Would this work? When calling functions, you can use different variables for the arguments. For log(), use whatever variable or number you want to find the ln of. 3
Examples of other Functions that you ve seen 1. pow(x,i) Notice this has 2 arguments, both need to be numbers (double or integer = the base and the exponent) and it will return a double. 2. a.push_back(n); This is a function of the vector class, thus you call it with a dot. It doesn t return anything, but changes the vector by adding the value of n to it. 3. printf( n = %i, n); printf is a function that can have many arguments or just one. It also doesn t return anything. 4. i < a.size(); size() is a vector function that returns the current size of the vector. It has no arguments, but still needs the parentheses. Notice that functions always have parentheses 4 after them.
Example (The entire function is placed after the } at the end of the program) //This function calculates the i th Fibonacci number int fibonacci(int i) { } int prev2 = 0, prev = 1, f = 0; for(int j=1; j<i; j++) { } f = prev + prev2; prev2 = prev; prev = f; return f; Function Syntax Note: This is just like a mini program! Instead of int main(), you ve got your function header. 5
Function Syntax Calling this function from within main(): int f, a=10; f = fibonacci(a); Notice how the argument is not called i even though you used i within the function. This is just like calling log(a)! Remember that!!!! Function Header (1 st Line): Note that you declare your parameters(arguments) inside the parentheses in the function header. int fibonacci( int i ) type of value to return parameter type=declaration of i name of function (you decide this) parentheses needed even if no parameters variable name for parameter (you also decide this) 6
Function Prototype Most of the time, you put your functions below the main() function in your program. Problem: the program starts executing at main() and at this point hasn t seen the function, so will be confused when you call the function. To get around this, use a function prototype: A function prototype is just the function header (1 st line of your function) copied and placed above main(). The only thing you need to change is to add a semicolon at the end of the line. Since the function prototype is above main(), the whole program can use this function (including other functions besides main) Example: Function prototype for the function fibonacci: int fibonacci(int i); 7
How to Write a Function 1. Decide what you want your function to do. Does it calculate a value, does it print something out? This will determine the return type and the code in the function. 2. Decide what values it will need in order to complete its task. For example, if you are calculating the area of a circle, you will need a radius or diameter. This will determine your parameters/arguments. 3. Start below the last brace of main() and make your function header. 4. Put curly braces after the header. 5. Fill in the code 6. Copy the header to the area above main() and put a semicolon after it. 7. Call the function within main. 8. Test your function with known values! 8
#include <stdio.h> Another Example double cylindervolume(double r, double h); int main() { double rad=2.6, height=8.9; //radius and height } printf( The volume is %lf \n, cylindervolume(rad, height)); //This function will take a radius and height and calculate the volume of a cylinder. double cylindervolume(double r, double h) { return 3.14159*r*r*h; } 9
What if I don t want to return a value? use void //This function writes an error message to the screen: void errmessage() { printf( You ve entered an invalid number ); return; //note return used without a value after it } Note that return will end the function. void means the function is not returning a value. To use this within main: errmessage( ); This is just like calling printf()! Remember that!!!! 10
Examples Create a function prototype and write a line to call a function that: 1. Simulates rolling a die. 2. Calculates the area of a triangle. 3. Calculates the magnitude of a resultant vector 11