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

Function in C

The document outlines topics to be covered in an applied computer programming course including functions in C, pointers and arrays, structures, object oriented programming in C++, and hands-on experience with Arduino libraries and interfacing. Example code is provided to demonstrate function definitions, parameters, return types, and calling functions.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

Function in C

The document outlines topics to be covered in an applied computer programming course including functions in C, pointers and arrays, structures, object oriented programming in C++, and hands-on experience with Arduino libraries and interfacing. Example code is provided to demonstrate function definitions, parameters, return types, and calling functions.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 46

GEC 225: Applied Computer

Programming II
Lecturers:
Prof E. Adetiba
Engr. O Abayomi-Alli
Engr. M. Odusami
Engr. C. Okereke
Mr F. Olaloye

Omega Semester
2018/2019 session
COURSE OUTLINE
Topics

Revision (Data types, Program Structures, Decision and Program Loops)

Functions in C
Storage, Classes and Scope
Pointers and Array
Structures, Unions and Data Storage

Arduino Libraries and Interfacing with the Outside World: Hands-on

Fundamentals of C++
Object Oriented Programming (OOP) with C++ (Basic Features of OOP, Classes
and Objects, Constructors and Destructors, Base and Derived Classes, Class
Hierarchies, Inheritance, Polymorphism)
Revision
FUNCTIONS in C
 Function can be defined as a subprogram which is
meant for doing a specific task.

 A function is a group or collection of statements that


together perform a specific 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().

 In a C program, a function definition will have name,


parentheses pair contain zero or more parameters and
a body.
Differences between Function and Procedures
Procedure Function
1. Procedure is a sub program Functions is sub program which is
which is included with in main intended for specific task. E.g.
program. sqrt()
2. Procedure does not return a Functions may or may not return a
value. value.
3. Procedure cannot be called 3. Function once defined can be
again and again. called anywhere n number of
times.
4. Global variables cannot be used 4. In functions both local and
in procedure. global variables can be used.

5. Procedures can be written only 5. Functions can be written in


in procedural programming such as Modular programming such as C,
Dbase, Foxpro. C++.
FUNCTIONS in C cont.
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.
Advantages of Function
The main advantages of using a function are:
• Easy to write a correct small function
• Easy to read and debug a function.
• Easier to maintain or modify such a function
• Small functions tend to be self documenting
and highly readable
• It can be called any number of times in any
place with different parameters.
Function Definition
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
return (<something>);
}
Component of C- function

From the above form the main components of


function are:
• Return type
• Function name
• Function body
• Return statement
Return Type
 Refers to the type of value (data type) it
would return to the calling portion of the
program.
 It can have any of the basic data types such

as int, float, char, etc.


 When a function is not supposed to return

any value, it may be declared as type void.


Example
1. void function name(- - - - - - - - - -);
2. int function name( - - - - - - - - - - );
3. char function name ( — - - - - - - );
Function Name
 This is the actual name of the function.

 The function name can be any name conforming to


the syntax rules of the variable.

 The function name and the parameter


list/argument together constitute the function
signature.

 This is relevant to the function operation.

Example
output( );
Parameters/ Argument
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.
◦ Actual parameter – This is the argument which is used
in function call.
◦ Formal parameter – This is the argument which is used
in function definition
Actual and Formal Parameters (or) Arguments
 Function parameters are the means of
communication between the calling and the called
functions. The parameters may classify under two
groups.
1. Formal Parameters
2. Actual Parameters
Actual and Formal Parameters (or) Arguments

Formal Parameters
 The formal parameters are the parameters

given in function declaration and function


definition. When the function is invoked, the
formal parameters are replaced by the actual
parameters.
Actual Parameters
 The parameters appearing in the function call

are referred to as actual parameters. The actual


arguments may be expressed as constants,
single variables or more complex expression.
Each actual parameter must be of the same data
type as its corresponding formal parameters.
Formal arguments
 The arguments are called formal arguments (or)
formal parameters, because they represent the
names of data items that are transferred into the
function from the calling portion of the program.
Example:
int biggest (int a, int b)
{
—————————
—————————
—————————
return( );
}
a, b are the formal arguments.
Example 1
1. #include <stdio.h>
2. int sum (int a , int b ) //formal perameters
3. {
4. int c;
5. c = a + b;
6. return(c);
7. }
8. main( )
9. {
10. int x,y,z; //actual paramet
11. printf (“enter value for x,y \n”);
12. scanf (“%d %d”,&x,&y);
13. z = x + y;
14. printf (“ sum is = %d”,z);
15. }
The variables a and b defined in function definition are known as
formal parameters. The variables x and y are actual parameters.
Example 2
/* 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. }
Example cont.
Example 2 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 .
Function Body
 Function body is a compound statement defines
the action to be taken by the function.
 It should include one or more “return” statement

in order to return a value to the calling portion of


the program.
Example 3:
1. int biggest(int a, int b)
2. {
3. if ( a > b)
4. return(a); body of function.
5. else
6. return(b);
7. }
Function Declaration/Prototype
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:
int max(int num1, int num2); or
int max(int, int);
Programs using function
Call
Calling a Function
 Every C program consists of one or more
functions.
 One of these functions must be called as

main.
 Execution of the program will always begin by

carrying out the instructions in main.


 Additional functions will be subordinate to

main.
Calling a Function cont.
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.
Calling a Function cont.
 The arguments are sent to the functions and
their values are copied in the corresponding
function.
 This is a sort of information inter change

between the calling function and called


function. This is known as Parameter passing.
 It is a mechanism through which arguments

are passed to the called function for the


required processing.
Example 4
Write a program to find factorial to the given positive integer,
using function technique.
1. # include <stdio.h>
2. main( )
3. {
4. int n;
5. printf ( “ Enter any positive number\n”);
6. scanf( “%d”, &n);
7. printf( “ The factorial of %d s %d \n”,fact (n));
8. }
9. fact( i)
10. int I;
11. {
12. int j; f = 1 ;
13. for ( j = I; j>0; j - -)
14. f = f * I;
15. return ( f ) ;
Example 5
1. #include <stdio.h> /* function declaration */
2. int max(int num1, int num2);

