ICS 103 Lecture 6 - Functions in C
ICS 103 Lecture 6 - Functions in C
Programming in C
Lecture 6
University of Hail
College of Computer Sciences and Engineering
Programming in C
(ICS 103)
Lecture 6
C – Functions
https://www.tutorialspoint.com/cprogramming/index.htm
2 02/19/2025
Overview
A function is a group of statements that together
perform a task.
Every C program has at least one function, which is
3 function.
Predefined Functions
The primary goal of software engineering is to write error-free code.
Reusing code that has already been written & tested is one way to
achieve this
C promotes reuse by providing many predefined functions.
The C standard library provides numerous built-in functions that
your program can call.
For example,
strcat() to concatenate two strings,
memcpy() to copy one memory location to another location,
and many more functions.
Mathematical computations.
Input/Output: e.g. printf, scanf
A function can also be referred as a method or a sub-routine or a
procedure, etc
4
Some Mathematical Library Functions
Function Header Purpose Arguments Result
File
abs(x) <stdlib.h> Returns the absolute value int int
of its integer argument x.
Returns the absolute value
fabs(x) of its double argument x.
<math.h> double double
return 0;
}
10
Example
/* function returning the max between two numbers */
int max(int num1, int num2) {
return result;
}11
Why do we use Functions?
There are two major reasons:
1) A large problem can be solved easily by breaking it up
into several small problems and giving the responsibility
of a set of functions to a specific programmer.
It is easer to write two 10 line functions than one 20 line function
and two smaller functions will be easier to read than one long one.
2) They can simplify programming tasks because existing
functions can be reused as the building blocks for new
programs.
Really useful functions can be bundled into libraries.
3) Functions can be executed more than once in a
program. Thus, reduces the overall length of the
program and the chance of error.
12
Reuse of Function Subprograms
Functions can be executed more than once in a
program.
Reduces the overall length of the program and the
chance of error.
Once you have written and tested a function, you
can use it in other programs or functions.
13
Common Programming Errors
Remember to use a #include preprocessor directives for every
standard library from which you are using functions.
Place prototypes for your own function subprogram in the source
file preceding the main function; place the actual function
definitions after the main function.
The acronym NOT summarizes the requirements for argument
list correspondence.
Provide the required Number of arguments
Make sure the Order of arguments is correct
Make sure each argument is the correct Type or that conversion to
the correct type will lose no information.
Include a statement of purpose on every function you write.
Also be careful in using functions that are undefined on some
range of values.
14
Lecture Summary
A function is a group of statements that together
perform a task.
A function declaration tells the compiler about a
function's name, return type, and parameters.
Functions can be executed more than once in a
program.
Reduces the overall length of the program and the
chance of error.
be careful in using functions that are undefined on some
range of values.
15