Lecture 9
Function Mathematical function and C+ + function Input: arguments Output: return value
Sqrt() Square root function finds the square root for you It is defined in the cmath library, #include<cmath> Example: double sqrt_of_n = sqrt(n); Input: n Output:sqrt(n) sqrt function takes in a number, and returns the square root sqrt function is defined as double sqrt(double) sqrt function takes an input argument of type double sqrt function returns a value of type double
pow() pow function finds exponential calculation for you It is defined in the cstdlib library, #include<cmath> pow function take two number arguments type of double, and return the result is first number power second number which type of double. pow function defines as pow(double,double) Example: Pow(2,3) will return 8
Exercise Exercise 1: Take a user input number, print out the square root of this number. Exercise 2: Take a user input number, print out the forth root of this number. Exercise 3: Take two number n1, n2 input from user, print out the n1 n2. Exercise 4: Take a user input number, print out the cube root of this number.
Create our own functions Creating a function is much like declaring a variable, it has two parts 1. Prototype This gives the compiler a preview of what your function would look like This usually goes after using namespace std; and before main() 2. Definition This defines the actions the function should take This usually goes after the main() function
Model of function prototype return_type function_name (parameter_list); 1. return_type What the function will return 2. function_name Name of the function 3. parameter_list List of data type of parameter(s)
Model for function definition return_type function_name (parameter_list) { } code goes here parameter_list This parameter list will include the type and the name of the variable
Example of function,reading input Prototype/Header: int getnumber(); Definition: int getnumber() //matches above prototype/header { Calling the function: int main() { int n= getnumber(); return 0; } } int num; cout<< Enter a number: ; cin>>num; return num; /*Note there turn type of the function matches the variable in which the value will be stored.*/
Function s Return Value Function often serve very specific purposes. In our example it was to read in a value from the user. This function getnumber need to be able to communicate this newly obtained value back to the calling function. It does so with a return statement. This return statement is for transferring information from the sub function back calling function. The act of returning a value is done so through the keyword return. Returning a value is NOT the same as cout information to screen. Next example demonstrates a function that outputs to the screen,however does not return a value.
Example - output function Prototype/Header: void printnumber(int); Definition: Calling the function: int main() { void printnumber(int num) // matches above { } cout<<num <<endl; Note the function type is void,nothing is being returned } //gets a number from the user int n=getnumber(); //prints the number to screen printnumber( n ); return0;
Basics of Using Functions At the beginning of your program, declare the function. Somewhere in your program, define the function. Other functions can then call the function.
Example of 3 part #include <iostream> using namespace std; double avg(double x, double y); // Declare int main() { double a = 1.2; double b = 2.7; cout << "Avg is " << avg(a,b); // Call } double avg(double x, double y) // Define { double v = (x + y)/2; return v; }
Why use functions Organizational reason 1. Sometimes we have a lot to do in our program 2. Functions offers a way to break a part a large program into smaller sub programs. Think of a paragraph of text that is very long, if you lost your position, it is hard to find it again. Logical reason 3. A task might be performed repeatedly throughout different parts of the program 4. Instead of copying and pasting the same code into multiple places, we can replace that with a function. If we need to make modifications it is much harder to change it in multiple places Much easier to change it in just that one function
Designing of functions There are many different views on what is consider a well design function. There are even arguments on why functions should be used at all, poorly designed functions will use up a lot of system resources, when the function is called. Each function should do one thing, achieve one task. Functions should be short, not more than X number of lines long X being a number that the designer sees fit and it also depends on what the function needs to accomplish. Think of it as writing a paragraph,as soon as you complete presenting the idea then you are done.
Exercise Exercise 5: Write a function called printhello() that outputs Hello on the screen. Exercise 6: Write a function called circlearea() that takes an double value from parameter, calculates the area of the circle, returns the area of the circle (pi=3.14, circle area = pi * r^2) Exercise 7: Write a function called displayreverse() that takes an integer and displays the reverse of that number on the screen. Exercise 8: Write a function called numbertotens() that takes an integer and returns the corresponding number in one and zeros. For example: if the number is 54321, returns 10000.
Exercise Exercise 9. Write a program that defines and tests a factorial function. The factorial of a number is the product of all whole numbers from 1 to N. For example, the factorial of 5 is 1 * 2 * 3 * 4 * 5 = 120 Exercise 10. Write a program that finds the first prime number greater than 1 billion (1,000,000,000).