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

8 Functions

C-Programming
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

8 Functions

C-Programming
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

FUNCTIONS

Introduction
 A function is a sub program that is executed once

called by the main program.


 A function may receive arguments from the main

program, process arguments and return results


to the main program.
Types Of Functions
There are two types of

functions:
- pre-defined (inbuilt) functions

- User-defined functions
Pre-defined
Functions
 Were created by the programming language developers to

assist programmers e.g. C programming has the following:

- Input/output functions

- String functions

- Math functions
Input/Output Functions
 Input/Output files/libraries containing I/O functions include:

- stdio.h – consisting of printf ()and scanf()

functions.

- conio.h- Stands for CONsole Input Output.

Consisting of the following functions:

clrscr(), delline(), getch(),getche(),

gotoxy(), textcolor(), textbackground()


String Functions
 The string functions are stored in the <string.h> header file

Function Action

 Strcat() Concatenates two string

 Strcmp() Compares two strings

 Strlen() Finds the length of a string

 Strupr() Converts to upper case

 Strlwr() Converts to lower case


//Converts to string to upper case

#include<string.h>

#include<stdio.h>

main() {

char mystring[10];

printf("Enter your string ");

scanf("%s", &mystring);

printf("string to upper case is %s", strupr(mystring));

return 0;

}
// Finds the length of a string
#include<string.h>
#include<stdio.h>
main() {
char mystring[10]; int x;
printf("Enter your string ");
scanf("%s", &mystring);
X = strlen(mystring);
printf(“String length is %d", x);
return 0;
}
#include<math.h>
#include<stdio.h>
main() {
int x;
double ans;
printf("Enter number");
scanf("%d", &x);
ans = sqrt(x); // function call
printf(“Square root is %f", ans);
return 0;
}
Math Functions
Use math.h file.
 sqrt
 round
 truc
 sin
 tan
 cos
 Large programs are divided in functional parts, and each of

the functional part is code separately and independently


but later combined into a single unit.

 The independently coded programs are called sub-

programs, and in C they are referred to as “functions”.


The functions can be called and used whenever needed.
Advantages of Using Functions

 The length of a source program can be reduced by

using functions at appropriate places.

 It is easy to locate and isolate a faulty function for

further investigations.

 A function may be used by many other programs.


User-defined Function

 It is a function developed by the programmer using the

program.

 A user-defined functions consists of these elements;

1. Function Definition: Write the function.

2. Function Call: The function is invoked or called by


the main program.
Definition of Functions
A function definition has the following elements;
1. Function name
2. Function return type
3. List of parameters.
4. Local variable declarations
5. Function body statements
6. A return statement.

 All the six elements are grouped together into two parts,
namely; function header and function body.
General Format Of A User-defined Function

return_type function_name (parameter list)


{
local variable declaration;
executable statement 1
executable statement 2;
------ -------
return statement;
}
Function Header
 The functional header consists of the first three (3)

elements:
 function type (also called return type);

 function name and

 the formal parameter list.

No semi colon is used at the end if the function header


section.
Function Header
 Function type specifies the type of value (int, float, void) that the

function is expected to return to the calling function.

 Function Name is any valid C identifier and thus must follow the

same rules of formulation as other variable names in C.

 Formal Parameter List Parameter list declares the variables that

will receive data sent by the calling program. They serve as input
data to the function to carry out the specified task.
Function Body
 Contain the declaration and statements necessary for performing

the required task. It is enclosed on braces { } and contain three


parts;

1. Local declarations that specify the variables needed by the

function

2. Function statements that perform the task of the function

3. Return statement that returns the value evaluated by the

function.
Example

float multiply (float x, float y)

float results; /* declare local variable*/

results = x * y; /*computes the products*/

return (results); /*returns the results*/

}
Function Call
 A function may be called by simply using the name followed

by a list of actual parameters (or arguments) if any enclosed


in parenthesis.

Example

1. Write a program to input 2 numbers then use a function to

calculate the sum of two numbers entered.

2. Write a program to input radius then use a function to

calculate the area of a circle.


# include <stdio.h> Scanf (“%d”,&a);

int add (int x,int y) Printf (“Enter number”);

{ Scanf (“%d”,&b);

int ans; Result=add(a,b); // Function call

ans=x+y; Printf (“Answer is %d”,result);

return ans; }

int main()
# include <stdio.h> scanf(“%d”,&rad);
float area(int r) ar=area(rad); // function call
{ printf(“Answer is %f”, ar);
int ans; float pi=3.14; return 0;
ans= pi*r*r; }
return ans;
}
int main()
{
int rad; float ar;
printf (“Enter radius”);

You might also like