subroutines and parameter passing
play

Subroutines and Parameter Passing ECE2893 Lecture 5 ECE2893 - PowerPoint PPT Presentation

Subroutines and Parameter Passing ECE2893 Lecture 5 ECE2893 Subroutines and Parameter Passing Spring 2011 1 / 10 C/C++ Subroutines (Functions) ECE2893 Subroutines and Parameter Passing Spring 2011 2 / 10 C/C++ Subroutines (Functions)


  1. Subroutines and Parameter Passing ECE2893 Lecture 5 ECE2893 Subroutines and Parameter Passing Spring 2011 1 / 10

  2. C/C++ Subroutines (Functions) ECE2893 Subroutines and Parameter Passing Spring 2011 2 / 10

  3. C/C++ Subroutines (Functions) Like almost all programming languages, C and C++ allow and 1 encourage the use of subroutines , which are often called functions in C/C++. ECE2893 Subroutines and Parameter Passing Spring 2011 2 / 10

  4. C/C++ Subroutines (Functions) Like almost all programming languages, C and C++ allow and 1 encourage the use of subroutines , which are often called functions in C/C++. Functions in C/C++ allow decomposition of a problem into smaller 2 sub–problems that are easier to comprehend and debug. ECE2893 Subroutines and Parameter Passing Spring 2011 2 / 10

  5. C/C++ Subroutines (Functions) Like almost all programming languages, C and C++ allow and 1 encourage the use of subroutines , which are often called functions in C/C++. Functions in C/C++ allow decomposition of a problem into smaller 2 sub–problems that are easier to comprehend and debug. Functions in C/C++ (optionally) return a value. This is analagous 3 to an algebraic function F ( x , y ) = x 2 + 2 y + 3, that computes a value given inputs (independent variables) x and y . ECE2893 Subroutines and Parameter Passing Spring 2011 2 / 10

  6. C/C++ Subroutines (Functions) Like almost all programming languages, C and C++ allow and 1 encourage the use of subroutines , which are often called functions in C/C++. Functions in C/C++ allow decomposition of a problem into smaller 2 sub–problems that are easier to comprehend and debug. Functions in C/C++ (optionally) return a value. This is analagous 3 to an algebraic function F ( x , y ) = x 2 + 2 y + 3, that computes a value given inputs (independent variables) x and y . Functions must be declared before they can be called. More on 4 this later. ECE2893 Subroutines and Parameter Passing Spring 2011 2 / 10

  7. C/C++ Subroutines (Functions) Like almost all programming languages, C and C++ allow and 1 encourage the use of subroutines , which are often called functions in C/C++. Functions in C/C++ allow decomposition of a problem into smaller 2 sub–problems that are easier to comprehend and debug. Functions in C/C++ (optionally) return a value. This is analagous 3 to an algebraic function F ( x , y ) = x 2 + 2 y + 3, that computes a value given inputs (independent variables) x and y . Functions must be declared before they can be called. More on 4 this later. Functions obviously can call other functions. This happens 5 frequently in most C/C++ programming. ECE2893 Subroutines and Parameter Passing Spring 2011 2 / 10

  8. C/C++ Subroutines (Functions) Like almost all programming languages, C and C++ allow and 1 encourage the use of subroutines , which are often called functions in C/C++. Functions in C/C++ allow decomposition of a problem into smaller 2 sub–problems that are easier to comprehend and debug. Functions in C/C++ (optionally) return a value. This is analagous 3 to an algebraic function F ( x , y ) = x 2 + 2 y + 3, that computes a value given inputs (independent variables) x and y . Functions must be declared before they can be called. More on 4 this later. Functions obviously can call other functions. This happens 5 frequently in most C/C++ programming. A function can be called in C/C++ simply by having a single 6 statement with the function name and arguments. More on this later. ECE2893 Subroutines and Parameter Passing Spring 2011 2 / 10

  9. C/C++ Subroutines (Functions) Like almost all programming languages, C and C++ allow and 1 encourage the use of subroutines , which are often called functions in C/C++. Functions in C/C++ allow decomposition of a problem into smaller 2 sub–problems that are easier to comprehend and debug. Functions in C/C++ (optionally) return a value. This is analagous 3 to an algebraic function F ( x , y ) = x 2 + 2 y + 3, that computes a value given inputs (independent variables) x and y . Functions must be declared before they can be called. More on 4 this later. Functions obviously can call other functions. This happens 5 frequently in most C/C++ programming. A function can be called in C/C++ simply by having a single 6 statement with the function name and arguments. More on this later. A function can also be called as a term in an expression. More on 7 this later also. ECE2893 Subroutines and Parameter Passing Spring 2011 2 / 10

  10. Declaring Functions ECE2893 Subroutines and Parameter Passing Spring 2011 3 / 10

  11. Declaring Functions A function must be declared before it can be called. This is 1 analogous to the need to declare a variable before it is referenced. ECE2893 Subroutines and Parameter Passing Spring 2011 3 / 10

  12. Declaring Functions A function must be declared before it can be called. This is 1 analogous to the need to declare a variable before it is referenced. Functions can be declared in one of two ways. 2 ECE2893 Subroutines and Parameter Passing Spring 2011 3 / 10

  13. Declaring Functions A function must be declared before it can be called. This is 1 analogous to the need to declare a variable before it is referenced. Functions can be declared in one of two ways. 2 Use a function prototype , such as: 1 int Average(int d[], int L); Notice we specfiy the return type ( int in this case), the name of the function ( Average in this case), the list of parameters to the function, followed by a semicolon. This tells the compiler that such a function exists and can be called, but the actual implementation of Average will be done elsewhere. ECE2893 Subroutines and Parameter Passing Spring 2011 3 / 10

  14. Declaring Functions A function must be declared before it can be called. This is 1 analogous to the need to declare a variable before it is referenced. Functions can be declared in one of two ways. 2 Use a function prototype , such as: 1 int Average(int d[], int L); Notice we specfiy the return type ( int in this case), the name of the function ( Average in this case), the list of parameters to the function, followed by a semicolon. This tells the compiler that such a function exists and can be called, but the actual implementation of Average will be done elsewhere. Provide a function implementation , such as: 2 int Average(int d[], int L) { int i = 0; // Index variable // Remainder omitted for brevity } ECE2893 Subroutines and Parameter Passing Spring 2011 3 / 10

  15. Declaring Functions A function must be declared before it can be called. This is 1 analogous to the need to declare a variable before it is referenced. Functions can be declared in one of two ways. 2 Use a function prototype , such as: 1 int Average(int d[], int L); Notice we specfiy the return type ( int in this case), the name of the function ( Average in this case), the list of parameters to the function, followed by a semicolon. This tells the compiler that such a function exists and can be called, but the actual implementation of Average will be done elsewhere. Provide a function implementation , such as: 2 int Average(int d[], int L) { int i = 0; // Index variable // Remainder omitted for brevity } If function A calls function B , and B also calls A , then a prototype 3 Must be used for one of the two. ECE2893 Subroutines and Parameter Passing Spring 2011 3 / 10

  16. Declaring Functions A function must be declared before it can be called. This is 1 analogous to the need to declare a variable before it is referenced. Functions can be declared in one of two ways. 2 Use a function prototype , such as: 1 int Average(int d[], int L); Notice we specfiy the return type ( int in this case), the name of the function ( Average in this case), the list of parameters to the function, followed by a semicolon. This tells the compiler that such a function exists and can be called, but the actual implementation of Average will be done elsewhere. Provide a function implementation , such as: 2 int Average(int d[], int L) { int i = 0; // Index variable // Remainder omitted for brevity } If function A calls function B , and B also calls A , then a prototype 3 Must be used for one of the two. A function can be declared with void return type, indicating the 4 function in fact does not return a value. ECE2893 Subroutines and Parameter Passing Spring 2011 3 / 10

  17. Calling Functions ECE2893 Subroutines and Parameter Passing Spring 2011 4 / 10

  18. Calling Functions A function can be called by simply using the function name in a 1 program, such as: PrintLn("GCD(", m[i], "," ,n[i], ") = ", gcd); The PrintLn function has been declared and implemented in ece2893.h . ECE2893 Subroutines and Parameter Passing Spring 2011 4 / 10

  19. Calling Functions A function can be called by simply using the function name in a 1 program, such as: PrintLn("GCD(", m[i], "," ,n[i], ") = ", gcd); The PrintLn function has been declared and implemented in ece2893.h . A function that returns a value (a return type of anything except 2 void ) can be used anywhere a variable name or constant is expected, such as: PrintLn("GCD(", m[i], ",", n[i], " = ", Euclid(m[i], n[i])); Notice the call to the Euclid function as a parameter to the PrintLn function. ECE2893 Subroutines and Parameter Passing Spring 2011 4 / 10

Recommend


More recommend