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

Chap - 4 Functions in C Language

Uploaded by

Zidama A SOULAMA
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Chap - 4 Functions in C Language

Uploaded by

Zidama A SOULAMA
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

Programming in Practical Engineering

Languages

Chapter 4 : Functions in C language

BURKINA INSTITUTE OF TECHNOLOGY


Electrical Engineering
(E.E)

Academic year : 2024-2025

Semester 3

12 octobre 2024
Course outline

1 Function Declaration and Definition

2 Parameters and Arguments

3 Recursivity

4 Types of functions

(B.i.t/E.E, S3) Engineering Practical Programming 12 octobre 2024 2 / 34


Quote

A programming language is a convention for giving commands to a

computer. It’s not supposed to be obscure, weird and full of

subtle traps...

Dave Small

(B.i.t/E.E, S3) Engineering Practical Programming 12 octobre 2024 3 / 34


1. Function Declaration and
Definition

(B.i.t/E.E, S3) Engineering Practical Programming 12 octobre 2024 4 / 34


1. Function Declaration and Definition

1.1. Introduction
A function in C is a set of statements that when called
perform some specific tasks.

It is the basic building block of a C program that provides


modularity and code reusability.

The programming statements of a function are enclosed within


{ } braces, having certain meanings and performing certain
operations.

They are also called subroutines or procedures in other


languages.

(B.i.t/E.E, S3) Engineering Practical Programming 12 octobre 2024 5 / 34


1. Function Declaration and Definition

1.2. Predefined functions


So it turns out you already know what a function is.

We have been using it the whole time while studying this

course!

For example, main() is a function, which is used to execute

code, and printf(), scanf() are functions; used to

output/print text to the screen.

(B.i.t/E.E, S3) Engineering Practical Programming 12 octobre 2024 6 / 34


1. Function Declaration and Definition

1.3. Syntax of functions in C


The syntax of function can be divided into 3 aspects:

Function Declaration

Function Definition

Function Calls

(B.i.t/E.E, S3) Engineering Practical Programming 12 octobre 2024 7 / 34


1. Function Declaration and Definition

1.3. Function declarations


In a function declaration, we must provide the function name,
its return type, and the number and type of its parameters.

A function declaration tells the compiler that there is a


function with the given name defined somewhere else in the
program.

Note: A function in C must always be declared globally before


calling it.

(B.i.t/E.E, S3) Engineering Practical Programming 12 octobre 2024 8 / 34


1. Function Declaration and Definition
1.3.1. Syntax of functions declaration (1/2)
1 return_type functionName ( param_1 , param_2 , ... , param_n ) ;

The parameter name is not mandatory while declaring functions.


We can also declare the function without using the name of the
data variables.

1 int sum ( int a , int b ) ; // declaration with parameter names


2 int sum ( int , int ) ; // declaration without parameter names

(B.i.t/E.E, S3) Engineering Practical Programming 12 octobre 2024 9 / 34


1. Function Declaration and Definition
1.3.1. Syntax of functions declaration (2/2)

(B.i.t/E.E, S3) Engineering Practical Programming 12 octobre 2024 10 / 34


1. Function Declaration and Definition
1.4. Function definition
The function definition consists of actual statements which
are executed when the function is called (i.e. when the
program control comes to the function ).

A C function is generally defined and declared in a single


step because the function definition always starts with the
function declaration so we do not need to declare it
explicitly.

The below example serves as both a function definition and a


declaration.

(B.i.t/E.E, S3) Engineering Practical Programming 12 octobre 2024 11 / 34


1. Function Declaration and Definition

1.4.1. Syntax of functions definition (1/2)


1 return_type function_name ( type param_1 ,... , type param_n )
2 {
3 // body of the function
4 }

(B.i.t/E.E, S3) Engineering Practical Programming 12 octobre 2024 12 / 34


1. Function Declaration and Definition

1.4.1. Syntax of functions definition (2/2)

(B.i.t/E.E, S3) Engineering Practical Programming 12 octobre 2024 13 / 34


1. Function Declaration and Definition
1.4.2. Syntax of functions definition: example
1 // function to calculate summ of two integers
2 int calculateSum ( int x , int y )
3 {
4 int sum = x + y ;
5 return sum ;
6 }

1 // Function to convert Fahrenheit to Celsius


2 float toCelsius ( float fahrenheit )
3 {
4 int t = (5.0 / 9.0) * ( fahrenheit - 32.0) ;
5 return t ;
6 }

(B.i.t/E.E, S3) Engineering Practical Programming 12 octobre 2024 14 / 34


1. Function Declaration and Definition
1.5. Function call
A function call is a statement that instructs the compiler to
execute the function.

We use the function name and parameters in the function call.

In the below example, the first sum function is called and


10,30 are passed to the sum function.

After the function call sum of a and b is returned and


control is also returned back to the main function of the
program.

Note: If not called, the function statements will not be


executed.

(B.i.t/E.E, S3) Engineering Practical Programming 12 octobre 2024 15 / 34


1. Function Declaration and Definition

1.5.1. Working of function in C

(B.i.t/E.E, S3) Engineering Practical Programming 12 octobre 2024 16 / 34


1. Function Declaration and Definition
1.5.2. Example
1 # include < stdio .h >
2
3 /* Function that takes two parameters
4 a and b as inputs and returns
5 their sum */
6 int sum ( int a , int b ) {
7 return a + b ;
8 }
9
10 // Driver code
11 int main () {
12 /* Calling sum function and
13 storing its value in add variable */
14 int add = sum (10 , 30) ;
15
16 printf ( " Sum is : % d " , add ) ;
17 return 0;
18 }

(B.i.t/E.E, S3) Engineering Practical Programming 12 octobre 2024 17 / 34


