C-Functions
Dr. D.H. Kisanga
May 2020
Function
is a group of statements that together perform a task.
Every C program has at least one function, which is main()
A function in C plays the same role that functions, subroutines,
and procedures play in other languages, although the details might
differ.
C functions can be classified into two types:
Library functions
User-defined functions
• The library functions are the functions which are already
defined in C’s functions library i.e. header files.
For example:. scanf() and printf() are the library functions
defined in file stdio.h
• User defined function is the function defined by the
programmer.
For example: the main() function.
• In order to make use of user defined functions, we need to
establish three elements that are related to functions.
i. Function definition
ii. Function declaration
iii. Function call
Function definition
It includes following topics:
i. Function Header
ii. Function Body
The general form of a function is:
function_type function_name (parameters list)
{
body of the function
}
Where:
Return_type: The return_type is the data type of the value the
function returns
Function_name: is the actual name of the function.
Parameter_list: refers to the type, order, and number of the
parameters of a function
Function body: contains a collection of C-statements
function_type function_name(parameters list): altogether are
known as function header and
Function declaration
Before a function is used in a C program it should be declared.
Declaration of function is known as function prototype.
A function declaration or a function prototype consists of a
function return type,
functions name, and
its parameter lists.
i.e. Function_return_type function_name (parameter list);
Eg. int myfunct(int x);
The only function that does not need a prototype is main() since it
is predefined by the C language.
Function call (Calling a Function)
To use a function, you will have to call that function to perform
the defined task.
When a function is called, program control is transferred to the
called function. A called function performs a defined task and
there after returns the program control back to the calling
function (or main program).
While calling a function, there are two ways that arguments can
be passed to a function:
i. Function call by value: This method copies the actual value of
an argument into the formal parameter of the function.
ii. Function call by reference: This method copies the address of
an argument into the formal parameter.
This class covers only function call by value.
Eg 1: A program that uses a function to return an integer value.
#include <stdio.h>
#include<conio.h>
int funct (void);
main ()
{
int num;
num = funct();
printf ("The number is %d", num);
getch();
}
Eg 2. This program demonstrates the use of arguments and how to pass them to
the called function.
#include<stdio.h>
#include<conio.h>
void sum(int x, int y);
main()
{
sum (2,20); /* sending 2 and 20 to sum() as values of x and y respectively */
sum (10,40); /*sending 10 and 40 to sum() */
getch();
}
void sum(int x, int y)
{
printf ("%d\n", x +y);
}
Eg 3. This program demonstrates the use of arguments and how to pass them to
the called function (A program that finds a maximum value)
#include <stdio.h>
int max(int num1, int num2);
int main ()
{
/* local variable definition */
int a = 100;
int b = 200;
int ret;
/* calling a function to get max value */
ret = max(a, b);
printf( "Max value is : %d\n", ret );
return 0;
}
Eg4: This program uses a function myvolume() to calculate the volume
of a sphere and return a value to the main() function.
#include<stdio.h>
#include<conio.h>
float myvol (float radius);
main()
{
float volume, r;
printf ("Enter the radius: ");
scanf ("%f", &r);
volume = myvol(r); /* calling a function myvolume ()*/
printf ("\n\n The volume of a sphere is %f", volume);
getch();
}
Eg5: This is a program that displays a square of a number entered
from the keyboard. The square is computed using the function
squared().
# include <stdio.h>
#include<conio.h>
int squared(void);
int main (void)
{
int sq;
sq = squared ();
printf ("Its square is %d", sq);
getch();
}
Eg6. Write a program that uses a function called convert (), which prompts the
user for an amount in US-Dollar and return this value converted into Tshs (Use
an exchange rate $1=TSh 2100) .Display the conversion.
#include<stdio.h>
#include<conio.h>
float convert (void);
float dollar; /* This is a global variable */
main ()
{
float TSH;
TSH = convert();
printf ("US$ %f = Tshs %f", dollar, TSH);
getch();
}
Eg 7. C-mathematics functions. All the math functions require that
the header file math.h be included in a program.
#include<stdio.h>
#include<conio.h>
#include<math.h>
main()
{
float num;
printf("\n\n Enter a number \n");
scanf("%f", &num);
/* The fabs() function returns the absolute value of a number*/
printf("\n Absolute value of %f is %1.1f", num, fabs(num));
/* The log() function returns the natural logarithm of a non-negative number*/
printf("\n The logarithm of %f is %1.1f", num, log(num));
/* The pow() function returns base raised to the exponent power. Where base>0 and exp>=o */
printf("\n The power of %f raised to 5 is %1.1f", num, pow(num,5));
/* The sqrt() function returns the square root of a non-negative number*/
printf("\n The square root of %f is %1.1f", num, sqrt(num));
getch();
}
Homework
Q1. Modify the program in Eg6 using a for loop that will request a
user to enter amount in USD from the keyboard and display the
conversion to the monitor screen. Use 5 iterations.
Q2. The moons gravity is about 17% of Earth’s. Write a program
that allows you to enter your weight and use a function convert() to
compute your effective weight on the moon and return the output to
the main() function for display.
Thank you for your attention.
Questions