1 / 23

Functions

Functions. Introduction. Most programs are much larger than you have worked with so far Its good practice to break these programs into smaller modules Makes program more manageable “Divide and Conquer” In C, these modules are called “Functions”. Why functions?.

shandi
Download Presentation

Functions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Functions

  2. Introduction • Most programs are much larger than you have worked with so far • Its good practice to break these programs into smaller modules • Makes program more manageable • “Divide and Conquer” • In C, these modules are called “Functions”

  3. Why functions? • Writing functions avoids rewriting the same code over and over. • IT become easy to keep track of what you are doing .

  4. Types of Functions • Programmer-defined functions • Already available functions (Standard Library)

  5. Function Result Parameters Function Input and Output • Return a value • Display or print • Modify memory

  6. How Functions work? main worker 1 worker 2 worker 3 worker 4 worker 5

  7. Function Mechanics • Functions generally have the following properties: • Each function has a name. • A function name is made up and assigned by the programmer and follows the naming rules. • Each function name has one set of parentheses immediately following it. • The body of each function, starting immediately after the closing parenthesis of the function name, must be enclosed by braces. • Function may take input. • Function may return the value. • Prototyping of the function.

  8. Example: function (without parameters) main() { message();//function gets called ; cout<<“\n cry and you stop the monotony!” } message() //function definition { cout<<“\n smile and the world smiles with you..” }

  9. Function Definition • Implementation of task to be performed by a function • Two parts: • Function signature • Function body int Square (int x) { return x * x; }

  10. Function Prototype • Just below pre-processor directives • Tells compiler • type of data returned by function • Number of parameters expected by function • Types of parameters • Order of parameters • Used to validate function call int Square (int);

  11. Function Call • Calling function where its functionality is required • Pass arguments as defined through parameter list int result; result = Square (a);

  12. Sample function Return type parameters Function name int add2ints(int a, int b) { return(a+b); } Functionbody

  13. Testing add2nums int main(void) { int y,a,b; cout << "Enter 2 numbers\n"; cin >> a >> b; y = add2nums(a,b); cout << "a is " << a << endl; cout << "b is " << b << endl; cout << "y is " << y << endl; return(0); }

  14. int add2nums( int firstnum, int secondnum ) { int sum; sum = firstnum + secondnum; return(sum); }

  15. What happens here? int add2nums(int a, int b) { a=a+b; return(a); } … int a,b,y; … y = add2nums(a,b);

  16. Pass by Value • Different location in memory • Changes to the parameters inside the function body have no effect outside of the function.

  17. Pass by Value: Example 1 For example, consider the following code: int sum(int a, int b){ a = a + b; return a; } void main(){ int x, y, z; x = 3; y = 5; z = sum(x,y); } What is the value of x, y, and z at the end of the main() program?

  18. Pass by Value: Example 1 • The answer: 3, 5, and 8. (x,y,z) • Even though the value of parameter a is changed, the corresponding value in variable x does not change. • This is why this is called pass by value. • The value of the original variable is copied to the parameter, but changes to the value of the parameter do not affect the original variable. • In fact, all information in local variables declared within the function will be lost when the function terminates. • The only information saved from a pass by value function is in the return statement.

  19. Pass by Value: Example 2 void Increment(int Number) { Number = Number + 1; cout << "Number =" << Number << endl; } void main() { int I = 10; cout << "I is: " << I << endl; Increment(I); cout << "I is: " << I << endl; }

  20. Passing Values to Functions • Many real-world functions you perform require that you provide information • A particular task might always be carried out in the same way, but with specific data • Consider a program that computes the amount of sales tax due on an item • You can write the prototype for computeTax() in one of two ways: void computeTax(int); OR void computeTax(int price);

  21. Passing Values to Functions

  22. Passing Values to Functions

  23. Arguments to Functions and Return Types of Functions • A function can contain a variety of combinations of actions • Some functions contain local variables declared within the function body • Some functions return and receive nothing • Others return values, receive values, or both • Functions may receive any number of variables as parameters, but may return, at most, only one variable of one type

More Related