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

ACLC College Computer Programming1 C SESSION11

Uploaded by

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

ACLC College Computer Programming1 C SESSION11

Uploaded by

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

Introduction to Programming

(C++)

Gliceria S. Tan
Instructor
Session 11 Topics

➢ Function
➢ Dissection of a function definition
➢ Function call

GLICERIA S. TAN, MIS COMPUTER PROGRAMMING 1


INSTRUCTOR
Function

A function is a subprogram
designed to perform a specific task.

Every C++ program has at least one


function, which is main().

GLICERIA S. TAN, MIS COMPUTER PROGRAMMING 1


INSTRUCTOR
You can define functions in your
program in addition to main().

To create a function:
returnType funcName(params){
function body
}

GLICERIA S. TAN, MIS COMPUTER PROGRAMMING 1


INSTRUCTOR
Dissection of a function definition:

returnType funcName(params){
function body
}

• returnType specifies the type of value the


function returns. If the function does not return a
value then returnType is void.

GLICERIA S. TAN, MIS COMPUTER PROGRAMMING 1


INSTRUCTOR
Dissection of a function definition:

returnType funcName(params){
function body
}

• funcName − This is the name of the function.

GLICERIA S. TAN, MIS COMPUTER PROGRAMMING 1


INSTRUCTOR
Dissection of a function definition:
returnType funcName(parameter list){
function body
}

• parameter list refers to the comma-separated list


of identifiers and their type declarations. A
parameter is like a placeholder. When a function
is called/invoked, you pass a value to the
parameter. This value is referred to as argument.
Parameters are optional. A function may have
no parameters.
Dissection of a function definition:

returnType funcName(params){
function body
}

• function body − The function body contains a


collection of statements that define what the
function does.

GLICERIA S. TAN, MIS COMPUTER PROGRAMMING 1


INSTRUCTOR
Here is a sample function called getSum that takes
two integer parameters a & b. The function
computes and returns the sum of two integers:

int getSum(int a, int b){


int sum;
sum = a + b;
return sum; //return statement
}

The return statement is used to return a value and


also causes an immediate exit from the function.
Here is getSum() as a flowchart:

getSum(a,b)

sum=a+b

return sum

GLICERIA S. TAN, MIS COMPUTER PROGRAMMING 1


INSTRUCTOR
Here’ a sample function called printMsg. This
function has no parameters and does not
return any value so its return type is void:

void printMsg(){
cout<<”Hello world!”<<endl;
}

GLICERIA S. TAN, MIS COMPUTER PROGRAMMING 1


INSTRUCTOR
Here is printMsg() as a flowchart:

printMsg()

OUTPUT “Hello World”

return

GLICERIA S. TAN, MIS COMPUTER PROGRAMMING 1


INSTRUCTOR
Function call

A function call causes the


execution of the statements in
the body of the function.

GLICERIA S. TAN, MIS COMPUTER PROGRAMMING 1


INSTRUCTOR
Syntax of a function call:
funcName(arguments);

arguments are the actual values


that are passed to the
parameters of a function.
GLICERIA S. TAN, MIS COMPUTER PROGRAMMING 1
INSTRUCTOR
Here’s the getSum() function in a program:

#include <iostream>
using namespace std;
int getSum(int a, int b);//Function prototype

int main(){
int x,y,sum;
cout<<"Enter values for x and y: ";
cin>>x>>y;
sum=getSum(x,y); //function call
cout<<“The sum is “<<sum;
}
int getSum(int a, int b){
int sum;
sum=a+b;
return sum;
}
Here’s a sample program that uses the getSum() function:

#include <iostream>
using namespace std;
int getSum(int a, int b);//Function prototype
When the program runs, the compiler calls main() first.
int main(){
int x,y,sum;
cout<<"Enter values for x and y: ";
cin>>x>>y; Calls function getSum(). Arguments x and y
sum=getSum(x,y); are copied into parameters a and b of
cout<<“The sum is “<<sum; function getSum().
}
int getSum(int a, int b){
int sum;
Value of sum is returned to main()
sum=a+b;
return sum;
}
Here’s the program flowchart.

START getSum(a,b)

INPUT x,y sum=a+b

sum=getSum(x,y) return sum

OUTPUT sum

STOP

GLICERIA S. TAN, MIS COMPUTER PROGRAMMING 1


INSTRUCTOR
Here’s a sample program that uses the printMsg()
function:

#include <iostream>
using namespace std;
void printMsg();
int main(){ In main(), the function call printMsg()
causes execution to jump to function
printMsg(); printMsg().

} In function printMsg(), “Hello world!” is printed, then flow of


void printMsg(){ execution goes back to the function call in main().
cout<<”Hello world!”<<endl;
}

GLICERIA S. TAN, MIS COMPUTER PROGRAMMING 1


INSTRUCTOR
Here’s the program flowchart.

START printMsg()

printMsg() OUTPUT “Hello World”

STOP return

GLICERIA S. TAN, MIS COMPUTER PROGRAMMING 1


INSTRUCTOR
Homework

GLICERIA S. TAN, MIS COMPUTER PROGRAMMING 1


INSTRUCTOR
Create a function named myFunction and
call it inside main(). Try to execute the
complete program on your device.
“QUOTE OF THE DAY!”

GLICERIA S. TAN, MIS COMPUTER PROGRAMMING 1


INSTRUCTOR

You might also like