1 ICS 2175 Lecture 4 Functions
1 ICS 2175 Lecture 4 Functions
Functions in C
Functions
#include <stdio.h>
}//end function
main()
{ int num;
scanf(“%d”,&num);
printf(“The square of %d is
%d”,num,square(num));
} //end main
Functions : Exercise
Write a function that converts an input string to uppercase. The argument to the
function is a string. The function then returns the string in uppercase. Use your
function in a main program where you ask the user to enter a string and then
you output the string in uppercase. Do not use the strupr or toupper functions.
You have to use a loop.
Hint: Characters in C are stored as integers using the American Standard Code
for information interchange (ASCII). The lowercase characters are given the
integers 97 to 122 where ‘a’=97, ‘b’=96, … The upercase characters are
represented by the integers 65 to 90 where ‘A’=65, ‘B’=66 …
If you have the statement putchar(97) in your program, then the character ‘a’ is
output on the screen. Since characters are integers, then it is possible to do
some basic mathematics with them e.g. ‘a’+2=99 which is equal to c.
Functions : Variable Scope
• Refers to the section of the program within which a variable is
accessible/visible.
• Variables can be classified according to their scope.
Functions : Variable Scope: local variables (Automatic variables)
• These are declared within a function and are local to that
function i.e. their scope is confined to that function.
• Local variables defined in different functions will be
independent of each other even though they may have the same
name.
• Any variable declared within a function is interpreted as a local
variable unless otherwise specified.
• Arguments to a function are also considered as local variables.
A local variable does not retain its value once control is
transferred out of the defining function. Therefore any value
assigned to a local variable within a function will be lost once
the function is exited.
Functions : Variable Scope: Global variables (External variables)
• Not confined to single functions.
• Their scope extends from the point of definition to through the
remainder of the program.
• Since they are recognized globally, they can be accessed by any
function that falls within their scope.
• They retain their values within this scope. Therefore a global
variable can be assigned a value within one function and this
value can be used within another function.
• Use of external variables provides an efficient mechanism for
transferring information back and forth between functions.
• Moreover we now have multiple ways to transfer multiple data
items out of a function, since the return statement can return
only one data item.
Functions : Variable Scope: External variables definition vs
declaration
• When you define an external variable, you give its type and
optionally, its value just like any other variable. This is done
outside any function and usually, before any function that
accesses it.
• If a function precedes any external variable definition and needs
to use the variable, then it needs to declare the variable using the
extern statement.
• The declaration is similar to the definition except for the use of
the extern keyword.
• An external variable declaration cannot include assignment of
an initial value to the external variable.
Functions : Static Variables
NB: Local and Static variables may have the same name within
a function as a global variable. In such a situation the local
variable takes precedence i.e. the common name, when
used, is assumed to refer to the local variable.
Functions : Variables: Example 1
To calculate the area of a circle given the
diameter
#include <stdio.h>
float pi=3.14; /*pi is an external/global variable –
accessible in all functions that come after it is
defined.*/
float function circle_area(float diameter)
{ float area,radius; /*area and radius are
local variables accessible only within the
circle_area function*/
radius=diameter/2;
area=pi*(radius*radius);
return area;
}
Functions : Variables: Example 1
#include <stdio.h>
float function circumference(float diameter)
{ extern float pi; /*we declare pi as an
external variable.*/
float perimeter; /*perimeter is a local
variable within circumference function*/
perimeter=pi*diameter;
return perimeter;
}
Functions : Variables: Example 2
}
Functions : Variables: Example 3
To demonstrate the use of static variables, we
write a function that counts the number of times
it has been called.
#include<stdio.h>
void times_called()
{ static int count=0;
count+=1;
if (count ==1)
{
printf(“ I have been called %d.
time”,count);
}
else
{ printf(“ I have been called %d.
times”,count);
}
} //end times_called function
Functions : Variables: Example 3
/*Using the times_called function and the pi
global variable*/
main()
{ char choice;
do
{
printf(“To call the function enter y
else enter n”);
choice=getchar();
if (choice==’y’)
{
times_called();
}
}while (choice==’y’);
}
Recursion
#include<stdio.h>
int factorial(int n); /* function prototype */
main()
{
int n;
printf(“n=”);
scanf(“%d”,&n);
printf(“\n n!=%d\n”,factorial(n));
} //end main
Recursion:Example
Use your function in a main program where you ask the user to a number n and
then you output the nth fibonacci number