Lecture6 C

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 19

MBEYA UNIVERSITY OF SCIENCE AND

TECHNOLOGY
COLLEGE OF SCIENCE AND TECHNICAL EDUCATION
DEPARTMENT OF MEDICAL SCIENCES AND
TECHNOLOGY

ORDINARY DIPLOMA IN BIOMEDICAL ENG.


NTA 6
CSET 06101: Basic Computer Programming

Course Instructor: MISS. C. MWABEZA


Outline
1. What is C function?
2.Types of C functions
3. Uses of C functions
4. Advantage of C functions
5. C function declaration, function call and definition
6. Simple Example Program for C Function
7. How to call C functions in a program?
8. Overview
What is C Function?
 In short: A function is a group of statements that together perform a
task.
 C functions are basic building blocks in a program.
 Long Answer: A large C program is divided into basic building blocks
called C function. C function contains set of instructions enclosed by
“{ }” which performs specific operation in a C program. Actually,
Collection of these functions creates a C program
 A large program in c can be divided to many subprogram
 The subprogram posses a self contain components and have well
define purpose.
 The subprogram is called as a function
 Basically a job of function is to do something
 C program contain at least one function which is main().
Types of C functions

-printf()
-scanf()
-sqrt()
-getchar()
Standard Library Functions:
• Library functions in C language are inbuilt functions which are
grouped together and placed in a common place called library.
Each library function in C performs specific operation.
• The standard library functions are built-in functions in C
programming to handle tasks such as mathematical
computations, I/O processing, string handling etc.
These functions are defined in the header file. When you include the
header file, these functions are available for use. For example:
➤ The printf() is a standard library function to send formatted output
to the screen (display output on the screen). This function is
defined in "stdio.h" header file.
➤ There are other numerous library functions defined under "stdio.h",
such as scanf(), printf(), getchar() etc. Once you include "stdio.h"
in your program, all these functions are available for use.
User-defined Functions:
• As mentioned earlier, C allow programmers to define
functions. Such functions created by the user are called
user-defined functions.
• Depending upon the complexity and requirement of the
program, you can create as many user-defined functions as
you want.
Uses of C functions:
 C functions are used to avoid rewriting same logic/code again and again in a
program.
 There is no limit in calling C functions to make use of same functionality
wherever required.
 We can call functions any number of times in a program and from any place in
a program.
 A large C program can easily be tracked when it is divided into functions.
 The core concept of C functions are, re-usability, dividing a big task into small
pieces to achieve the functionality and to improve understandability of very
large C programs.
Advantages of Function
 It is much easier to write a structured program where a large
program can be divided into a smaller, simpler task.
 Allowing the code to be called many times
 Easier to read and update
 It is easier to debug a structured program where there error is easy
to find and fix
C function declaration, function call and function
definition:
There are 3 aspects in each C function. They are:
 Function declaration or prototype – this informs compiler about
the function name, function parameters and return value’s data
type.
 Function call - this calls the actual function
 Function definition – this contains all the statements to be
executed.
#include<stdio.h> Example 1
// function prototype, also called function declaration
float square ( float x ); Output:
// main function, program starts from here Enter some number for finding square 2
int main( ) Square of the given number 2.000000 is
{ 4.000000
float m, n ;
printf ( "\n Enter the number to find it’s square \n“ ); Enter some number for finding square 4
scanf ( "%f", &m ) ; Square of the given number 2.000000 is
// function call 16.000000
n = square ( m ) ;
printf ( "\n Square of the given number %f is %f", m, n );
}
float square ( float x ) // function definition
{
float p ;
p=x*x;
return p ;
}
Example 2
#include <stdio.h> 10 }
/* function declaration */ /* function returning the max between
int max(int num1, int num2); two numbers */
int main () { int max(int num1, int num2) {
/* local variable definition */ /* local variable declaration */
int a = 200; int result;
int b = 800; if (num1 > num2)
int ret; result = num1;
/* calling a function to get max value else
*/ result = num2;
ret = max(a, b); return result;
printf( "Max value is : %d\n", ret ); }
return 0;
Overview
C functions are basic building blocks in every C program.
There are 2 types of functions in C. They are Library functions and User
defined functions
Key points to remember while writing functions in C language:
 All C programs contain main() function which is mandatory.
 main() function is the function from where every C program is started to
execute.
 Name of the function is unique in a C program.
 C Functions can be invoked from anywhere within a C program.
 There can any number of functions be created in a program. There is no
limit on this.
 There is no limit in calling C functions in a program.
 All functions are called in sequence manner specified in main() function.
 One function can be called within another function.
 C functions can be called with or without arguments/parameters. These
arguments are nothing but inputs to the functions.
 C functions may or may not return values to calling functions. These
values are nothing but output of the functions.
 When a function completes its task, program control is returned to the
function from where it is called.
 There can be functions within functions.
 Before calling and defining a function, we have to declare function
prototype in order to inform the compiler about the function name,
function parameters and return value type.
 C function can return only one value to the calling function.
 When return data type of a function is “void”, then, it won’t return any
values
 When return data type of a function is other than void such as “int, float,
double”, it returns value to the calling function.
 main() program comes to an end when there is no functions or
commands to execute.
Thank You

You might also like