PPS Ch-9.Functions
PPS Ch-9.Functions
The above code segments is a function named calculate. This function accepts two arguments i.e.
num 1 and num2 of the type int. The return_value is int, thus this function will return an integer value.
So, when this function is called in the program, it will perform its task which is to calculate the sum of
any two numbers and return the result of the summation.
Note that if the function is returning a value, it needs to use the keyword
‘return’.
Function Call
• Any function (including main) could utilize any other function definition
that exist in a program – hence it is said to call the function (function call).
• To call a function (i.e. to ask another function to do something for the
calling function), it requires the FunctionName followed by a list of actual
parameters (or arguments), if any, enclosed in parenthesis.
Function Call cont…
• If the function requires some arguments to be passed along, then the
arguments need to be listed in the bracket ( ) according to the specified
order. For example:
void Calc(int, double, char, int);
void main(void) {
int a, b;
double c;
char d;
// Function Call
Calc(a, c, d, b);
}
Function Call cont…
• If the function returns a value, then the returned value need to be assigned
to a variable so that it can be stored. For example:
int GetUserInput (void); /* function prototype*/
void main(void) {
int input;
input = GetUserInput( );
}
• However, it is perfectly okay (syntax wise) to just call the function without
assigning it to any variable if we want to ignore the returned value.
• We can also call a function inside another function. For example:
printf(“User input is: %d”, GetUserInput( ));
Function call cont…
• There are 2 ways to call a function:
– Call by value
• In this method, only the copy of variable’s value (copy of actual parameter’s value) is passed
to the function. Any modification to the passed value inside the function will not affect the
actual value.
• In all the examples that we have seen so far, this is the method that has been used.
– Call by reference
• In this method, the reference (memory address) of the variable is passed to the function. Any
modification passed done to the variable inside the function will affect the actual value.
• To do this, we need to have knowledge about pointers and arrays, which will be discussed in
chapter pointer.
Basic Skeleton…
Basic skeleton…
#include <stdio.h> void fn1(void)
{
//function prototype local variable
//global variable declaration declaration;
statements;
}
void main(void)
{ void fn2(void)
local variable declaration; {
statements; local variable
declaration;
fn1( );
statements;
fn2( ); }
}
19
Examples
• A function may
– Receive no input parameter and return nothing
– Receive no input parameter but return a value
– Receive input parameter(s) and return nothing Receive input
parameters(s) and return a value
Example 1: receive nothing and return nothing
#include <stdio.h>
void greeting(void); /* function
prototype */
void main(void){ • In this example, function greeting does not receive
any arguments from the calling function (main), and
greeting( ); does not return any value to the calling function,
greeting( ); } hence type ‘void’ is used for both the input
arguments and return data type.
void greeting(void){ • The output of this program is:
printf("Have fun!! \n"); } Have fun!!
Have fun!!
Example 2: receive nothing and return a value
#include <stdio.h>
int getInput(void); int getInput(void)
{
void main(void){ int number;
printf("Enter a number:");
int num1, num2, sum; scanf("%d“,&number);
num1 = getInput( ); return number;
}
num2 = getInput( );
sum = num1 + num2; Sample Output:
Enter a number: 3
printf("Sum is %d\n",sum); } Enter a number: 5
Sum is 8
Example 3: receive parameter(s) and return nothing
#include <stdio.h>
int getInput(void)
int getInput(void); {
int number;
void displayOutput(int); printf("Enter a number:");
scanf("%d",&number);
void main(void){
int num1, num2, sum; return number;
}
num1 = getInput();
void displayOutput(int sum)
num2 = getInput(); {
printf("Sum is %d \n",sum);
sum = num1 + num2; }
displayOutput(sum);}
Example 4: receive parameters and return a value
#include <stdio.h>
int calSum(int,int); /*function protototype*/
void main(void){int sum, num1, num2;
printf(“Enter two numbers to calculate its sum:”);
scanf(“%d%d”,&num1,&num2);
sum = calSum(num1,num2); /* function call */
printf(“\n %d + %d = %d”, num1, num2, sum); }
int calSum(int val1, int val2) /*function definition*/
{ int sum;
sum = val1 + val2;
return sum; }
Example 4 explanation
In this example, the calSum function receives input parameters of type int from the
calling function (main).
The calSum function returns a value of type int to the calling function.
Therefore, the function definition for calSum:
int calSum(int val1, int val2)
Note that the function prototype only indicates the type of variables used, not the
names.
int calSum(int,int);
Note that the function call is done by (main) calling the function name (calSum), and
supplying the variables (num1,num2) needed by the calSum function to carry out its
task.
Function – complete flow
#include<stdio.h>
int calSum(int, int);
void main( );
{
int num1, num2, sum;
printf(“Enter 2 numbers to calculate its sum:”);
scanf(“%d %d”, &num1, &num2);
38
Summary
• In this chapter, you have learnt:
– Standard vs User Define functions
– Function prototype, function definition and function call
– Formal vs Actual parameters
– Local vs Global variables
– Auto vs Static storage class
THANK YOU