C Functions
C Functions
Computer Programming I
Course Outline
The parameter list refers to the type, order, and number of the
parameters of a function.
From Example 1:
int max(int num1, int num2); or
int max(int, int);
Calling a Function
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.
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
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);
Output:
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. }
Explanation: Example 4 program
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?
Thank you all for
listening…