2. Parameters and Arguments

(B.i.t/E.E, S3) Engineering Practical Programming 12 octobre 2024 18 / 34


2. Parameters and Arguments

2.1. Definition
Function Arguments (also known as Function Parameters) are
the data that is passed to a function.

Example:

int functionName(int var1, int var2);

(B.i.t/E.E, S3) Engineering Practical Programming 12 octobre 2024 19 / 34


2. Parameters and Arguments

2.2. Conditions of return types and arguments


In C programming language, functions can be called either with or
without arguments and might return values. They may or might not
return values to the calling functions.

1 Function with no arguments and no return value

2 Function with no arguments and with return value

3 Function with argument and with no return value

4 Function with arguments and with return value

(B.i.t/E.E, S3) Engineering Practical Programming 12 octobre 2024 20 / 34


2. Parameters and Arguments

2.3. Passing parameters to functions


The data passed when the function is being invoked is known
as the Actual parameters.

In the below program, 10 and 30 are known as actual


parameters.

Formal Parameters are the variable and the data type as


mentioned in the function declaration.

In the below program, a and b are known as formal parameters.

(B.i.t/E.E, S3) Engineering Practical Programming 12 octobre 2024 21 / 34


2. Parameters and Arguments
2.3.1. Passing parameters to functions: example

(B.i.t/E.E, S3) Engineering Practical Programming 12 octobre 2024 22 / 34


2. Parameters and Arguments
2.4. How does C function work
1 Declaring a function: Declaring a function is a step where we
declare a function. Here we specify the return types and
parameters of the function.
2 Defining a function: This is where the function’s body is
provided. Here, we specify what the function does, including
the operations to be performed when the function is called.
3 Calling the function: Calling the function is a step where we
call the function by passing the arguments in the function.
4 Executing the function: Executing the function is a step
where we can run all the statements inside the function to
get the final result.
5 Returning a value: Returning a value is the step where the
calculated value after the execution of the function is
returned.
(B.i.t/E.E, S3) Engineering Practical Programming 12 octobre 2024 23 / 34
3. Recursivity

(B.i.t/E.E, S3) Engineering Practical Programming 12 octobre 2024 24 / 34


3. Recursivity

3.1. Definition
Recursion is the technique of making a function call itself.

This technique provides a way to break complicated problems


down into simple problems which are easier to solve.

Recursion may be a bit difficult to understand.

The best way to figure out how it works is to experiment with


it.

(B.i.t/E.E, S3) Engineering Practical Programming 12 octobre 2024 25 / 34


3. Recursivity
3.2. Example
1 # include < stdio .h >
2 int sum ( int k ) ;
3
4 int main () {
5 int result = sum (10) ;
6 printf ( " % d " , result ) ;
7 return 0;
8 }
9
10 int sum ( int k ) {
11 if ( k > 0) {
12 return k + sum ( k - 1) ;
13 } else {
14 return 0;
15 }
16 }

(B.i.t/E.E, S3) Engineering Practical Programming 12 octobre 2024 26 / 34


3. Recursivity

3.3. Example explanation


When the sum() function is called, it adds parameter k to the
sum of all numbers smaller than k and returns the result.
When k becomes 0, the function just returns 0.
When running, the program follows these steps:

(B.i.t/E.E, S3) Engineering Practical Programming 12 octobre 2024 27 / 34


4. Types of functions

(B.i.t/E.E, S3) Engineering Practical Programming 12 octobre 2024 28 / 34


4. Types of functions

4.1. Definition
There are two types of functions in C:
Library Functions
User Defined Functions

(B.i.t/E.E, S3) Engineering Practical Programming 12 octobre 2024 29 / 34


4. Types of functions

4.2. Library Functions


A library function is also referred to as a built-in
function.

A compiler package already exists that contains these


functions, each of which has a specific meaning and is
included in the package.

Built-in functions have the advantage of being directly


usable without being defined, whereas user-defined functions
must be declared and defined before being used.

Example: pow(), sqrt(), strcmp(), strcpy() etc.

(B.i.t/E.E, S3) Engineering Practical Programming 12 octobre 2024 30 / 34


4. Types of functions
4.2.1. Library Functions: example
1 # include < math .h >
2 # include < stdio .h >
3
4 int main () {
5 double x ;
6 x = 49;
7
8 // Computing the square root with
9 // the help of predefined C
10 // library function
11 double squareRoot = sqrt ( x ) ;
12
13 printf ( " The Square root of %.2 lf = %.2 lf " , x , squareRoot ) ;
14 return 0;
15 }

(B.i.t/E.E, S3) Engineering Practical Programming 12 octobre 2024 31 / 34


4. Types of functions

4.3. User Defined Functions


Functions that the programmer creates are known as
User-Defined functions or tailor-made functions.

User-defined functions can be improved and modified according


to the need of the programmer.

Whenever we write a function that is case-specific and is not


defined in any header file, we need to declare and define our
own functions according to the syntax.

(B.i.t/E.E, S3) Engineering Practical Programming 12 octobre 2024 32 / 34


4. Types of functions
4.3.1. User Defined Functions: example
1 # include < stdio .h >
2
3 int sum ( int a , int b ) {
4 int c = a * a + b * b ;
5 return c ;
6 }
7
8 int main () {
9 int a = 6 , b = 4;
10
11 // function call
12 int res = sum (a , b ) ;
13
14 printf ( " Sum is : % d " , res ) ;
15 return 0;
16 }

(B.i.t/E.E, S3) Engineering Practical Programming 12 octobre 2024 33 / 34


==END==

(B.i.t/E.E, S3) Engineering Practical Programming 12 octobre 2024 34 / 34

You might also like