Skip to main content

Command Palette

Search for a command to run...

Day 7 Of C:basic To Advance Course

Published
3 min read
Day 7 Of C:basic To Advance Course

FUNCTIONS

Points to be Covered:

Introduction to Functions

Structure of Functions

Types of Functions

Advantages of Functions

FUNCTIONS

  • Functions are the basic building block of the program to make the particular task done.

  • Functions is used to increase relaibility of the program, allows reusibility of the code,increase modularity of the program.

  • Function comes with many small terms like Function Definition ,function declaration,Function call,Arguments,Parameters,Return Statements.

STRUCTURE OF FUNCTION

#include<stdio.h>
#include<stdlibr.h>
int main(){                        //Function Prototype
  int a=10,b=12;                  //a,b are parameters
  printf("The value of a is %d and b is %d",a,b);
  great(a,b);                     //Function call
  return 0;                       //Return statement
}
int great(int a,int b){          //function defintition
  int max;
  max=a>b;a:b;
}

Basic Structure of Functions include

  1. Function Prototypes

  2. Function Parameters /Arguments

  3. Function Call

  4. Function Definition

  5. Return Statement

FUNCTION PROTOTYPES

Function Prototype is basically tells about the input datatype given with the function names and other parameters are present.

FUNCTION PARAMETERS

They are basically used to check out what kind of values are passed on to the function.

Parameters are the variables passed through the function for function declaration and function definition.

FUNCTION CALL

Function call is basically calling out the function name to pass the function and use it to print the expected output.

We can call the Functions the function as many times as we want by just writing the function names and their passed parameters.

FUNCTION DEFINITION

Function Definition is basically the defining of a function that can be used in the program anywhere by just calling it out when needed.

RETURN STATEMENTS

Return Statement is used to define in what kind of datatype we want to achieve the following output. For it just write return __ and what kind of value you want by writting the function name.

TYPES OF FUNCTIONS

#include <stdio.h>

// Global variable
int globalVar = 10;

// Global function
void globalFunction() {
    printf("Inside globalFunction: globalVar = %d\n", globalVar);
}

int main() {
    // Local variable in the main function
    int localVar = 20;

    printf("Inside main: globalVar = %d, localVar = %d\n", globalVar, localVar);

    // Accessing global variable and function
    globalFunction();
    // Changing global and local variables
    globalVar = 30;
    localVar = 40;

    printf("After modification inside main: globalVar = %d, localVar = %d\n", globalVar, localVar);
    return 0;
}
  1. GLOBAL FUNCTION

  2. LOCAL FUNCTION

GLOBAL FUNCTION

Global Function is the function that accessed anywhere and anytime in the provided code.

It is basically written outside the given function.

LOCAL FUNCTION

Local Function is the function that can be accessed only once inside the function.

It is written inside the given function.

FINDING THE GCD OF TWO NUMBERS

// Online C compiler to run C program online
#include <stdio.h>
int gcd(int num1,int num2){
    int i,min,res;
    min = (num1 > num2) ? num1 : num2;
    for(i=1;i<=min;i++)
    {
        if(num1%i==0 && num2%i==0)
        res=i;
    }
    return res;
}

int main() {
    int num1,num2,res;
    printf("ENter the Numbers:\n");
    scanf("%d %d",&num1,&num2);
    res=gcd(num1,num2);
    printf("THe GCD of two numbers are %d",res);
}