0% found this document useful (0 votes)
19 views23 pages

Session 8

The document explains the concept of functions in C programming, including their definition, structure, and role in providing modularity. It covers the importance of function headers, rules for declaring and calling functions, and the distinction between local and global variables. Additionally, it provides examples of function implementation and tasks for practice.

Uploaded by

samwel.ochieng21
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)
19 views23 pages

Session 8

The document explains the concept of functions in C programming, including their definition, structure, and role in providing modularity. It covers the importance of function headers, rules for declaring and calling functions, and the distinction between local and global variables. Additionally, it provides examples of function implementation and tasks for practice.

Uploaded by

samwel.ochieng21
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/ 23

Functions

Introduction

 The C functions that you have used so far (such as


printf and scanf ) are built into the C libraries, but you
can also write your own functions.
 Therefore functions can be classified as built-in and
user defined.
What is a function

 A named Block of source code that performs a specific task and


optionally returns a value to the calling program.
 Named-@ function has a unique name. By using the name in other
parts of a program you can execute statements in the function.
 Task -discrete job your program must perform as part of overall
operation i.e calc triangle area.
 When program calls function, statements inside it are executed.
These statements can pass information back to the calling
program
Function Role

 Help to provide modularity to software


 Can be executed many times at different points of
your program with no code duplication
 Complex tasks can be divided into small manageable
and can be tested independently before joining them
again
 Allows sharing of rensponsibilities between
developers if tam define standard functions to use.
Function Structure

 It starts with function header, which is the same as


the function prototype but does not end with a
semicolon.
 The function prototype and the definition must have
exactly the same return type, name and parameter
list.
 It followed by the function name and then the
parameter List.
Structure

Return_type function_name(parametr_list)
{
//statements
}
Sample

int square(int x)
{
int y;
y = x*x;
return y;
}
Function header has three parts

 Return type
 Refers to the data type of the return value of function. If function
doesn’t return a value we use void.
 Function name-
 Refers to the name of function.
 It must be meaningful and express what it does.
 Par list-
 Refers to parameters passed to function which is optional.
 Must contain a datatype and name.
 {}
 Real work happens here.
 execution begins at start of { and ends return statement or }
Rules

 Before using a function, you must declare it.


 Function implementation must match function
declaration for all parts of return datatype,function
name, par list.
 When you pass parameter to function,thy must match
datatype , order of parameters
 You cant define a function within another function.
Example 1

 Write a function that displays your name when its


called from the main program.
Calling the function

 To execute the function we must call it


 names();
Function with Arguments
Calling the Function
Example 2

 Using a function write a program that computes the


area of a rectangle
int area(int l,int w)
{
int ansa=l*w;
return ansa;
}
Main Program

 int length,width,ansa;
 printf(“Enter Length\n");
 scanf("%d",&length);
 printf(“Enter Width\n");
 scanf("%d",&width);
 ansa=area(length,width);
 printf("Total:%d",ansa);
Note

 Use function name that describe its purpose


 Don’t pass values to function if it doesn’t need
 Don’t pass fewer or more arguments than the
parameters in function
Task

 Write a function that will accept the radius of a circle


and return the area of the circle to the calling
program.

 Write a C program to calculate the circumference of a


circle given its radius. Implement calculation of
circumference as a separate function
The Scope of a Variable

 A variable has a scope, which determines how long it


is available to your program (or function) and where
it can be accessed from.
 Variables declared within a block are scoped only to
that block; they can be accessed only within that
block and go out of existence when the execution of
the block is completed.
Local Variables

 A variable declared inside a function is called a local


variable. Scope of a local variable is limited to the
function. Such variables are not seen by any other
functions including the main function.
Local Variables

 When you pass a variable to a function ,you are


actually passing a copy of the variable (called passed
by value), but not the variable it self.
 This means that the value of the passed variable
cannot be changed by any other function.
Global Variables

 Global-variables declared outside any function. global


means their scope is global can be accessed from any
part of program
Example
Continuation

You might also like