Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
23 views

Classification - Methods of Functions

C NOTES METHODS AND ARRAY

Uploaded by

AJAY J BIT
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Classification - Methods of Functions

C NOTES METHODS AND ARRAY

Uploaded by

AJAY J BIT
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 31

ALPHA BREATHING

RECAP
• Function
• Types of Functions
• Parts of a Function
Contd..
Classification of Functions
Calling Function & Called Function

1.Calling Function:
A function which executes the function call
1
i.e., When a program calls a function, the
program control is transferred from the
calling function to the called function

2. Called Function:
A called function performs the defined task
and returns the program control back to the
calling function.
Categories of Functions
Functions are categorized into 4 types:

1 •Function with no arguments and no return values

2 •Function with arguments but no return values

3 •Function with no arguments but with return values

4 •Function with arguments and return values


Function with no arguments and no return values

• A C function without any arguments means


you cannot pass data to the called function.
• Similarly, function with no return type does
not pass back data to the calling function. 
Logic
Example
void main()
{
    clrscr();
    printf("Welcome to function in C");
    printline();
    printf("Function easy to learn");
    printline();
    getch();
}
void printline()
{
    int i;
    printf("\n");
    for(i=0;i<30;i++)
    {
        printf(“*");
    }
    printf("\n");
}
Output
Welcome to function in C
******************************
Function easy to learn
******************************
Function with arguments but no return
values
• This type of function can accept data from
calling function.
• In other words, you send data to the called
function from calling function but you cannot
send result data back to the calling function.
• Rather, it displays the result on the terminal. 
Logic
Example
void add(int x, int y)
void main()
{
    clrscr();
    add(30,15);
    add(50,50);
    getch();
}
void add(int x, int y)
{
    int result;
    result = x+y;
    printf("Sum of %d and %d is %d.\n\n", x, y, result);
}
Output
Sum of 30 and 15 is 45.
Sum of 50 and 50 is 100.
Function with no arguments but with return
values

• This type of function does not take any


argument but only returns values to the
calling function
Logic
Example
void main()
{
    int z;
    clrscr();
    z = send();
    printf("\nYou entered : %d.", z);
    getch();
}
int send()
{
    int num;
    printf("Enter a no : ");
    scanf("%d",&num);
    return num;
}
Output
Enter a no : 55
You entered : 55
Function with arguments and return values

• This type of function can send arguments


(data) from the calling function to the called
function and wait for the result to be returned
back from the called function to the calling
function.
Logic
Example
void main()
{
    int a,b,z;
    clrscr();
printf(“Enter two numbers:”);
scanf(“%d %d”, &a, &b);
    z = add(a,b);
    printf(“\n Result: %d", z);
    getch();
}
int add (int x, int y)
{
    int result;
    result = x+y;
    return result;
}
Output
Enter two numbers: 20 50
Result: 70
Methods of Passing Parameters/ Function
Calling Methods
2 Methods
1. Call/Pass by Value:
In this approach, the value of the variable is
passed as argument to the function
definition.
2. Call/Pass by Reference:
In this approach, the address(reference) of
the variable is passed as argument to the
function definition.
Call by Value
#include<stdio.h>
void fun(int X, int Y)
void main()
{
int A=10,B=20;
printf("\nValues before calling %d, %d",A, B);
fun(A,B);
printf("\nValues after calling %d, %d",A, B);
}
void fun(int X, int Y)
{
X=11;
Y=22;
}
Output :
Values before calling 10, 20
Values after calling 10, 20
Illustration

Any changes made by value type variables X and


Y will not effect the values of A and B.
Call by Reference
#include<stdio.h>
void fun(int *X, int *Y)
void main()
{
int A=10,B=20;
printf("\nValues before calling %d, %d",A, B);
fun(&A,&B);
printf("\nValues after calling %d, %d",A, B);
}
void fun(int *X, int *Y)
{
*X=11;
*Y=22;
}
Output :
Values before calling 10, 20
Values after calling 11, 22
Illustration

Any changes made by reference type variables


*X and *Y will change the values of A and B
respectively.
Call by Value vs. Call by Reference
Contd..
Call by Value Call by Reference

The copy of value is passed as The address of the variable is


argument to the function passed as argument to the
function

Changes made inside the Changes made inside the


function are not reflected on function are reflected outside
other functions the function also

Actual and formal arguments Actual and formal arguments


will be created in different will be created in same memory
memory location location
Implement the following:

1. Write a C program to check whether the


given number is prime or not using functions.
2. Write a C program to check whether the
given number is armstrong number or not
using functions.

You might also like