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

ICS 103 Lecture 6 - Functions in C

This lecture covers the concept of functions in C programming, emphasizing their role in organizing code into manageable tasks. It explains function declarations, definitions, and the importance of reusing functions to reduce errors and simplify programming. Additionally, common programming errors related to functions are highlighted, along with the benefits of using functions in software development.

Uploaded by

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

ICS 103 Lecture 6 - Functions in C

This lecture covers the concept of functions in C programming, emphasizing their role in organizing code into manageable tasks. It explains function declarations, definitions, and the importance of reusing functions to reduce errors and simplify programming. Additionally, common programming errors related to functions are highlighted, along with the benefits of using functions in software development.

Uploaded by

Diaa Uliyan
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

ICS103

Programming in C
Lecture 6

Dr. Mohammed Mahdi Al-Wosaby

University of Hail
College of Computer Sciences and Engineering
Programming in C
(ICS 103)
Lecture 6

C – Functions

Most of the slide material are copied from

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

main(), and all the most trivial programs can define


additional functions.
 You can divide up your code into separate functions.

 How you divide up your code among different functions is

up to you, but logically the division is such that each


function performs a specific task.
 A function declaration tells the compiler about a function's

name, return type, and parameters.


 A function definition provides the actual body of the

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

sin(x),cos(x) <math.h> Returns the sine, cosine, or double double


, tangent of angle x. (in radians)
tan(x)
log(x) <math.h> Returns the natural log of x. double (must be double
positive)
Log10(x) <math.h> Returns base 10 log of x Double (positive) double

pow(x, y) <math.h> Returns xy double, double double


sqrt(x) <math.h> double (must be double
positive)
5
Defining a Function
 The general form of a function definition in C programming
language is as follows −
return_type function_name( parameter list ) {
body of the function
}

 A function definition in C programming consists of a function


header and a function body.
 Return Type − A function may return a value. The

return_type is the data type of the value the function


returns. Some functions perform the desired operations
without returning a value. In this case, the return_type is
the keyword void.
6
Defining a Function
 Function Name − This is the actual name of the
function. The function name and the parameter list
together constitute the function signature.
 Parameters − A parameter is like a placeholder. When
a function is invoked, you pass a value to the
parameter. This value is referred to as actual parameter
or argument. The parameter list refers to the type, order,
and number of the parameters of a function. Parameters
are optional; that is, a function may contain no
parameters.
 Function Body − The function body contains a
collection of statements that define what the function
does.
7
Function Declarations
 A function declaration tells the compiler about a function
name and how to call the function. The actual body of the
function can be defined separately.
 A function declaration has the following parts −
return_type function_name( parameter list );
 For the above defined function max(), the function
declaration is as follows −
int max(int num1, int num2);
 Parameter names are not important in function declaration
only their type is required, so the following is also a valid
declaration −
int max(int, int);
8
Calling a Function
 While creating a C function, you give a definition of what the
function has to do.
 To use a function, you will have to call that function to
perform the defined task.
 When a program calls a function, the program control is
transferred to the called function.
 A called function performs a defined task and when its
return statement is executed or when its function-ending
closing brace is reached, it returns the program control back
to the main program.
 To call a function, you simply need to pass the required
parameters along with the function name, and if the function
returns a value, then you can store the returned value.
9
Example
#include <stdio.h>
/* function declaration */
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;
}
10
Example
/* function returning the max between two numbers */
int max(int num1, int num2) {

/* local variable declaration */


int result;

if (num1 > num2)


result = num1;
else
result = 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

You might also like