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

The concept of function overloading in C programming. It describes how two or more functions can have the same name as long as the parameter portion of the signature is different. It also explains the advantages and disadvantages of overloaded functions and introduces the concept of function templates. The document also covers the basics of arrays in C programming, including formal definitions, memory requirements, and array initialization. It also provides some reading materials for further study.

Typology: Slides

2021/2022

Available from 11/16/2022

razaroghani
razaroghani 🇵🇰

4.5

(4)

151 documents

1 / 32

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Programming Fundamentals
Chapter 05 Functions
Part - C
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20

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 !!!