You are on page 1of 19

Programming II - CMP1025

Lecture Two Modularity Using Functions By David W. White dwwhite@utech.edu.jm University of Technology, Jamaica January 2013

Expected Outcome
At the end of this lecture, the student should be able to:

Explain what a function is Identify and describe all the parts of a function Demonstrate how to write, accept arguments, call, and return values from a function Declare a function explicitly and implicitly

Topics to be Covered

What is a Function? Parts of a function Function header and function body Function name Accepting Arguments Inside a Function Accepting Arguments Inside a Function

Returning a Value from Within a Function Return Type Calling a Function Retrieving the Value Returned from a Function Function Body Function Prototype Complete Program Example

What is a Function?

A block of code which does a specific task Has a name which is used to refer to the function Can be called as often as the specified task is required Enhances modularity, separation of concerns Promotes software reuse
ReturnType FunctionName (DataType1 Argument1, DataType1 Argument2) { DataType1 Variable1; Variable1 = Argument1 + Argument2; return Variable1; }

Parts of a function
Two main parts of a function:

Function Header Function Body


ReturnType FunctionName (DataType1 Argument1, DataType1 Argument2) { DataType1 Variable1; Variable1 = Argument1 + Argument2; return Variable1; }

Function header and function body


The function header contains:

Unique Function Name Optional Arguments Return Type

ReturnType FunctionName (DataType1 Arg1) { DataType1 Variable1; Variable1 = Arg1 * 2; return Variable1; }

The function body contains:

The block of statements (code) for the function It can be empty Always enclosed between a pair of curly braces { }

Function name

Placed between the return type and the argument list Must follow the rules for naming identifiers in C Used to refer to the function Must be unique from all other identifiers including variable names and other functions
ReturnType FunctionName (DataType1 Argument1, DataType1 Argument2) { DataType1 Variable1; Variable1 = Argument1 + Argument2; return Variable1; }

Accepting Arguments Inside a Function


Accomplished by use of an argument list Enclosed between a pair of round brackets ( ) Follows function name Arguments are also known as parameters Argument list also known as parameter list Requires a separate argument for each value expected to be passed to the function By default, arguments passed by value to a function thus function gets copy of value passed

Accepting Arguments Inside a Function


Argument list can be empty e.g. ( ) For each argument listed, its data type must be specified e.g. (int Age) If more than one argument listed, separate arguments with a comma Format (DataType1 Arg1[, DataType2 Arg2...]) Example (int A, int B, float C)

Accepting Arguments Inside a Function


Notes on Function Arguments

By default arguments passed into a function are passed by value This means the function receives/accepts a copy of the value and not the original value itself Changes made to the copy inside the function do not affect the original value Later on in the course we will see how pass by reference instead In this case any change made to the value inside the function affects the original value

Returning a Value from Within a Function

Keyword return used to return a value from a function Only a single value can be returned at a time from each function Data type of value returned must match return type A function does not have to return a value Note that the return keyword also causes the function to be exited immediately

Return Type

Dictates what type of value can be returned by the function Is specified before the function name All functions in C must have a return type If one is not explicitly specified, then int is assumed If a function does not return a value, then its return type is void Examples:
void F1() { } int F2() { return 5*5; }

float F3() { return 22/7; }

Calling a Function

Calling (also known as invoking) a function transfers control (program execution) to the statements inside the function Done by stating the name of the function followed by its arguments in round brackets Examples:
//function call with no arguments //two arguments passed to function Calc //three arguments passed to function SUM //string Hello class passed to function printf //function call inside another function call

F(); Calc( 5.3, 8.2); SUM(i, j, k); printf(Hello class!); printf(The sum is %i, SUM(i, j, k));

Retrieving the Value Returned from a Function

If a function returns a value, its value can be assigned to a variable Example:


int x = 1, y = 2, z = 4, k; k = SUM(x,y,z); //k is assigned the value returned from function SUM

printf("The sum of x, y, and z is %d", k);

char ch; ch = getch(); //ch is assigned the value returned from built in function getch() printf("The character entered was %c", ch);

Function Body

Always enclosed in curly braces { } Contains 0, 1 or more statements Contains the variable declarations for the function Contains the executable code for the function Examples:
void F1() { } int F2(int x, int y) { return x * y; } double F3(double r) { double pi = 3.142; return pi * r * r; }

Function Body

Function Prototype

All functions must be declared explicitly or implicitly in C before they are used Since all programs start from the main() function... ...Either place the function before main() in the program (implicit declaration) ...Or create a function prototype for the function (explicit declaration) then place the actual function anywhere in the program

Function Prototype

A function prototype contains the function header followed by a semi-colon i.e. return type followed by function name followed by argument list followed by semicolon If one or more arguments are present, data type is mandatory but argument name is optional int SUM1(int a, int b, int c); Examples:
int SUM2(int , int , int ); float Area(float base, float height); void Display();

Complete Program Example


Without prototype. Function placed before main(). #include <stdio.h> int SUM(int a, int b, int c) { int s; s = a + b + c; return s; } int main () { int x = 1, y = 2, z = 4, k; k = SUM(x,y,z); printf("The sum of x, y, and z is %d", k); return 0; }

Complete Program Example


With prototype. Function placed after main(). #include <stdio.h> int SUM(int , int, int); int main (int argc, char *argv[]) { int x = 1, y = 2, z = 4, k; k = SUM(x,y,z); printf("The sum of x, y, and z is %d", k); return 0; } int SUM(int a, int b, int c) { int s; s = a + b + c; return s; }

You might also like