Presentation is loading. Please wait.

Presentation is loading. Please wait.

User-defined Functions

Similar presentations


Presentation on theme: "User-defined Functions"— Presentation transcript:

1 User-defined Functions
ELEC 206 Computer Tools for Electrical Engineering

2 Functions A program can be thought of as a collection of sub parts or sub tasks: input data analyze data output results In C++ these subtasks are called functions.

3 Functions Complex problems can be broken down into sub tasks, each of which is easy to implement in C++. What are some other advantages to using functions, as opposed to writing the entire solution in main? Multiple programmers Testing/Debugging/Maintaining Reduce duplication of code

4 Functions Pre-defined standard libraries User-defined

5 Pre-defined Functions - Example
#include <iostream> #include <cmath> using namespace std; int main() { double angle; cout << “input angle in radians: “; cin >> angle; cout << “\nthe sine of the angle is “ << sin(angle) << endl; return 0; }//end main

6 Cast Functions Return value as new data type,
Have no affect on the argument #include <iostream> using namespace std; int main() { int x=9, y=2; cout << x/y << endl; cout << (double)x/(double)y << endl; cout << (double)x/y << endl; cout << ((double)x)/y << endl; return 0; }//end main Output? 4.5 4

7 User-defined Functions Terminology
Function Prototype describes how a function is called Function Call Function Arguments used in the function call Function Definition function header function body Formal Parameters used in function definition Formal parameters must agree with arguments in order, number and data type, but the identifiers can be different.

8 User-defined Functions
Can be defined to perform a task (return no value) return a single value to the calling function change the value of multiple variables

9 Void Functions A void function may be called to
perform a particular task modify data perform input and output A void function does not return a value to the calling program return statement is optional if a return statement is used, it has the following form: return;

10 Example of void function
//output formatted date //function definition void print_date(int mo, int day, int year) //function header { string month; switch(mo) { case 1: month = “January”; break; case 2: month = “February”; case 12: month = “December”; }//end switch cout << month << ‘ ’ << day << “, << year << endl; return; //return is optional } //end print date

11 Value-returning Functions
A function returns a single value to the calling program The function header declares the type of value to be returned A return statement is required in the body of the function

12 Example: n! n! = n*(n-1)*(n-2)*…*1 n is a positive integer
0! is 1 by definition

13 Example - factorial function
//function definition: n! = n*(n-1)*(n-2)*…*1, // ! is 1 by definition //Function fact returns n! //Function fact assumes n is non-negative integer int fact(int n) //function header, NO SEMICOLON { int nfact = 1; while(n>1) { nfact = nfact*n; n--; }//end while block return(nfact); }//end fact

14 Calling a function #include <iostream> using namespace std;
int fact(int n); //function prototype, semicolon required // parameter identifier is optional int main() { int n; cin >> n; if(n>=0) cout<<n<<“! is ” <<fact(n)<<endl; //n is the argument else cout <<“factorial not defined for negative numbers” <<endl; return 0; }//end main

15 Calling a function- second example
int fact(int); //function prototype, //parameter identifier is optional #include <iostream> using namespace std; int main() { int n, factorial; cin >> n; if(n>=0) { factorial = fact(n); //function call cout << n <<“! is ” << factorial << endl; } else cout << “factorial not defined for negative numbers” << endl; return 0; }//end main

16 2 Points of Style When Writing Value-returning Functions
Formal parameters are used to pass information to the function. cin statements are usually not required. A return statement returns a value to the calling program. cout statements are usually not required. Use library functions as model (sin, log, etc)

17 Parameter Passing - pass by value
the default in C++ (except when passing arrays as arguments to functions) formal parameter receives the value of the argument changes to the formal parameter do not affect the argument

18 #include <iostream>
using namespace std; int fact(int); //function prototype int main() { int n, factorial; cin >> n; if(n>=0) { factorial = fact(n); //function call cout << n <<“! is “ << factorial << endl; }//end if return 0; }//end main int fact(int n) //function header, NO SEMICOLON { int nfact = 1; while(n>1) { nfact = nfact*n; n--; }//end while block return(nfact); } //end fact

19 Parameter Passing - pass by reference
append an & to the parameter data type in both the function prototype and function header void get_date(int& day, int& mo, int& year) formal parameter receives the address of the argument any changes to the formal parameter directly change the value of the argument

20 Example - pass by reference
#include <iostream> using namespace std; void swap(double&, double&); //function prototype int main() { double x=5, y=10; swap(x,y); //function call; x y are arguments cout >> “x = “ << x << ‘,’ << “ y= “ << y << endl; return 0; }//end main Output is: x = 10, y = 5

21 Example - pass by reference
//Function swap interchanges the values of two variables //function definition void swap(double& x, double& y) //function header { double temp; //local variable temp temp = x; x=y; y=temp; return; //optional return statement }//end swap

22 Practice! - What is the output?
#include <iostream> using namespace std; void fun(int&, int&, int); int main() { int c1=1, c2=2, c3=3; cout << c1 << ‘,’ << c2 << ‘,’ << c3 << endl; fun(c1,c2,c3); fun(c3, c2, c1); return 0; } void fun(int& a1, int& a2, int a3) { a1++; a2++; a3--; 1,2,3 2,3,3 2,4,4

23 Storage Class and Scope
Scope refers to the portion of the program in which it is valid to reference a function or a variable Storage class refers to the lifetime of a variable

24 Scope Local scope - a local variable is defined within a function or a block and can be accessed only within the function or block that defines it Global scope - a global variable is defined outside the main function and can be accessed by any function within the program file.

25 Storage Class - 4 types automatic - key word auto - default for local variables Memory set aside for local variables is not reserved when the block in which the local variable was defined is exited. external - key word extern - used for global variables Memory is reserved for a global variable throughout the execution life of the program. static - key word static Requests that memory for a local variable be reserved throughout the execution life of the program. The static storage class does not affect the scope of the variable. register - key word register Requests that a variable should be placed in a high speed memory register.

26 //Scope Example - What is the output
#include <iostream> using namespace std; void a(); //Function prototypes void b(); int x=1; //Global (file scope) int main() { int x=5; //Local to main cout << "before call to a, x= " << x << endl; cout << "global x is " << ::x << endl; a(); cout << "after call to a, x= " << x << endl; b(); cout << "after call to b, x= " << x << endl; cout << "after 2nd call to b, x= " << x << endl; return(0); } void a() { int x=25; //Local to a x++; void b() {

27 Scope Example - Output before call to a, x= 5 global x is 1
after call to a, x= 5 after call to b, x= 5 global x is 2 after 2nd call to b, x= 5 global x is 3

28 //Storage Class Example - What is the output
#include <iostream> using namespace std; void donothing(); int main() { donothing(); donothing(); return(0); }//endmain void donothing() { int x=0; static int y=0; x++; y++; cout <<“x is ” x << “, y is ” << y << endl; return; }//end donothing

29 Storage Class Example - Output
x is 1, y is 1 x is 1, y is 2 x is 1, y is 3


Download ppt "User-defined Functions"

Similar presentations


Ads by Google