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

EE172_Lecture_4_Functions_Pointers

The document covers the basics of functions and pointers in programming, emphasizing their importance for modularity, code reusability, and ease of debugging. It explains different types of functions, how to declare and define them, and the methods for passing variables (call by value and call by reference). Additionally, it discusses pointers, their uses, and includes exercises to reinforce the concepts learned.

Uploaded by

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

EE172_Lecture_4_Functions_Pointers

The document covers the basics of functions and pointers in programming, emphasizing their importance for modularity, code reusability, and ease of debugging. It explains different types of functions, how to declare and define them, and the methods for passing variables (call by value and call by reference). Additionally, it discusses pointers, their uses, and includes exercises to reinforce the concepts learned.

Uploaded by

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

EE172

Computer Programming for


Engineers
Lecture 4 (21st April, 2022):
Functions + Pointers
1
Functions

2
Functions
3

 Function is a block of code that performs a specific task.


 Large programs are usually divided into small subprograms
known as functions.
 Functions are used to provide modularity to the program.
 Functions provide code reusability.
 Functions makes it easy to understand, edit and debug the
large program
Types of Functions
4

Functions

Library User-defined
functions functions
Functions Declaration
5

 Function is one of the user-defined data types.


 Function declaration, is done to tell the compiler about the
existence of the function.
 Declaration consists of three parts:
Functions Definition
6

 Function definition consists of a function header and a


function body.
 The function body contains a collection of statements that
define what and how the function does.
7
8
9
Passing Variables to a Function
10

 Most of the functions need input arguments.


 When we call a function we can supply input arguments in
different ways.

Call by Value
Types of function
calling Call by Reference
Call by Value
11

 Copy of the actual value is passed as argument to a


function.
 The actual value remains unaffected.
 Changes made to the parameter inside the function
have no effect on the original argument.
12
Call by Reference
13

 The memory address of the variable is passed to a function.


 It allows a function to modify the actual content of a
variable without having to create a copy of it.
 Changes to the value have an effect on the original data.
 It allows a function to modify a variable without having to
create a copy of it.
14
Pointers

15
Pointers
16

 If we have a variable var, then &var will give us its memory


address.
 A pointer is a variable that stores memory address of another
variable.
 To access the value stored in memory address, we use asterisk
sign (*)
17
Uses of Pointers?
 Performance efficiency: Execution time with pointers is faster
because data is accessed directly from the memory location.
 Dynamic memory allocation: Pointers assign and release
memory dynamically, saving memory usage.
 Pointers are useful in accessing arrays.
 Pointers can be used to return multiple values from a function.

18
19
20
21
22
How can we return both the quotient and remainder from a function?

23
How to return multiple values from a function…

24
How to return multiple values from a function…

25
26

 Scope of Variables in functions:


 Local variables
 Global variables

 Recommended library functions:


 <cmath> - for common math functions (sin, log, sqrt etc.)

 <cctype> - for character checking functions (isalpha, isdigit etc.)


Exercise 1

Write a C++ program which asks the user to


enter any three integers.
The program should finally display the largest
integer.

27
Exercise 2

Write a C++ program which asks the user to


enter any integer.
The program should finally display the factorial of
that number.

28
Exercise 3

Write a C++ program which asks the user to


enter any number.
The program should finally display the square,
square root and logarithm of that number.

29
Exercise 4

Write a C++ program which asks the user to


enter any sentence.
The program should finally calculate the number
of words from the sentence.

30
31

You might also like