0% found this document useful (0 votes)
71 views

CS205-2020 Spring - Lecture 6 PDF

This document provides an overview of C/C++ programming functions. It reviews function definitions, prototypes, return values, and passing values. It then discusses various function types including arrays, C-style strings, structures, classes, recursion, and pointers to functions. Examples are provided for each function type to demonstrate proper usage. The goal is to teach the different components and usages of functions in C/C++ programming.

Uploaded by

Eason Guan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
71 views

CS205-2020 Spring - Lecture 6 PDF

This document provides an overview of C/C++ programming functions. It reviews function definitions, prototypes, return values, and passing values. It then discusses various function types including arrays, C-style strings, structures, classes, recursion, and pointers to functions. Examples are provided for each function type to demonstrate proper usage. The goal is to teach the different components and usages of functions in C/C++ programming.

Uploaded by

Eason Guan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

C/C++ Programming

Language
CS205 Spring
Feng Zheng
2020.03.26
Content
• Brief Review

• Function Review

• Various Functions

• Summary
Brief Review
Content of Last Class
• Loops
➢ for( ; ; )
➢ while( )
➢ do while( )
➢ Increment/decrement operations(++,--)
• Branching
➢ if; if else; if else if else
➢ switch
➢ ?; continue; break;
• The expressions for Loops
➢ Relational expressions (6 operations)
➢ Logical expressions (AND, OR, NOT)
Function Review
Functions
• Three components
➢ Provide a function definition including two parts: head and body
➢ Provide a function prototype
➢ Call the function
• Two types of usage
➢ Use a library function
✓ Including the header file
✓ Static library is locked into at compiling time
✓ Dynamic library exists as a separate file outside of the executable file
➢ Create your own functions
✓ Handle all three aspects
Defining a Function
• Two categories
➢ Don’t have return values

➢ Do have return values


✓ Return value can be a constant, a variable, or a more general expression
✓ Both the returning function and the calling function have to agree on the
type of data at that location
✓ The function terminates after it executes the first return statement it
reaches
Prototyping and Calling a Function
• Why prototypes?
➢ The function interface to the compiler
➢ The only way to avoid using a function prototype is to place the function
definition before its first use
➢ Prototype syntax
✓ A function prototype is a statement
✓ Does not require that you provide names for the variables
• What prototypes do for you
➢ The compiler handles the function return value
➢ The compiler checks the number of function arguments
➢ The compiler checks the type of arguments and converts the arguments
to the correct type
Function Arguments and Passing by
Value
• Call a function
➢ Create a new type double variable--formal argument or formal parameter
➢ Initialize it with the value--actual argument or actual parameter
➢ Insulate data from the calling function--rather than with the original data
• Multiple Arguments
➢ Have more than one argument
➢ Comma is used
• See program example 1
Local variables
• Automatic variables
➢ Variables declared within a function
are private to the function
➢ They are allocated and deallocated
automatically during program
execution
✓ When a function is called, the computer
automatically allocates the memory
needed for these variables
✓ When the function terminates, the
computer automatically frees the
memory that was used for those
variables
Various Functions
Functions and Arrays
• See program example 2
➢ Suppose you use an array to keep track of how many cookies each
person has eaten at a family picnic
• How pointers enable array-processing functions
➢ Treat the name of an array as a pointer
➢ There are a few exceptions to this rule
✓ Use the array name to label the storage
✓ sizeof operation yields the size of the whole array in bytes
✓ Address operator & returns the address of the whole array
➢ int *arr and int arr[]
✓ Have the identical meaning when (and only when) used in a function header or function
prototype
✓ Not synonymous in any other context
More about Arrays for Functions
• The implications of using arrays as arguments
➢ If you pass an ordinary variable, the function works with a copy.
➢ If you pass an array, the function works with the original
➢ Use array addresses as arguments saves the time and memory

• See program example 3


➢ Explicitly pass the size of the array
More Array Function Examples
• See program example 4
➢ Fill the array
➢ Show the array and protect it with const
➢ Modify the Array
• Problems
➢ Need to be informed about the kind of data in the array, the location
of the beginning of the array, and the number of elements in the array
• See program example 5
➢ Functions using array ranges
Pointers and const
• Make a pointer point to a constant object
Pointers and const
• Declare pointer arguments as
pointers to constant data
➢ It protects you against
programming errors that
inadvertently alter data

➢ Using const allows a function to


process both const and non-const
actual arguments, whereas a
function that omits const in the
prototype can accept only nonconst
data
Functions and Two-Dimensional
Arrays
• The name of an array is treated as its address
➢ The type of data is pointer-to-array-of-four-int

Declare an array of
four pointers-to-int

• See program example 6


Functions and C-Style Strings
• See program example 7
➢ Functions with C-Style string arguments
✓ An array of char
✓ A quoted string constant (also called a string literal)
✓ A pointer-to-char set to the address of a string
• See program example 8
➢ Functions that return C-Style strings
➢ It is not recommended to use new and delete separately
Functions and Structures
• A structure ties its data in to a single entity, or data object,
that will be treated as a unit
➢ A function can receive a structure
➢ A function can return a structure
• Disadvantage
➢ If the structure is large, the space and effort involved in making
a copy of a structure can increase memory requirements and slow
down the system
• See program example 9 How to check
➢ Passing and returning structures whether is there a
copy?
Passing Structure Addresses
• Save time and space
➢ Pass it the address of the structure
➢ Declare parameter to be a pointer-to- structure type
➢ Use the indirect membership operator (->)

• See program example 10


Functions and Two Class Objects
• Functions and string class objects
➢ A string class object is more closely related to a structure
than to an array
➢ See program example 11

• Functions and array objects (a type of class)


➢ See program example 12
Recursion
• C++ function has the characteristic that it can call itself
➢ Artificial intelligence
• C++ does not let main() call itself
➢ See program example 13
• Recursion with multiple recursive calls
➢ Divide-and-conquer strategy (merge sort)
➢ See program example 14
Difference Between Recursion and
Iteration
Conditional control statement At least one base case

The value of the control The function keeps on


variable continuously converging to the defined
approaches the value in base case as it continuously
the conditional statement. calls itself.

A control variable stores Stack memory is used


the value, which is then to store the current
updated, monitored, and state of the function.
compared with the
conditional statement. If there is no base
case defined,
Infinite loops keep recursion causes
utilizing CPU cycles until we a stack overflow
stop their execution manually. faster slower error.
Pointers to Functions
• Functions, like data items, have addresses
➢ The stored machine language code for the function begins
➢ Write a function that takes the address of another function
as an argument
• Three steps
➢ 1: obtain the address of a function
Pointers to Functions
➢ 2: declare a pointer to a function short tell[10];
short (*pas)[10] = &tell;
pas=&tell

➢ 3: use a pointer to invoke a function


Two Function Pointer Examples
• See program example 15

• See program example 16


➢ Variations on the theme of function pointers
Summary
• Function review
➢ Function definition and prototype
➢ Returned and passed values
➢ Local values
• Various functions
➢ Arrays
➢ C-style
➢ Structure
➢ String class and array objects
➢ Recursion
➢ Pointer to functions
Thanks

[email protected]

You might also like