KEMBAR78
GEC225 Module 2 | PDF
0% found this document useful (0 votes)
27 views25 pages

GEC225 Module 2

Uploaded by

Daniel Joseph
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views25 pages

GEC225 Module 2

Uploaded by

Daniel Joseph
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

COVENANT UNIVERSITY

Course Title: Applied Computer Programming II


Course Code: GEC 225
Omega Semester
2022/2023

Lecturers: Prof. E. Adetiba, Mrs Temitope Takpor,


Engr. (Mrs) A.H. Ifijeh, Miss Omolola Ademola
MODULE 2:
FUNCTIONS IN C LANGUAGE
OUTLINE
• 2.1 Definition of a Function

• 2.2 Benefits of C Functions

• 2.3 Structure of C Functions


• 2.3.1 Function Declarations
• 2.3.2 Calling a Function
• 2.3.3 Function Arguments

• 2.4 Function Overloading and Library


A FUNCTION IS A COLLECTION OF
CODE STATEMENTS THAT PERFORMS A
PARTICULAR TASK.
2.1 What FUNCTIONS ARE ALSO CALLED
is a Function METHOD, SUB-ROUTINE OR
in C PROCEDURE.
Language
NOTE THAT EVERY C PROGRAM HAS AT
LEAST ONE FUNCTION, WHICH IS
NAMED MAIN().

Friday, April 14, 2023 4


2.2 Benefits of C functions

01 02 03 04
C functions are used to You can call functions A large C program can The core concept of C
avoid rewriting same any number of times easily be tracked when functions are re-
logic/code repeatedly in a program and from it is divided into usability and to
in a program. any place in the functions. improve
program. understandability of
very large C programs.

Friday, April 14, 2023 5


• A function definition in C programming
consists of a function header and the body
with the following syntax.
2.3
Structure of • return_type function_name(parameter list)
C Functions • {
• body of the function
• }

Friday, April 14, 2023 6


Structure of C Functions cont’d …

Return Type − this is the data type of the value the function returns. A
function may return a value whereas some functions perform the desired
operations without returning a value, in which case the return_type is void.

Function Name − This is the actual name of the function. The function name
and the parameter list together constitute the function signature.

Friday, April 14, 2023 7


Structure of C Functions cont’d …

Parameters − A parameter is a placeholder in C


language, when a function is involed, you pass a value to
the parameter. This value is referred to as the actual
parameter or argument.

The parameter list refers to the type, order, and


number of the parameters of a function.
Parameters are optional in C language, meaning
that there are some functions that may contain
no parameters.
Friday, April 14, 2023 8
Structure of C Functions cont’d …

• Function Body − The function body contains a collection of statements that


define what the function does.
• It begins with the opening brace ({) and follows the closing parenthesis of the
argument list and extends to the closing brace (}).
• The syntax for a typical function body is as shown below:

• int VolumeOfCube(int width, int length, int height)


• {
• int volume;
• volume = width*length*height;
• return volume;
• }

Friday, April 14, 2023 9


Example 2.1 – Creating a function in C

// A function that returns the minimum between two numbers


1. #include<stdio.h>
 Example 2.1 shows the
2. int min(int val1, int val2) source code for a function
3. { called min().
4. int result; /* local variable declaration */
 This function takes two
5. if (val1 < val2) parameters:
6. result = val1; val1 and val2 and returns the
minimum value between the
7. else two.
8. result = val2;
9. return result;
10 Friday, April 14, 2023

10. }
2.3.1 Function Declarations

• A function declaration or prototype tells the compiler about a function's


name, return type, and parameters. It also tells the compiler how to call
the function.

• The syntax of a function prototype is:


return_type function_name(parameter list);

• From Example 2.1, we have:


int min(int val1, int val2);
or
int min(int, int); Friday, April 14, 2023 11
2.3.2 Calling a Function

A called function performs a


When C function is created, You simply pass the required
defined task and when its
you define what the parameters along with the
return statement is executed
function must do, while to function name in order to call
or when its function-ending
use the function, you must a function, and you can store
closing brace is reached, it
call it to perform the the returned value in case the
returns the program control
defined task. function returns a value.
back to the main program.

Friday, April 14, 2023 12


