5.1 Types of User Defined Function

ā§Ģ.ā§§ āĻĒā§āĻ°āĻ•āĻžāĻ°āĻ­ā§‡āĻĻ: āĻ‡āĻ‰āĻœāĻžāĻ° āĻĄāĻŋāĻĢāĻžāĻ‡āĻ¨āĻĄ āĻĢāĻžāĻ‚āĻļāĻ¨ (ā§Ē)

  1. [1] Write a user-defined function that prints "Hello World" inside the function [Function Prototype: void print_hello(void)]

  2. [2] Pass an integer number to a user-defined function and print the given integer number inside the function [Function Prototype: void print_int(int)]

  3. [3] Write a user-defined function that takes input of an integer number inside the function and returns the number [Function Prototype: int input_int()]

  4. [4] Calculate the summation of two integer numbers using a user-defined function and return the result [Function Prototype: int summation(int, int)]

āĻ¸āĻŽāĻžāĻ§āĻžāĻ¨

[1] Write a user-defined function that prints "Hello World" inside the function [Function Prototype: void print_hello(void)]

#include<stdio.h>

void print_hello(void); // Function Prototype

int main(){

    print_hello(); // Function Calling
    
    return 0;
}

// Function Definition
void print_hello(void){
    printf("Hello World\n");
}

[2] Pass an integer number to a user-defined function and print the given integer number inside the function [Function Prototype: void print_int(int)]

#include<stdio.h>

void print_int(int num); // Function Prototype
//void print_int(int);

int main(){
    int a = 10;
    
    print_int(42); // Function Calling by Passing Value (Constant Literal)
    print_int(a);// Function Calling by Passing Value (Variable)
    
    return 0;
}

// Function Definition
void print_int(int num){
    printf("%d\n", num);
}

[3] Write a user-defined function that takes input of an integer number inside the function and returns the number [Function Prototype: int input_int()]

#include<stdio.h>

int input_int(void); // Function Prototype

int main(){
    int number;
    
    number = input_int(); // Funcion Return Value Assignment
    printf("You entered: %d\n", number);
    
    return 0;
}

// Function Definition
int input_int(void){
    int num;
    printf("Enter an integer: ");
    scanf("%d", &num);
    return num;
}

[4] Calculate the summation of two integer numbers using a user-defined function and return the result [Function Prototype: int summation(int, int)]

#include<stdio.h>

int summation(int a, int b);
//int summation(int, int);

int main() {
    int a = 5;
    int b = 7;
    
    printf("Summation of %d and %d: %d\n", a, b, summation(a, b));
    
    return 0;
}

int summation(int a, int b) {
    int sum;
    sum a + b;
    return sum;
}

Last updated