Function in C
Function in C
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
Functions in C
Storage, Classes and Scope
Pointers and Array
Structures, Unions and Data Storage
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.
Example
output( );
Parameters/ Argument
Parameters − A parameter is like a placeholder.
Formal Parameters
The formal parameters are the parameters
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
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
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
4. int main()
5. {
9. swap(m, n);
10.}
Output:
11.
14.inttmp;
15.tmp = a;
16.a = b;
17.b = tmp;
19.}
Explanation: Example 11 program
In this program, the values of the variables
“m” and “n” are passed to the function “swap”.
function as parameter.
There is no copying of values since their
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”.