Example 2.2: Calling a Function in C
1. #include <stdio.h>
2. int min(int, int); /* function declaration or prototype*/
3. int main () {
4. int a = 100;
5. int b = 200;
6. int ret;
7. ret = min(a, b); /* calling a function to get the minimum value */
8. printf( ”Min value is : %d\n", ret );
9. return 0;
10. }
11. int min(int val1, int val2) { /* function definition */
12. int result;
13. if (val1 < val2)
14. result = val1;
15. else
16. result = val2;
17. return result;
Friday, April 14, 2023 13
Syntax for C function declaration
• The summary of the syntax for C function
14
declaration, function call and function
definition are shown in Table 2.1.
Table 2.1: Summary of C Function Syntaxes

C functions aspects Syntax

function definition return_type function_name (arguments list)


{
Body of function;
}

function call function_name (arguments list);

function declaration return_type function_name (argument list);


Friday, April 14, 2023
Example 2.3 – A C program to compute the square of an input
1. #include<stdio.h>
2. // function prototype, also called function declaration
3. float square ( float x );
4. // main function, program starts from here
5. int main( )
6. {
7. float m, n ;
8. printf ( "\nEnter some number for finding square \n");
9. scanf ( "%f", &m ) ;
10. // function call
11. n = square ( m ) ;
12. printf ( "\nSquare of the given number %f is %f",m,n );
13. }
14. float square ( float x ) // function definition
15. {
16. float p ;
17. p=x*x;
18. return ( p ) ;
19. }
15 Friday, April 14, 2023
In the Example 2.3 program, the function
“square” is called from main function.

The value of “m” is passed as argument to the


function “square”.

This value is multiplied by itself in this function


and the multiplied value “p” is returned to
main function from function “square”.
2.3.3 Function Arguments

If a function is to use arguments, it must declare variables


that accept the values of the arguments. These variables
are called the formal parameters of the function.

Formal parameters behave like other local variables


inside the function and are created upon entry into the
function and destroyed upon exit.
2.3.3 Function Arguments

• While calling a function, there are two ways in which arguments can be passed to a
function :
• Call by Value: This method copies the actual value of an argument into the formal
parameter of the function.
• Call by Reference: This method copies the address of an argument into the formal
parameter.

• By default, C uses call by value to pass arguments.

Friday, April 14, 2023 18


In call by value method, the value of the variable is
passed to the function as parameter.

The value of the actual parameter can not be


modified by formal parameter.
a) Function
Argument: Call
Note that actual parameter is the argument which
By Value is used in function call while formal parameter is
the argument that is used in function definition.

Different memory is allocated for both actual and


formal parameters because the value of actual
parameter is copied to formal parameter.

Friday, April 14, 2023 19


Example 2.4 - A sample C program to illustrate call by value
1. #include<stdio.h>
2. // function prototype, also called function declaration
3. void swap(int a, int b);
4. int main()
5. {
6. int m = 22, n = 44;
7. // calling swap function by value
8. printf(" values before swap m = %d \nand n = %d", m, n);
9. swap(m, n);
10. }
11.
12. void swap(int a, int b)
13. {
14. int tmp;
15. tmp = a;
16. a = b;
17. b = tmp;
18. printf(" \nvalues after swap m = %d\n and n = %d", a, b);
19. return;
}

20 Friday, April 14, 2023


In call by reference, the address of
the variable is passed to the function
as parameter.

b) Function The value of the actual parameter


Argument: Call can be modified by formal
by reference parameter.

Same memory is used for both actual


and formal parameters since only
address is used by both parameters.

Friday, April 14, 2023 21


Example 2.5 - A sample C program to illustrate call by reference  In Example 2.5 program, the
address of the variables “m” and
• 1. #include<stdio.h>
“n” are passed to the function
• 2. // function prototype, also called function declaration
“swap”.
• 3. void swap(int *a, int *b);
• 4. int main()
• 5. {  These values are not copied to
• 6. int m = 22, n = 44; formal parameters “a” and “b” in
• 7. // calling swap function by reference swap function because, they are

Friday, April 14, 2023


• 8. printf("values before swap m = %d \n and n = %d",m,n); just holding the address of those
• 9. swap(&m, &n); variables.
• 10. }
• 11. void swap(int *a, int *b)  This address is used to access and
• 12. { change the values of the variables.
• 13. int tmp;
• 14. tmp = *a;
• 15. *a = *b;
• 16. *b = tmp;
• printf("\n values after swap a = %d \nand b = %d", *a, *b);
• return;
• 18. } 22
2.4 Function Overloading and Library

• When a function shares a common name, but has two or more different
signatures, it is called an overloaded function. In most cases it is the argument
list that differs across signatures.

• A function library is a collection of functions that share a common area of


interest (e.g., math and time function libraries).

• Many vendors have added new libraries and add-ons to support their products

• You can also create your own functions library in C language.

Friday, April 14, 2023 23


Module 2 Assignment
• Using the concept of function, write a C program to compute the
statistical values Mode, Median, Mean, Standard Deviation, Kurtosis
and Skewness for any input vector.

• Note:
• This assignment should be submitted on Moodle latest before the
next class.

Friday, April 14, 2023 24


Thank You All for
Listening

You might also like