Docsity

Programming Fundamentals Chapter 05 – Functions Part - C Function Overloading, Slides of Programming Languages

Partial preview of the text

Download Programming Fundamentals Chapter 05 – Functions Part - C Function Overloading and more Slides Programming Languages in PDF only on Docsity!

Programming Fundamentals

Chapter 05 – Functions Part - C

Function Overloading

  • Two or More Functions may have the

same name … As long as parameter

portion of signature is different

  • Same Function
    • Different set of Parameters
    • Or Parameter of Different Types

Function Overloading

  • Process of declaring multiple functions

with same name but different parameters

  • Functions must differ in either:
    • Number of Parameters
    • Type of Parameters
    • Sequence of Parameters

Function Overloading

  • Advantage:
    • Programmer does not need to memorise names of different functions with similar tasks.
    • E.g., float Average(int, int); float Average(float, float); float Average(int, float);
  • Note: These functions differentiated by their signatures

Function Overloading

  • Overloading function also useful in cases

of different parameter length.

Function Templates

  • Advantage of Overloaded Functions
    • Programmer remembers only one function name (for set of functions performing similar operations).
  • Drawbacks of Overloaded Functions
    • Each function must be written specifically still.

Function Templates

  • More convenient to use Function Template

instead of Overloaded Function

  • Function Template
    • Write a single function definition
    • This definition will work for different data types

Function Templates

Template Prefix Begins with keyword “template” Generic Data Type(s) Begins with keyword “class” Followed by Param Name Multiple Types separated by commas Type parameters or Generic Data Type Replaced with actual data type by compiler

Function Templates

Arrays

  • 1 Variable → 1 Value
  • 1 Array → Multiple Values
  • Array Summary
    • Group of Variables (of same Type)
    • Stored in consecutive memory locations

Arrays

  • Formal Definitions:
    • A Data structure of related data items of same type
    • Consecutive group of memory locations, all of which have same name and same type
  • Referring to particular location/element?
    • Specify name of Array
    • Specify Position number of element in Array

Arrays

  • More about Size Declarator
    • Must be constant integer expression (>0)
      • Literal → e.g., int hours[6];
      • Named Constant → e.g., const int SIZE = 6; int hours[SIZE];
    • If not mentioned, number of elements in initializer list will be the number of elements in array. - int n[] = { 1, 2, 3, 4, 5, 6 };
  • Valid Definitions:

Arrays

  • Memory Requirements
    • Depends on:
      • Array Data Type (int, double, float, etc.)
      • Number of elements
    • Examples
      • char letter[30]? 30 bytes int mile[56]? 224 bytes float temp[12]? 48 bytes double dist[1000]? 8000 bytes
    • !!! No Bounds !!!