Uploaded by Izi Kome

C Functions

advertisement
www.covenantuniversity.
Raising a new Generation of Leaders
5
Course Title: Applied
Computer Programming I.
Course Code: GEC 215
Alpha Semester
2018/2019.
Course Outline
•
•
•
•
•
•
•
•
•
•
•
•
Software Development Life Cycle (SDLC)
Introduction to Linux Operating System, Installation, Understanding
Ubuntu Desktop, Basic Command and Security Concept.
Understand C Language Overview and Program Structure, and Data
types.
Fundamentals of C Operators and Escape Sequence
Decision making in C
Program Loops in C
Uses of Functions in C
Understand Storage Classes and Scope
Understand Pointers and Use Pointers Effectively
Create Structures, Unions and Data Storage
Use Arduino Libraries and Interfacing with the Outside World
Understand the basics of Object Oriented Programming such as C++
2
www.covenantuniversity.
Raising a new Generation of Leaders
C-Functions
Functions in C
A function is a group of statements that
together perform a task.
A function can also be referred as a
method or a sub-routine or a
procedure, etc.
Every C program has at least one
function, which is main().
4
Functions in C
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.
5
Functions in C
A function definition in C programming
consists of a function header and
a function body.
return_type function_name(
parameter list )
{
body of the function
}
6
Functions in C
Return Type − is the data type of the value
the function returns.
A function may return a value.
Some functions perform the desired
operations without returning a value. In this
case, the return_type is the keyword void.
Function Name − This is the actual name of
the function.
The function name and the parameter list
together constitute the function signature.
7
Functions in C
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.
8
Functions in C
Example 1:
/* function returning the max between two numbers */
1.
#include<stdio.h>
2.
int max(int num1, int num2)
3.
{ /* local variable declaration */
4.
int result;
5.
if (num1 > num2)
6.
result = num1;
7.
Else
8.
result = num2;
9.
return result;
10. }
9
Functions in C
Example 1 shows the source code
for a function called max().
This function takes two parameters:
num1 and num2 and
returns the maximum value
between the two .
10
Function Declarations
A function declaration or prototype: tells the compiler
about a function's name, return type, and parameters.
or
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.
return_type function_name( parameter list );
From Example 1:
int max(int num1, int num2);
int max(int, int);
or
11
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.
• 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.
12
Example 2: Calling a Function in C
1.
#include <stdio.h>
2.
int max(int num1, int num2);
3.
int main () {
/* function declaration */
/* local variable definition */
4.
int a = 100;
5.
int b = 200;
6.
int ret;
7.
ret = max(a, b);
8.
printf( "Max value is : %d\n", ret );
9.
/* calling a function to get max value */
return 0;
10. }
/* function returning the max between two numbers */
11. int max(int num1, int num2) {
12.
int result;
13.
if (num1 > num2)
14.
result = num1;
15.
16.
17.
/* local variable declaration */
else
result = num2;
return result;
18. }
13
C function declaration, function call and
function definition:
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);
14
Example 3 program for C function
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
Output:
15.{
16.float p ;
Enter some number for finding square
17.p = x * x ;
2
18.return ( p ) ;
Square of the given number 2.000000 is 4.000000
19.}
15
Explanation: Example 3 program
• In the example program, 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 multiplied value “p” is
returned to main function from
function “square”.
16
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.
While calling a function, there are two ways in which arguments can be
passed to a function :
•
•
Calling by Value: This method copies the actual value of an argument
into the formal parameter of the function.
Calling by Reference: This method copies the address of an argument
into the formal parameter.
By default, C uses call by value to pass arguments.
17
Function Argument: Call By Value
Call by value:
• 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.
• Different Memory is allocated for both actual and formal
parameters. Because, value of actual parameter is copied to
formal parameter.
Note:
• Actual parameter – This is the argument which is used in
function call.
• Formal parameter – This is the argument which is used in
function definition
18
Example 4 program for C function (using 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. }
Output:
19
Explanation: Example 4 program
In this program, the values of the variables
“m” and “n” are passed to the function
“swap”.
These values are copied to formal
parameters “a” and “b” in swap function
and used.
20
Call by reference:
In call by reference method, the
address of the variable is passed to the
function as parameter.
• The value of the actual parameter can
be modified by formal parameter.
• Same memory is used for both actual
and formal parameters since only
address is used by both parameters.
21
Example 5 program for C function (using call by reference)
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 reference
8.
printf("values before swap m = %d \n and n = %d",m,n);
9.
swap(&m, &n);
10.
}
11.
void swap(int *a, int *b)
12.
{
13.
int tmp;
14.
tmp = *a;
15.
*a = *b;
16.
*b = tmp;
17.
printf("\n values after swap a = %d \nand b = %d", *a, *b);
18.
}
Output:
22
Explanation: Example 5 program
• In this program, the address of the variables “m” and
“n” are passed to the function “swap”.
• These values are not copied to formal parameters “a”
and “b” in swap function.
• Because, they are just holding the address of those
variables.
• This address is used to access and change the values
of the variables.
23
Function Library
• A function library is a collection functions that
share a common area of interest (e.g. Math,
Time functions in Arduino C).
• Many vendors have added new libraries to
support products and add-ons they sell for
Arduino family.
• You can create your own functions library in C
language.
24
Function Signature and Function Prototype
Int LeapYear(int year) // Function Signature
Int LeapYear(int year);
// Function Prototype (note semicolon at
the end!)
25
Function Body
int VolumeOfCube(int width, int length, int height)
{
int volume;
volume = width*length*height;
return volume;
}
• NB: The function body begins with the opening
brace ({) and follows the closing parenthesis of the
argument list and extends to the closing to the
closing brace (}). It starts where the function
signature ends.
26
Overloaded Function
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
This is a C++ concept. Technically C
Programming does not allow overloading.
But since Arduino uses C++ compiler, it
allows it.
27
Revision Exercises
1.What is a function?
2. What are the two principal components
of a function?
3.What is a function signature?
4.What does function overloading mean?
5.What is a function type specifier?
6.What is the purpose of a return
statement and can a function return more
than one value?
7.Name 3 things you should strive for
when writing your own functions?
28
www.covenantuniversity.
Raising a new Generation of Leaders
Thank you all for
listening…
Download