Thread: How to make function

  1. #1
    Registered User
    Join Date
    May 2021
    Posts
    66

    How to make function

    I've written program that store numbers from user

    Code:
    #include<stdio.h>
    
    int main (void)
    {
        int i, N;
        
        printf(" size of numbers: " );
        scanf("%d",&N);                                  
        
        int Number[N];                            
            
        
        for(i=0; i<N; i++)                               
        {
            printf("print Number : ", Number[i]);
            scanf("%d",&Number[i]);                   
        }
        
        
        return 0;
    
    
    }
    size of numbers: 4
    print Number : 1
    print Number : 2
    print Number : 3
    print Number : 4

    How to make function that store numbers on each function call. so whenever I call the I'll get the updated value of numbers.

    How to make a function ? What I need to pass into function from main function ?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,675
    You could start with this idea.
    Code:
    #include<stdio.h>
    
    void function(int N, int Number[])
    {
        for(int i=0; i<N; i++)                               
        {
            printf("print Number : ", Number[i]);
            scanf("%d",&Number[i]);                   
        }
    }
     
    int main (void)
    {
        int i, N;
         
        printf(" size of numbers: " );
        scanf("%d",&N);                                  
         
        int Number[N];                            
             
        function(N,Number); 
         
        return 0;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2021
    Posts
    66
    Quote Originally Posted by Salem View Post
    You could start with this idea.

    Thank you you're idea is very useful.

    I've written code that can store N numbers in array and It can remove any number from array

    I don't want to store and remove number in array every time

    I've decided to use function.

    when I call to function1 It should be store numbers
    when I call to function2 It should be remove requested number

    Code:
    #include<stdio.h>
    
    int main (void)
    {
        int i, N, R;
        
        printf("size of numbers: " );
        scanf("%d",&N);                                  
        
        int Number[N];                            
            
        
        for(i=0; i<N; i++)                               
        {
            printf("print Number : ", Number[i]);
            scanf("%d",&Number[i]);                   
        }
        
    	printf("Remove number : " );
        scanf("%d",&R);
        
    	for(i=R; i<N; i++)                               
        {
    		Number[i] = Number[ i + 1];         
                              
        }
    	
    	for(i=0; i< N-1; i++)                               
        {
            printf(" %d  ", Number[i]);
            
        }
    
    
        return 0;
    
    
    
    
    }
    I've to pass the number that I want to remove number. I don't understand how to implement function that remove number from array.

  4. #4
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    338

    Post

    implement function that remove number from array.
    Code:
    #include <stdio.h>
    
    #define maxSize 64
    
    int number[maxSize];
    
    void display() {
        for (int i=0; i<maxSize; i++) {
            if (number[i] != 0) {
                printf( "%i, ", number[i] );
            }
        }
        printf( "\n" );
    }
    
    void add(int newNumber) {
        for (int i=0; i<maxSize; i++) {
            if (number[i] == 0) {
                number[i] = newNumber;
                break;
            }
        }
    }
    
    void del(int oldNumber) {
        for (int i=0; i<maxSize; i++) {
            if (number[i] == oldNumber) {
                number[i] = 0;
                break;
            }
        }
    }
    
    int main () {
        
        add( 5 );
        add( 7 );
        add( 5 );
        add( 10 );
        add( 5 );
        display();
    
        del( 7 );
        display();
    
        printf( "\n" );
        return 0;
    }
    "without goto we would be wtf'd"

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,675
    A bit like that, but without using any global variables.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 01-15-2014, 04:15 AM
  2. Replies: 3
    Last Post: 12-10-2013, 07:20 PM
  3. how can i make a gotoxy() function in c
    By Masterx in forum C Programming
    Replies: 9
    Last Post: 06-10-2009, 09:58 AM
  4. make function?
    By D3V1LD0G in forum C Programming
    Replies: 8
    Last Post: 08-21-2008, 11:23 AM
  5. How to make a function and call it?
    By AmbliKai in forum C Programming
    Replies: 1
    Last Post: 11-14-2007, 10:30 AM

Tags for this Thread