3. int main () { /* local variable definition */


4. int a = 100;
5. int b = 200;
6. int ret; /* calling a function to get max value */
7. ret = max(a, b);
8. printf( "Max value is : %d\n", ret );
9. return 0;

10.} /* function returning the max between two numbers */


11.int max(int num1, int num2) { /* local variable declaration */
12. int result;

13. if (num1 > num2)

14. result = num1;


15. else

16. result = num2;


17. return result;
Example 6
Write a program to find the value of f(x) as f(x) = x 2

+ 4, for the given of x. Make use of function


technique.
1. # include <stdio.h>
2. main( )
3. {
4. f ( );
5. }
6. f ( )
7. { int x,y ;
8. printf( “ Enter value of x \n”);
9. scanf( “ %d”, & x );
10. y = (x * x + 4);
11. printf ( “ The value of f (x) id %d \n”, y ) ;
Categories of Functions
A function, depending on, whether arguments
are present or not and a value is returned or
not.
A function may be belonging to one of the
following types.
1. Function with no arguments and no return
values.
2. Function with arguments and no return
values.
3. Function with arguments and return values
Functions with no arguments and no return value

A function is invoked without passing any


formal arguments from the calling portion of
a program and also the function does not
return back any value to the called function.
Example 7:
Example 7: Functions with no arguments and no return value

1. #include <stdio.h>
2. main()
3. {
4. void message( ); //Function
declaration
5. message( ); //Function calling
6. }
7. void message( )
8. {
9. printf (“Applied Computer
Programming \n”);
10.printf (“\t GEC225”);
Function with arguments and no return value
 This type of functions passes some formal
arguments to a function but the function does
not return back any value to the caller. It is any
one way data communication between a calling
portion of the program and the function block.
Example 8: Function with arguments and no return value
1. #include <stdio.h>
2. main()
3. {
4. void square(int);
5. printf (“Enter a value for n \n”);
6. scanf (“%d”,&n);
7. square(n);
8. }
9. void square (int n)
10. {
11. int value;
12. value = n * n;
13. printf (“square of %d is %d “,n,value);
14. }
Function with arguments and return value
 The third type of function passes some
formal arguments to a function from a calling
portion of the program and the computer
value is transferred back to the caller. Data
are communicated between the calling
portion and the function block.
Example 9: Function with arguments and return value
1. #include <stdio.h>
2. main()
3. {
4. int square (int);
5. int value;
6. printf (“enter a value for n \n”);
7. scanf(“%d”, &n);
8. value = square(n);
9. printf (“square of %d is %d “,n, value);
10. }
11. int square(int n)
12. {
13. int p;
14. p = n * n;
15. return(p);
16. }
 The keyword VOID can be used as a type specifier when defining a
function that does not return anything or when the function
definition does not include any arguments.
Calling functions by
value or by reference
Call by value
 When the values of arguments are passed from
calling function to a called function, these values
are copied in to the called function.
 If any changes are made to these values in the

called function, there are NO CHANGE the


original values within the calling function.
 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.
Example 10: Call by value
1. #include <stdio.h>
2. main();
3. {
4. int n1,n2,x;
5. int cal_by_val();
6. N1 = 6;
7. N2 = 9;
8. printf( n1 = %d and n2= %d\n”, n1,n2);
9. X = cal_by_Val(n1,n2);
10. Printf( n1 = %d and n2= %d\n”, n1,n2);
11. Printf(“ x= %d\n”, x);
12. / * end of main*/
13. /*function to illustrate call by value*/
14. Cal_by_val(p1,p2)
15. int p1,p2;
16. {
17. int sum;
18. Sum = (p1 + p2);
19. P1 + = 2;
20. P2* = p1;
21. printf( p1 = %d and p2= %d\n”, p1,p2);
22. return( sum);
23. }
24. }
Example 11: 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.}
Output:
11.

12.void swap(int a, int b)


13.{

14.inttmp;
15.tmp = a;

16.a = b;

17.b = tmp;

18.printf(" \nvalues after swap m = %d\n and n = %d", a, b);

19.}
Explanation: Example 11 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.
Call by Reference
 In this method, the actual values are not
passed, instead their addresses are passed.
 The address of the variable is passed to the

function as parameter.
 There is no copying of values since their

memory locations are referenced.


 If any modification is made to the values in

the called function, then the original values


get changed with in the calling function.
 Passing of addresses requires the knowledge

of pointers.
Example 12: 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. } Output:
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. }
Explanation: Example 12 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.
Recursion
 One of the special features of C language is
its support to recursion. Very few computer
languages will support this feature.
 Recursion can be defines as the process of a

function by which it can call itself. The


function which calls itself again and again
either directly or indirectly is known as
recursive function.
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.
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.
Assignment
 Explain in detail Functions in C.
 Explain different types of functions with an
example.
 Explain about I/O functions.
 Discuss about Global and Local variables.
 Explain with examples Call-by-value and
Call-by-reference.
 What is a function signature?
 What does function overloading mean?
 What is a function type specifier?
 What is the purpose of a return statement and
can a function return more than one value?

You might also like