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

CBCP2202 E-Tutorial 5 Functions

The document discusses functions in C programming. It defines what functions are, their components including the function head and body, and provides examples of function definitions and calls. Functions allow programmers to break programs into smaller, reusable segments and make programs easier to read, understand and maintain.

Uploaded by

djaljd
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

CBCP2202 E-Tutorial 5 Functions

The document discusses functions in C programming. It defines what functions are, their components including the function head and body, and provides examples of function definitions and calls. Functions allow programmers to break programs into smaller, reusable segments and make programs easier to read, understand and maintain.

Uploaded by

djaljd
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

CBCP 2202

Functions
E-TUTORIAL 5
Learning Outcomes
1. Define functions and its components
2. Write function prototype, function call and function definition
3. Write C programs that invoke functions
4. Describe the use of library functions in a program
Introduction
C is a procedural language.

Its functional features enable us to structure our programs better.

All program codes can be put into one function which is the main() function.

However, for larger programs this is not suitable, because the program becomes
hard to read and understand.

Functions will make sure programs are easier to read, understand and maintain.
What are functions?
Functions are segments of a program by itself that does a specific task.

All C programs have at least one function, and it is called main function.

A program in C can have many functions, but a function cannot be inside another
function.

There are three important components in a function:

1. Function declaration
2. Function prototype
3. Function calls
Functions in programs
Function Definition
Functions are defined as having a head and body.

returned_data_type function_name (parameter_list) HEAD

{
BODY
function_body
}

The function head is made up of three main parts:


● return data type
● Function name
● Parameter list
Function Head
Return data type:
There are four return data type: char, int, float, and double.
There is a void return type, when no data is to be returned to the calling
function.

Function name:
Naming a function is similar to naming a variable.
Each function has a unique name

Parameter list:
This list is made up of variable declaration of all the values to be inserted
into this function.
Function Body
The function body is made up of statements that you have learnt before, such as
declaring variables, input and output statements, selection and repetition.

The function body must be enclosed in { and }

The last statement in a function should be the return statement (unless if it is


void, return statement is not necessary).

There should only be one return statement per function.


Function examples:
void display (void)
{
printf(“Hello\n”);
}

In this example, the function name is display, there is no parameter list or return
values.
Function example 2:
int addition (int x, int y)
{
int total = 0;
total = x + y;
return total;
}

Question:
1. What is the return type?
2. What are the parameter list?
Complete program with functions.
#include <stdio.h>
void display_message (void);

int main()
{ OUTPUT:
printf("In function main\n"); In function main
display_message(); In function display_message
Return to function main
printf("Return to function main\n");
}

void display_message(void)
{
printf("In function display_message\n");
}
The End

Q&A
Thank you.

You might also like