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

Week 11 - Lecture 29 - Intro Function

Uploaded by

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

Week 11 - Lecture 29 - Intro Function

Uploaded by

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

Week# 11

Functions

Dr Taimur Ahmed
Department of IT & CS, PAF-IAST
Lecture# 29
Library Functions & User defined Functions
Why do we need Functions?
Modular Programming
Modular Programming
❑ Modular Programming:
❑ Breaking a program into small, manageable modules
❑ Function:
❑ A collection of statements to perform a task
❑ Motivation for modular programming
➢ Simplifies the process of writing programs
➢ Improves maintainability of programs
➢ Helps programmer to understand purpose of each module in the program

Lecture# 29 - Introduction to Functions | 4


Modular Programming

Lecture# 29 - Introduction to Functions | 5


Type of Functions
❑ Two types of functions
1. Library/standard functions
2. User defined functions

❑ Library/standard Functions
❑ Already defined in the header files

❑ User defined Functions


➢ Programmers can define any functions to perform specific tasks

Lecture# 29 - Introduction to Functions | 6


Defining and Calling Functions
❑ Function Call:
➢ A statement is used to call the function
❑ Function definition:
➢ Statements that are written in the body of a function

Function header
int main()
{
Function
cout << “Hello! << endl; definition

return 0;
}

Lecture# 29 - Introduction to Functions | 7


Function header
❑ Function header contains data type it returns, name of the function and
arguments/parameters list enclosed in ( )

Function header
int main()
{
cout << “Hello! << endl; Function
definition

return 0;
}

Lecture# 29 - Introduction to Functions | 8


Function definition
❑ Function name: should follow same rules as variables
❑ Body of the function: statements enclosed by { } that perform set of
tasks.
❑ Body of a function can call other functions too.

Lecture# 29 - Introduction to Functions | 9


Function Return Type
❑ If a function returns a value, the type of the value must be indicated:
int main()
❑ If a function does not return a value, its return type is void:

void printHeading()
{
cout << "Monthly Sales\n";
}

Lecture# 29 - Introduction to Functions | 10


Calling a Function
❑ To call a function, use the function name followed by ( ) and ;
printHeading();

❑ When called, program executes the body of the called function

❑ After the function terminates, execution resumes in the calling function at


point of call.

Lecture# 29 - Introduction to Functions | 11


Example of Function

Lecture# 29 - Introduction to Functions | 12


Flow of Control in Calling a Function

Lecture# 29 - Introduction to Functions | 13


Calling a Function
❑ main can call any number of functions
❑ Functions can call other functions
❑ Compiler must know the following about a function before it is called:
➢ name
➢ return type
➢ number of parameters
➢ data type of each parameter

Lecture# 29 - Introduction to Functions | 14


Function Prototypes
Function Prototypes

What is the output of this


program?

Lecture# 29 - Introduction to Functions | 16


Function Prototypes

What is the output of this


program?

[Error] ‘displayMessage’ was


not declared in this scope

Lecture# 29 - Introduction to Functions | 17


Function Prototypes
❑ Before a function is called, complier should be notified that our code will
use a function or multiple functions

❑ Place function declaration before calling function’s definition – like the


function definition without the body

➢ Function Header: void displayMessage ()


➢ Prototype: void displayMessage();

Lecture# 29 - Introduction to Functions | 18


Function Prototypes

Lecture# 29 - Introduction to Functions | 19

You might also like